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