flutter_service_worker.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. 'use strict';
  2. const MANIFEST = 'flutter-app-manifest';
  3. const TEMP = 'flutter-temp-cache';
  4. const CACHE_NAME = 'flutter-app-cache';
  5. const RESOURCES = {"assets/AssetManifest.bin": "0b0a3415aad49b6e9bf965ff578614f9",
  6. "assets/AssetManifest.bin.json": "a1fee2517bf598633e2f67fcf3e26c94",
  7. "assets/AssetManifest.json": "99914b932bd37a50b983c5e7c90ae93b",
  8. "assets/FontManifest.json": "7b2a36307916a9721811788013e65289",
  9. "assets/fonts/MaterialIcons-Regular.otf": "0db35ae7a415370b89e807027510caf0",
  10. "assets/NOTICES": "1d15f9e2616e0c487485bbd796a82bb4",
  11. "assets/shaders/ink_sparkle.frag": "ecc85a2e95f5e9f53123dcaf8cb9b6ce",
  12. "canvaskit/canvaskit.js": "26eef3024dbc64886b7f48e1b6fb05cf",
  13. "canvaskit/canvaskit.js.symbols": "efc2cd87d1ff6c586b7d4c7083063a40",
  14. "canvaskit/canvaskit.wasm": "e7602c687313cfac5f495c5eac2fb324",
  15. "canvaskit/chromium/canvaskit.js": "b7ba6d908089f706772b2007c37e6da4",
  16. "canvaskit/chromium/canvaskit.js.symbols": "e115ddcfad5f5b98a90e389433606502",
  17. "canvaskit/chromium/canvaskit.wasm": "ea5ab288728f7200f398f60089048b48",
  18. "canvaskit/skwasm.js": "ac0f73826b925320a1e9b0d3fd7da61c",
  19. "canvaskit/skwasm.js.symbols": "96263e00e3c9bd9cd878ead867c04f3c",
  20. "canvaskit/skwasm.wasm": "828c26a0b1cc8eb1adacbdd0c5e8bcfa",
  21. "canvaskit/skwasm.worker.js": "89990e8c92bcb123999aa81f7e203b1c",
  22. "favicon.png": "a8dac4984aa5229f2f2c176b2b626e1f",
  23. "flutter.js": "4b2350e14c6650ba82871f60906437ea",
  24. "flutter_bootstrap.js": "325864442292aa5f75e899ec22f8f189",
  25. "icons/Icon-192.png": "ab1f3346eb3e23e19304ac088cc236c1",
  26. "icons/Icon-512.png": "29fa67fed2325c5febd96dd9180e60dc",
  27. "icons/Icon-maskable-192.png": "c8230bd12eac2e925faa8531f5633292",
  28. "icons/Icon-maskable-512.png": "4eb4557b51424f785b4dcf7096a9fdde",
  29. "icons/icon.png": "05d3e2fc50953462d65573d2d14a08aa",
  30. "index.html": "7d3b2cf31efb546199ca72c05f92f77d",
  31. "/": "7d3b2cf31efb546199ca72c05f92f77d",
  32. "main.dart.js": "cc24e470e01472dbd8cfa6db657afc85",
  33. "manifest.json": "27a391c5ac472fe33d066bdd6e1c50be",
  34. "version.json": "da11dc51486ca396fa77df44efc636c4"};
  35. // The application shell files that are downloaded before a service worker can
  36. // start.
  37. const CORE = ["main.dart.js",
  38. "index.html",
  39. "flutter_bootstrap.js",
  40. "assets/AssetManifest.bin.json",
  41. "assets/FontManifest.json"];
  42. // During install, the TEMP cache is populated with the application shell files.
  43. self.addEventListener("install", (event) => {
  44. self.skipWaiting();
  45. return event.waitUntil(
  46. caches.open(TEMP).then((cache) => {
  47. return cache.addAll(
  48. CORE.map((value) => new Request(value, {'cache': 'reload'})));
  49. })
  50. );
  51. });
  52. // During activate, the cache is populated with the temp files downloaded in
  53. // install. If this service worker is upgrading from one with a saved
  54. // MANIFEST, then use this to retain unchanged resource files.
  55. self.addEventListener("activate", function(event) {
  56. return event.waitUntil(async function() {
  57. try {
  58. var contentCache = await caches.open(CACHE_NAME);
  59. var tempCache = await caches.open(TEMP);
  60. var manifestCache = await caches.open(MANIFEST);
  61. var manifest = await manifestCache.match('manifest');
  62. // When there is no prior manifest, clear the entire cache.
  63. if (!manifest) {
  64. await caches.delete(CACHE_NAME);
  65. contentCache = await caches.open(CACHE_NAME);
  66. for (var request of await tempCache.keys()) {
  67. var response = await tempCache.match(request);
  68. await contentCache.put(request, response);
  69. }
  70. await caches.delete(TEMP);
  71. // Save the manifest to make future upgrades efficient.
  72. await manifestCache.put('manifest', new Response(JSON.stringify(RESOURCES)));
  73. // Claim client to enable caching on first launch
  74. self.clients.claim();
  75. return;
  76. }
  77. var oldManifest = await manifest.json();
  78. var origin = self.location.origin;
  79. for (var request of await contentCache.keys()) {
  80. var key = request.url.substring(origin.length + 1);
  81. if (key == "") {
  82. key = "/";
  83. }
  84. // If a resource from the old manifest is not in the new cache, or if
  85. // the MD5 sum has changed, delete it. Otherwise the resource is left
  86. // in the cache and can be reused by the new service worker.
  87. if (!RESOURCES[key] || RESOURCES[key] != oldManifest[key]) {
  88. await contentCache.delete(request);
  89. }
  90. }
  91. // Populate the cache with the app shell TEMP files, potentially overwriting
  92. // cache files preserved above.
  93. for (var request of await tempCache.keys()) {
  94. var response = await tempCache.match(request);
  95. await contentCache.put(request, response);
  96. }
  97. await caches.delete(TEMP);
  98. // Save the manifest to make future upgrades efficient.
  99. await manifestCache.put('manifest', new Response(JSON.stringify(RESOURCES)));
  100. // Claim client to enable caching on first launch
  101. self.clients.claim();
  102. return;
  103. } catch (err) {
  104. // On an unhandled exception the state of the cache cannot be guaranteed.
  105. console.error('Failed to upgrade service worker: ' + err);
  106. await caches.delete(CACHE_NAME);
  107. await caches.delete(TEMP);
  108. await caches.delete(MANIFEST);
  109. }
  110. }());
  111. });
  112. // The fetch handler redirects requests for RESOURCE files to the service
  113. // worker cache.
  114. self.addEventListener("fetch", (event) => {
  115. if (event.request.method !== 'GET') {
  116. return;
  117. }
  118. var origin = self.location.origin;
  119. var key = event.request.url.substring(origin.length + 1);
  120. // Redirect URLs to the index.html
  121. if (key.indexOf('?v=') != -1) {
  122. key = key.split('?v=')[0];
  123. }
  124. if (event.request.url == origin || event.request.url.startsWith(origin + '/#') || key == '') {
  125. key = '/';
  126. }
  127. // If the URL is not the RESOURCE list then return to signal that the
  128. // browser should take over.
  129. if (!RESOURCES[key]) {
  130. return;
  131. }
  132. // If the URL is the index.html, perform an online-first request.
  133. if (key == '/') {
  134. return onlineFirst(event);
  135. }
  136. event.respondWith(caches.open(CACHE_NAME)
  137. .then((cache) => {
  138. return cache.match(event.request).then((response) => {
  139. // Either respond with the cached resource, or perform a fetch and
  140. // lazily populate the cache only if the resource was successfully fetched.
  141. return response || fetch(event.request).then((response) => {
  142. if (response && Boolean(response.ok)) {
  143. cache.put(event.request, response.clone());
  144. }
  145. return response;
  146. });
  147. })
  148. })
  149. );
  150. });
  151. self.addEventListener('message', (event) => {
  152. // SkipWaiting can be used to immediately activate a waiting service worker.
  153. // This will also require a page refresh triggered by the main worker.
  154. if (event.data === 'skipWaiting') {
  155. self.skipWaiting();
  156. return;
  157. }
  158. if (event.data === 'downloadOffline') {
  159. downloadOffline();
  160. return;
  161. }
  162. });
  163. // Download offline will check the RESOURCES for all files not in the cache
  164. // and populate them.
  165. async function downloadOffline() {
  166. var resources = [];
  167. var contentCache = await caches.open(CACHE_NAME);
  168. var currentContent = {};
  169. for (var request of await contentCache.keys()) {
  170. var key = request.url.substring(origin.length + 1);
  171. if (key == "") {
  172. key = "/";
  173. }
  174. currentContent[key] = true;
  175. }
  176. for (var resourceKey of Object.keys(RESOURCES)) {
  177. if (!currentContent[resourceKey]) {
  178. resources.push(resourceKey);
  179. }
  180. }
  181. return contentCache.addAll(resources);
  182. }
  183. // Attempt to download the resource online before falling back to
  184. // the offline cache.
  185. function onlineFirst(event) {
  186. return event.respondWith(
  187. fetch(event.request).then((response) => {
  188. return caches.open(CACHE_NAME).then((cache) => {
  189. cache.put(event.request, response.clone());
  190. return response;
  191. });
  192. }).catch((error) => {
  193. return caches.open(CACHE_NAME).then((cache) => {
  194. return cache.match(event.request).then((response) => {
  195. if (response != null) {
  196. return response;
  197. }
  198. throw error;
  199. });
  200. });
  201. })
  202. );
  203. }