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 <noreply@anthropic.com>
This commit is contained in:
parent
5ce9cec581
commit
500b3fa780
1 changed files with 10 additions and 17 deletions
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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(
|
event.respondWith(
|
||||||
caches.match(event.request).then(function (cached) {
|
caches.match(event.request).then(function (cached) {
|
||||||
if (cached) return cached;
|
return cached || fetch(event.request);
|
||||||
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;
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue