- Bump sw.js cache from v2 to v3 - Unregister all old service workers on page load - Stop caching HTML/navigation (caused stale CSS refs after rebuild)
66 lines
1.3 KiB
JavaScript
66 lines
1.3 KiB
JavaScript
const CACHE = "atom-v3";
|
|
const API_CACHE = "atom-api-v3";
|
|
|
|
self.addEventListener("install", () => self.skipWaiting());
|
|
|
|
self.addEventListener("activate", (event) => {
|
|
event.waitUntil(
|
|
caches
|
|
.keys()
|
|
.then((keys) =>
|
|
Promise.all(
|
|
keys.filter((k) => k !== CACHE && k !== API_CACHE).map((k) => caches.delete(k)),
|
|
),
|
|
)
|
|
.then(() => self.clients.claim()),
|
|
);
|
|
});
|
|
|
|
self.addEventListener("fetch", (event) => {
|
|
const req = event.request;
|
|
if (req.method !== "GET") return;
|
|
const url = new URL(req.url);
|
|
if (url.origin !== self.location.origin) return;
|
|
|
|
if (
|
|
url.pathname.startsWith("/assets/") ||
|
|
url.pathname.startsWith("/_next/static/")
|
|
) {
|
|
event.respondWith(
|
|
caches.open(CACHE).then((cache) =>
|
|
cache.match(req).then(
|
|
(hit) =>
|
|
hit ||
|
|
fetch(req).then((res) => {
|
|
if (res.ok) cache.put(req, res.clone());
|
|
return res;
|
|
}),
|
|
),
|
|
),
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (
|
|
url.pathname.startsWith("/api/home") ||
|
|
url.pathname.startsWith("/api/online") ||
|
|
url.pathname.startsWith("/api/radio/config")
|
|
) {
|
|
event.respondWith(
|
|
caches.open(API_CACHE).then((cache) =>
|
|
fetch(req)
|
|
.then((res) => {
|
|
if (res.ok) cache.put(req, res.clone());
|
|
return res;
|
|
})
|
|
.catch(() => cache.match(req)),
|
|
),
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (req.mode === "navigate") {
|
|
event.respondWith(fetch(req));
|
|
}
|
|
});
|