Files

63 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2026-05-15 00:58:40 +08:00
const CACHE_NAME = "polyweather-v2";
const CACHEABLE_PUBLIC_ASSETS = [
"/favicon.ico",
"/favicon-16x16.png",
"/favicon-32x32.png",
"/apple-touch-icon.png",
"/android-chrome-192x192.png",
"/android-chrome-512x512.png",
"/manifest.webmanifest",
"/site.webmanifest",
];
2026-05-14 21:31:05 +08:00
self.addEventListener("install", (event) => {
event.waitUntil(self.skipWaiting());
});
self.addEventListener("activate", (event) => {
event.waitUntil(
2026-05-15 00:58:40 +08:00
caches
.keys()
.then((keys) =>
Promise.all(
keys
.filter((key) => key !== CACHE_NAME)
.map((key) => caches.delete(key)),
),
)
.then(() => self.clients.claim()),
2026-05-14 21:31:05 +08:00
);
});
self.addEventListener("fetch", (event) => {
const url = new URL(event.request.url);
2026-05-15 00:58:40 +08:00
// Do not cache-first Next.js build chunks. A user can keep an old app shell
// open across deployments; if checkout UI is loaded later, stale/missing
// chunks surface as a generic page fault. Let the browser/Vercel handle
// immutable _next/static asset caching instead.
if (CACHEABLE_PUBLIC_ASSETS.includes(url.pathname)) {
2026-05-14 21:31:05 +08:00
event.respondWith(
caches.open(CACHE_NAME).then((cache) =>
cache.match(event.request).then(
(cached) =>
cached ||
fetch(event.request).then((response) => {
if (response.ok) {
cache.put(event.request, response.clone());
}
return response;
}),
),
),
);
return;
}
event.respondWith(
fetch(event.request).catch(() =>
caches.match(event.request).then((cached) => cached || Response.error()),
),
);
});