const SHELL_CACHE='fixfin-shell-v3.19'; const DATA_CACHE='fixfin-data-v1'; const APP_SHELL=['/','/manifest.webmanifest','/icons/icon-192.png','/icons/icon-512.png','/icons/apple-touch-icon.png']; self.addEventListener('install',event=>{ event.waitUntil(caches.open(SHELL_CACHE).then(cache=>cache.addAll(APP_SHELL)).then(()=>self.skipWaiting())); }); self.addEventListener('activate',event=>{ event.waitUntil(Promise.all([ caches.keys().then(keys=>Promise.all(keys.filter(key=>key.startsWith('fixfin-shell-')&&key!==SHELL_CACHE).map(key=>caches.delete(key)))), self.clients.claim(), ])); }); self.addEventListener('fetch',event=>{ const request=event.request; if(request.method!=='GET') return; const url=new URL(request.url); if(url.origin!==self.location.origin) return; if(url.pathname==='/api/data') { event.respondWith(fetch(request).then(response=>{ if(response.ok) caches.open(DATA_CACHE).then(cache=>cache.put(request,response.clone())); return response; }).catch(()=>caches.match(request).then(cached=>cached||new Response(JSON.stringify({error:'Offline und keine zwischengespeicherten Daten verfügbar.'}),{status:503,headers:{'Content-Type':'application/json'}})))); return; } if(url.pathname.startsWith('/api/')) return; if(request.mode==='navigate') { event.respondWith(fetch(request).then(response=>{ caches.open(SHELL_CACHE).then(cache=>cache.put('/',response.clone())); return response; }).catch(()=>caches.match('/'))); return; } event.respondWith(caches.match(request).then(cached=>{ const network=fetch(request).then(response=>{ if(response.ok) caches.open(SHELL_CACHE).then(cache=>cache.put(request,response.clone())); return response; }).catch(()=>cached); return cached||network; })); });