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>
43 lines
1.1 KiB
JavaScript
43 lines
1.1 KiB
JavaScript
const CACHE_NAME = "paperclip-v2";
|
|
|
|
self.addEventListener("install", () => {
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener("activate", (event) => {
|
|
event.waitUntil(
|
|
caches.keys().then((keys) =>
|
|
Promise.all(keys.map((key) => caches.delete(key)))
|
|
)
|
|
);
|
|
self.clients.claim();
|
|
});
|
|
|
|
self.addEventListener("fetch", (event) => {
|
|
const { request } = event;
|
|
const url = new URL(request.url);
|
|
|
|
// Skip non-GET requests and API calls
|
|
if (request.method !== "GET" || url.pathname.startsWith("/api")) {
|
|
return;
|
|
}
|
|
|
|
// Network-first for everything — cache is only an offline fallback
|
|
event.respondWith(
|
|
fetch(request)
|
|
.then((response) => {
|
|
if (response.ok && url.origin === self.location.origin) {
|
|
const clone = response.clone();
|
|
caches.open(CACHE_NAME).then((cache) => cache.put(request, clone));
|
|
}
|
|
return response;
|
|
})
|
|
.catch(() => {
|
|
if (request.mode === "navigate") {
|
|
return caches.match("/") || new Response("Offline", { status: 503 });
|
|
}
|
|
return caches.match(request);
|
|
})
|
|
);
|
|
});
|