const CACHE_NAME = 'podcast-mindmap-v2'; // Core assets to cache immediately const CORE_ASSETS = [ '/', '/index.html', '/d3.v7.min.js', '/manifest.json' ]; // Install: cache core assets self.addEventListener('install', event => { event.waitUntil( caches.open(CACHE_NAME) .then(cache => cache.addAll(CORE_ASSETS)) .then(() => self.skipWaiting()) ); }); // Activate: clean old caches self.addEventListener('activate', event => { event.waitUntil( caches.keys().then(keys => Promise.all(keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k))) ).then(() => self.clients.claim()) ); }); // Fetch: network-first for API, cache-first for static assets self.addEventListener('fetch', event => { const url = new URL(event.request.url); // Skip non-GET requests if (event.request.method !== 'GET') return; // API calls: network-first with no cache if (url.pathname.startsWith('/api/')) { event.respondWith( fetch(event.request).catch(() => new Response(JSON.stringify({ error: 'offline' }), { headers: { 'Content-Type': 'application/json' } }) ) ); return; } // App shell (index.html): network-first, sonst kommen Code-Updates erst beim // uebernaechsten Reload an. Cache-Fallback bleibt fuer Offline-Betrieb erhalten. if (url.pathname === '/' || url.pathname === '/index.html') { event.respondWith( fetch(event.request).then(resp => { if (resp.ok) { const clone = resp.clone(); caches.open(CACHE_NAME).then(cache => cache.put(event.request, clone)); } return resp; }).catch(() => caches.match(event.request)) ); return; } // Audio files: network-first, cache on success (optional offline playback) if (url.pathname.startsWith('/audio/')) { event.respondWith( fetch(event.request).then(resp => { // Only cache complete responses (not range requests) if (resp.status === 200) { const clone = resp.clone(); caches.open(CACHE_NAME).then(cache => cache.put(event.request, clone)); } return resp; }).catch(() => caches.match(event.request)) ); return; } // Static assets + JSON data: stale-while-revalidate event.respondWith( caches.match(event.request).then(cached => { const fetchPromise = fetch(event.request).then(resp => { if (resp.ok) { const clone = resp.clone(); caches.open(CACHE_NAME).then(cache => cache.put(event.request, clone)); } return resp; }).catch(() => cached); return cached || fetchPromise; }) ); });