Fix SW: only cache static assets, not API/HTML responses
All checks were successful
Build and push Docker image / build (push) Successful in 12s
Test / test (push) Successful in 15s

Caching /books/ caused stale empty list after upload. Caching / caused
stale window.USER_ID after session changes.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
marwin 2026-03-19 22:05:50 +01:00
parent 5ce9cec581
commit 500b3fa780

View file

@ -2,10 +2,9 @@
* diora service worker caches the app shell for offline use.
*/
const CACHE = 'diora-v3';
const CACHE = 'diora-v4';
const PODCAST_CACHE = 'diora-podcast-v1';
const SHELL = [
'/',
'/static/css/app.css',
'/static/js/app.js',
'/static/manifest.json',
@ -72,19 +71,13 @@ self.addEventListener('fetch', function (event) {
return;
}
event.respondWith(
caches.match(event.request).then(function (cached) {
if (cached) return cached;
return fetch(event.request).then(function (response) {
// Cache successful GET responses for shell assets
if (response && response.status === 200 && response.type === 'basic') {
const clone = response.clone();
caches.open(CACHE).then(function (cache) {
cache.put(event.request, clone);
});
}
return response;
});
})
);
// Cache-first only for pre-defined shell assets; everything else hits the network
const isShell = SHELL.some(function (s) { return url.pathname === s; });
if (isShell) {
event.respondWith(
caches.match(event.request).then(function (cached) {
return cached || fetch(event.request);
})
);
}
});