|
| 1 | +/** |
| 2 | + * Service Worker to enable PWA functionality. |
| 3 | + * |
| 4 | + * @see https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps/Guides/Caching |
| 5 | + * @see https://web.archive.org/web/20231106070218/https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps/Guides/Caching |
| 6 | + */ |
| 7 | +const CACHE_NAME = "mylist_1"; |
| 8 | +const PRECACHED_RESOURCES = [ |
| 9 | + "./", |
| 10 | + "./index.html", |
| 11 | + "./assets/styles.css", |
| 12 | + "./apple-touch-icon.png", |
| 13 | + "./favicon-32x32.png", |
| 14 | + "./favicon-16x16.png", |
| 15 | + "./assets/lib.js", |
| 16 | + "./assets/components.js", |
| 17 | + "./assets/main.js", |
| 18 | + "./assets/sortable.min.js", |
| 19 | + "./assets/sticky-tape.png", |
| 20 | + "./assets/images/check.svg", |
| 21 | + "./assets/icons/check.svg", |
| 22 | + "./assets/icons/info.svg", |
| 23 | + "./assets/icons/list.svg", |
| 24 | + "./assets/icons/loop.svg", |
| 25 | + "./assets/fonts/xkcd.otf", |
| 26 | +]; |
| 27 | + |
| 28 | +function isCacheable(request) { |
| 29 | + const url = new URL(request.url); |
| 30 | + |
| 31 | + return !url.pathname.endsWith(".json"); |
| 32 | +} |
| 33 | + |
| 34 | +function precache() { |
| 35 | + return caches.open(CACHE_NAME) |
| 36 | + .then((cache) => { |
| 37 | + console.debug('[mylist:sw.js] #precache: adding cache for', PRECACHED_RESOURCES); |
| 38 | + |
| 39 | + return cache.addAll(PRECACHED_RESOURCES); |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +function fetchResponsePromise(request, cache) { |
| 44 | + console.warn('Fetching response for', request.url); |
| 45 | + |
| 46 | + return fetch(request) |
| 47 | + .then(function(networkResponse) { |
| 48 | + if (networkResponse.ok) { |
| 49 | + console.info('Storing cache for', request.url); |
| 50 | + cache.put(request, networkResponse.clone()); |
| 51 | + } |
| 52 | + |
| 53 | + return networkResponse; |
| 54 | + }) |
| 55 | + .catch(function(err) { |
| 56 | + console.error('Could not fetch', request.url, '. Error:', err); |
| 57 | + return Response.error(); |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +function cacheFirstWithRefresh(request) { |
| 62 | + return caches |
| 63 | + .open(CACHE_NAME) |
| 64 | + .then(function(cache) { |
| 65 | + if (cache.match(request)) { |
| 66 | + console.info('Found cache for', request.url); |
| 67 | + return cache.match(request); |
| 68 | + } |
| 69 | + |
| 70 | + return fetchResponsePromise(request, cache); |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +self.addEventListener("fetch", function(ev) { |
| 75 | + const url = new URL(ev.request.url); |
| 76 | + |
| 77 | + if (!url.pathname.endsWith(".json")) { |
| 78 | + ev.respondWith( cacheFirstWithRefresh(ev.request) ); |
| 79 | + } |
| 80 | +}); |
| 81 | + |
| 82 | +self.addEventListener("install", function(ev) { |
| 83 | + console.log('[mylist:sw.js] #install: pre-caching files...'); |
| 84 | + |
| 85 | + ev.waitUntil(precache()); |
| 86 | +}); |
| 87 | + |
| 88 | +self.addEventListener("push", function(ev) { |
| 89 | + console.debug('[mylist:sw.js] push event received:', ev); |
| 90 | +}); |
| 91 | + |
| 92 | +function enableNavigationPreload() { |
| 93 | + if (self.registration.navigationPreload) { |
| 94 | + return self.registration.navigationPreload.enable(); |
| 95 | + } |
| 96 | + |
| 97 | + return null; |
| 98 | +}; |
| 99 | + |
| 100 | +self.addEventListener('activate', (ev) => { |
| 101 | + ev.waitUntil(enableNavigationPreload()); |
| 102 | +}); |
| 103 | + |
| 104 | +console.log('[mylist:sw.js] loaded.'); |
0 commit comments