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