From 500b3fa780cd45c7d2f4ea9520ff5951cb67748d Mon Sep 17 00:00:00 2001 From: marwin Date: Thu, 19 Mar 2026 22:05:50 +0100 Subject: [PATCH] Fix SW: only cache static assets, not API/HTML responses Caching /books/ caused stale empty list after upload. Caching / caused stale window.USER_ID after session changes. Co-Authored-By: Claude Sonnet 4.6 --- static/js/sw.js | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/static/js/sw.js b/static/js/sw.js index d52308e..bb6afc0 100644 --- a/static/js/sw.js +++ b/static/js/sw.js @@ -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); + }) + ); + } });