'use strict'; const CACHE = 'chimera-shell-v1'; const SHELL = ['/offline.html', '/manifest.webmanifest', '/build/icon.png']; self.addEventListener('install', event => { event.waitUntil(caches.open(CACHE).then(cache => cache.addAll(SHELL)).then(() => self.skipWaiting())); }); self.addEventListener('activate', event => { event.waitUntil(caches.keys().then(keys => Promise.all(keys.filter(key => key.startsWith('chimera-shell-') && key !== CACHE).map(key => caches.delete(key)))).then(() => self.clients.claim())); }); function cacheable(url) { return url.origin === self.location.origin && (url.pathname === '/manifest.webmanifest' || url.pathname === '/offline.html' || url.pathname === '/build/icon.png' || url.pathname.startsWith('/styles/') || url.pathname.startsWith('/modules/')); } self.addEventListener('fetch', event => { if (event.request.method !== 'GET') return; const url = new URL(event.request.url); if (event.request.mode === 'navigate') { event.respondWith(fetch(event.request).catch(() => caches.match('/offline.html'))); return; } if (!cacheable(url)) return; const isIcon = url.pathname === '/build/icon.png'; const cacheFallback = isIcon ? '/build/icon.png' : event.request; event.respondWith( fetch(event.request).then(response => { if (response.ok && response.type === 'basic') { caches.open(CACHE).then(cache => { cache.put(event.request, response.clone()); if (isIcon) cache.put(cacheFallback, response.clone()); }); } return response; }).catch(() => caches.match(cacheFallback)), ); });