const CACHE_NAME = 'offline-cache-v1' const ASSETS = [ '/', '/index.html', '/styles.css', '/styles3.css', '/tempo.md', '/bell.png', '/bell.svg', '/by-sa.png', '/cc-by-sa-legalcode.txt', '/symbols.txt' ] self.addEventListener('install', e => { console.log('installing sw') e.waitUntil(caches.open(CACHE_NAME).then(cache => cache.addAll(ASSETS))) self.skipWaiting() }) self.addEventListener('activate', e => { console.log('sw activated') e.waitUntil(self.clients.claim()) }) self.addEventListener('fetch', e => { const url = new URL(e.request.url) const isTarget = ASSETS.includes(url.pathname) || url.pathname.startsWith('/fonts/') if (!isTarget) return e.respondWith( caches.open(CACHE_NAME).then(async cache => { const cachedResponse = await cache.match(e.request) if (cachedResponse) console.log(`cache ${url.pathname}`) else console.log(`fetch ${url.pathname}`) const networkFetch = fetch(e.request).then(networkResponse => { if (networkResponse.ok) { console.log(`updating ${url.pathname}`) cache.put(e.request, networkResponse.clone()) } return networkResponse }).catch(err => { console.error(`failed ${url.pathname}:`, err) }) return cachedResponse || networkFetch }) ) })