CACHE_NAME von v1 auf v2 erhoeht; activate-Handler loescht damit den alten Cache und liefert beim Reload die aktuelle index.html. Zusaetzlich laeuft '/' und '/index.html' jetzt network-first, sodass weitere Code-Updates schon beim naechsten Reload greifen statt erst beim uebernaechsten. Der Cache-Fallback bleibt fuer Offline-Betrieb. Hintergrund: Mit stale-while-revalidate fuer die App-Shell sah der Browser nach dem Mindmap-Fix weiterhin die kaputte index.html, weil die alte Version aus dem Cache kam und das Update erst im Hintergrund geholt wurde. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
93 lines
2.6 KiB
JavaScript
93 lines
2.6 KiB
JavaScript
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;
|
|
})
|
|
);
|
|
});
|