const CACHE_PREFIX = 'dartboat-' const CACHE_VERSION = '2'; const CACHE_NAME = `${CACHE_PREFIX}${CACHE_VERSION}`; const FILES = [ './', 'style.css', 'dartboat.js', 'dartboat_wasm.js', 'dartboat_wasm.wasm', 'fonts/bootstrap-icons-sub.woff2', 'fonts/inter-num.woff2' ] function swlog(str) { console.log(`[service worker] ${str}`); } self.addEventListener('install', e => { swlog('installing'); e.waitUntil((async() => { swlog(`initialising cache ${CACHE_NAME}`); const cache = await caches.open(CACHE_NAME); await cache.addAll(FILES); })()); swlog('installed'); }); self.addEventListener('activate', e => { e.waitUntil(caches.keys().then((keys) => { return Promise.all(keys.map((k) => { if (k === CACHE_NAME || !k.startsWith(CACHE_PREFIX)) return; swlog(`deleting cache ${k}`); return caches.delete(k); })); })); }); self.addEventListener('fetch', e => { e.respondWith((async () => { const cache = await caches.open(CACHE_NAME); const cached = await cache.match(e.request); swlog(`${cached ? 'hit' : 'miss'} ${e.request.url}`); if (cached) return cached; const resp = await fetch(e.request); if (!resp.ok) { swlog(`ignore status:${resp.status} ${e.request.url}`); } else { swlog(`put ${e.request.url}`); cache.put(e.request, resp.clone()); } return resp; })()); });