2026-03-06 09:08:39 -06:00
|
|
|
const CACHE_NAME = "paperclip-v2";
|
2026-03-05 17:01:07 -06:00
|
|
|
|
2026-03-06 09:08:39 -06:00
|
|
|
self.addEventListener("install", () => {
|
2026-03-05 17:01:07 -06:00
|
|
|
self.skipWaiting();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
self.addEventListener("activate", (event) => {
|
|
|
|
|
event.waitUntil(
|
|
|
|
|
caches.keys().then((keys) =>
|
2026-03-06 09:08:39 -06:00
|
|
|
Promise.all(keys.map((key) => caches.delete(key)))
|
2026-03-05 17:01:07 -06:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-06 09:08:39 -06:00
|
|
|
// Network-first for everything — cache is only an offline fallback
|
2026-03-05 17:01:07 -06:00
|
|
|
event.respondWith(
|
2026-03-06 09:08:39 -06:00
|
|
|
fetch(request)
|
|
|
|
|
.then((response) => {
|
2026-03-05 17:01:07 -06:00
|
|
|
if (response.ok && url.origin === self.location.origin) {
|
|
|
|
|
const clone = response.clone();
|
|
|
|
|
caches.open(CACHE_NAME).then((cache) => cache.put(request, clone));
|
|
|
|
|
}
|
|
|
|
|
return response;
|
2026-03-06 09:08:39 -06:00
|
|
|
})
|
|
|
|
|
.catch(() => {
|
|
|
|
|
if (request.mode === "navigate") {
|
|
|
|
|
return caches.match("/") || new Response("Offline", { status: 503 });
|
|
|
|
|
}
|
|
|
|
|
return caches.match(request);
|
|
|
|
|
})
|
2026-03-05 17:01:07 -06:00
|
|
|
);
|
|
|
|
|
});
|