flutter_service_worker.js 6.8 KB

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