flutter_service_worker.js 7.2 KB

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