const CACHE_NAME = 'notes2-{{ .VERSION }}' const CACHED_ASSETS = [ '/', '/js/{{ .VERSION }}/app.mjs', ] async function precache() { const cache = await caches.open(CACHE_NAME) return cache.addAll(CACHED_ASSETS) } async function fetchAsset(event) { try { return await fetch(event.request) } catch (e) { const cache = await caches.open(CACHE_NAME) return cache.match(event.request) } } async function cleanupCache() { const keys = await caches.keys() const keysToDelete = keys.map(key => { if (key !== CACHE_NAME) return caches.delete(key) }) return Promise.all(keysToDelete) } self.addEventListener('install', event => { console.log('SERVICE WORKER: install') self.skipWaiting() event.waitUntil(precache()) }) self.addEventListener('activate', event => { console.log('SERVICE WORKER: activate') self.clients.claim() event.waitUntil(cleanupCache()) }) self.addEventListener('fetch', event => { //console.log('SERVICE WORKER: fetch') event.respondWith(fetchAsset(event)) })