flutter_service_worker.js 7.8 KB

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