fix(ui): switch service worker to network-first to prevent stale content
The cache-first strategy for static assets was causing pages to serve stale content on refresh. Switched to network-first for all requests with cache as offline-only fallback. Bumped cache version to clear existing caches on activate. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,21 +1,13 @@
|
|||||||
const CACHE_NAME = "paperclip-v1";
|
const CACHE_NAME = "paperclip-v2";
|
||||||
const STATIC_ASSETS = ["/", "/favicon.ico", "/favicon.svg"];
|
|
||||||
|
|
||||||
self.addEventListener("install", (event) => {
|
self.addEventListener("install", () => {
|
||||||
event.waitUntil(
|
|
||||||
caches.open(CACHE_NAME).then((cache) => cache.addAll(STATIC_ASSETS))
|
|
||||||
);
|
|
||||||
self.skipWaiting();
|
self.skipWaiting();
|
||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener("activate", (event) => {
|
self.addEventListener("activate", (event) => {
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
caches.keys().then((keys) =>
|
caches.keys().then((keys) =>
|
||||||
Promise.all(
|
Promise.all(keys.map((key) => caches.delete(key)))
|
||||||
keys
|
|
||||||
.filter((key) => key !== CACHE_NAME)
|
|
||||||
.map((key) => caches.delete(key))
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
self.clients.claim();
|
self.clients.claim();
|
||||||
@@ -30,31 +22,21 @@ self.addEventListener("fetch", (event) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Network-first for navigation requests (SPA)
|
// Network-first for everything — cache is only an offline fallback
|
||||||
if (request.mode === "navigate") {
|
|
||||||
event.respondWith(
|
|
||||||
fetch(request)
|
|
||||||
.then((response) => {
|
|
||||||
const clone = response.clone();
|
|
||||||
caches.open(CACHE_NAME).then((cache) => cache.put(request, clone));
|
|
||||||
return response;
|
|
||||||
})
|
|
||||||
.catch(() => caches.match("/"))
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cache-first for static assets
|
|
||||||
event.respondWith(
|
event.respondWith(
|
||||||
caches.match(request).then((cached) => {
|
fetch(request)
|
||||||
if (cached) return cached;
|
.then((response) => {
|
||||||
return fetch(request).then((response) => {
|
|
||||||
if (response.ok && url.origin === self.location.origin) {
|
if (response.ok && url.origin === self.location.origin) {
|
||||||
const clone = response.clone();
|
const clone = response.clone();
|
||||||
caches.open(CACHE_NAME).then((cache) => cache.put(request, clone));
|
caches.open(CACHE_NAME).then((cache) => cache.put(request, clone));
|
||||||
}
|
}
|
||||||
return response;
|
return response;
|
||||||
});
|
})
|
||||||
})
|
.catch(() => {
|
||||||
|
if (request.mode === "navigate") {
|
||||||
|
return caches.match("/") || new Response("Offline", { status: 503 });
|
||||||
|
}
|
||||||
|
return caches.match(request);
|
||||||
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user