- Introduce redis-backed Prisma query cache (prisma-cache.ts) with per-model TTL - Replace force-dynamic with revalidate=60 + generateStaticParams on news/[slug] - Wrap article query with Redis cache (60s TTL) - Upgrade service worker to v2 with dedicated API cache and stale-while-revalidate for static assets
74 lines
1.6 KiB
JavaScript
74 lines
1.6 KiB
JavaScript
const CACHE = "atom-v2";
|
|
const API_CACHE = "atom-api-v2";
|
|
|
|
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)
|
|
.then((res) => {
|
|
const copy = res.clone();
|
|
caches.open(CACHE).then((c) => c.put(req, copy)).catch(() => {});
|
|
return res;
|
|
})
|
|
.catch(() => caches.match(req).then((hit) => hit || caches.match("/"))),
|
|
);
|
|
}
|
|
});
|