|
| 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": "97fc783525dc47c2fd11716962e26865", |
| 7 | +"assets/assets/image/construction.png": "53faa0de8d0dbd7a8ef98461044f5d8b", |
| 8 | +"assets/FontManifest.json": "dc3d03800ccca4601324923c0b1d6d57", |
| 9 | +"assets/fonts/MaterialIcons-Regular.otf": "4e6447691c9509f7acdbf8a931a85ca1", |
| 10 | +"assets/NOTICES": "663b21400892cd5c32825b6b5d3ad5e7", |
| 11 | +"assets/packages/cupertino_icons/assets/CupertinoIcons.ttf": "6d342eb68f170c97609e9da345464e5e", |
| 12 | +"favicon.png": "5dcef449791fa27946b3d35ad8803796", |
| 13 | +"icons/Icon-192.png": "ac9a721a12bbc803b44f645561ecb1e1", |
| 14 | +"icons/Icon-512.png": "96e752610906ba2a93c65f8abe1645f1", |
| 15 | +"index.html": "2687f8c8662c9d16df65a4c28f847e85", |
| 16 | +"/": "2687f8c8662c9d16df65a4c28f847e85", |
| 17 | +"main.dart.js": "f69d6a3e8fc105fdeabda1669e9c127c", |
| 18 | +"manifest.json": "5a2c0526f1fe73e6f211bc7a9d15c210", |
| 19 | +"version.json": "426313f2f3133c2f20415344c4a22df3" |
| 20 | +}; |
| 21 | + |
| 22 | +// The application shell files that are downloaded before a service worker can |
| 23 | +// start. |
| 24 | +const CORE = [ |
| 25 | + "/", |
| 26 | +"main.dart.js", |
| 27 | +"index.html", |
| 28 | +"assets/NOTICES", |
| 29 | +"assets/AssetManifest.json", |
| 30 | +"assets/FontManifest.json"]; |
| 31 | +// During install, the TEMP cache is populated with the application shell files. |
| 32 | +self.addEventListener("install", (event) => { |
| 33 | + self.skipWaiting(); |
| 34 | + return event.waitUntil( |
| 35 | + caches.open(TEMP).then((cache) => { |
| 36 | + return cache.addAll( |
| 37 | + CORE.map((value) => new Request(value, {'cache': 'reload'}))); |
| 38 | + }) |
| 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 | + |
| 99 | +// The fetch handler redirects requests for RESOURCE files to the service |
| 100 | +// worker cache. |
| 101 | +self.addEventListener("fetch", (event) => { |
| 102 | + if (event.request.method !== 'GET') { |
| 103 | + return; |
| 104 | + } |
| 105 | + var origin = self.location.origin; |
| 106 | + var key = event.request.url.substring(origin.length + 1); |
| 107 | + // Redirect URLs to the index.html |
| 108 | + if (key.indexOf('?v=') != -1) { |
| 109 | + key = key.split('?v=')[0]; |
| 110 | + } |
| 111 | + if (event.request.url == origin || event.request.url.startsWith(origin + '/#') || key == '') { |
| 112 | + key = '/'; |
| 113 | + } |
| 114 | + // If the URL is not the RESOURCE list then return to signal that the |
| 115 | + // browser should take over. |
| 116 | + if (!RESOURCES[key]) { |
| 117 | + return; |
| 118 | + } |
| 119 | + // If the URL is the index.html, perform an online-first request. |
| 120 | + if (key == '/') { |
| 121 | + return onlineFirst(event); |
| 122 | + } |
| 123 | + event.respondWith(caches.open(CACHE_NAME) |
| 124 | + .then((cache) => { |
| 125 | + return cache.match(event.request).then((response) => { |
| 126 | + // Either respond with the cached resource, or perform a fetch and |
| 127 | + // lazily populate the cache. |
| 128 | + return response || fetch(event.request).then((response) => { |
| 129 | + cache.put(event.request, response.clone()); |
| 130 | + return response; |
| 131 | + }); |
| 132 | + }) |
| 133 | + }) |
| 134 | + ); |
| 135 | +}); |
| 136 | + |
| 137 | +self.addEventListener('message', (event) => { |
| 138 | + // SkipWaiting can be used to immediately activate a waiting service worker. |
| 139 | + // This will also require a page refresh triggered by the main worker. |
| 140 | + if (event.data === 'skipWaiting') { |
| 141 | + self.skipWaiting(); |
| 142 | + return; |
| 143 | + } |
| 144 | + if (event.data === 'downloadOffline') { |
| 145 | + downloadOffline(); |
| 146 | + return; |
| 147 | + } |
| 148 | +}); |
| 149 | + |
| 150 | +// Download offline will check the RESOURCES for all files not in the cache |
| 151 | +// and populate them. |
| 152 | +async function downloadOffline() { |
| 153 | + var resources = []; |
| 154 | + var contentCache = await caches.open(CACHE_NAME); |
| 155 | + var currentContent = {}; |
| 156 | + for (var request of await contentCache.keys()) { |
| 157 | + var key = request.url.substring(origin.length + 1); |
| 158 | + if (key == "") { |
| 159 | + key = "/"; |
| 160 | + } |
| 161 | + currentContent[key] = true; |
| 162 | + } |
| 163 | + for (var resourceKey of Object.keys(RESOURCES)) { |
| 164 | + if (!currentContent[resourceKey]) { |
| 165 | + resources.push(resourceKey); |
| 166 | + } |
| 167 | + } |
| 168 | + return contentCache.addAll(resources); |
| 169 | +} |
| 170 | + |
| 171 | +// Attempt to download the resource online before falling back to |
| 172 | +// the offline cache. |
| 173 | +function onlineFirst(event) { |
| 174 | + return event.respondWith( |
| 175 | + fetch(event.request).then((response) => { |
| 176 | + return caches.open(CACHE_NAME).then((cache) => { |
| 177 | + cache.put(event.request, response.clone()); |
| 178 | + return response; |
| 179 | + }); |
| 180 | + }).catch((error) => { |
| 181 | + return caches.open(CACHE_NAME).then((cache) => { |
| 182 | + return cache.match(event.request).then((response) => { |
| 183 | + if (response != null) { |
| 184 | + return response; |
| 185 | + } |
| 186 | + throw error; |
| 187 | + }); |
| 188 | + }); |
| 189 | + }) |
| 190 | + ); |
| 191 | +} |
0 commit comments