const CACHE_NAME = 'study-lab-shell-v4'; const APP_SHELL = [ './', './index.html', './styles.css', './app.js', './manifest.webmanifest', './icons/icon-192.png', './icons/icon-512.png', './icons/icon-maskable-512.png', './icons/apple-touch-icon.png' ]; self.addEventListener('install', event => { event.waitUntil(caches.open(CACHE_NAME).then(cache => cache.addAll(APP_SHELL))); self.skipWaiting(); }); self.addEventListener('activate', event => { event.waitUntil( caches.keys() .then(keys => Promise.all(keys.filter(key => key !== CACHE_NAME).map(key => caches.delete(key)))) .then(() => self.clients.claim()) ); }); self.addEventListener('fetch', event => { if (event.request.method !== 'GET') return; const url = new URL(event.request.url); if (url.origin !== self.location.origin) return; event.respondWith( fetch(event.request) .then(response => { if (response.ok) { const copy = response.clone(); caches.open(CACHE_NAME).then(cache => cache.put(event.request, copy)); } return response; }) .catch(async () => { const cached = await caches.match(event.request); if (cached) return cached; if (event.request.mode === 'navigate') return caches.match('./index.html'); return Response.error(); }) ); });