mirror of
https://github.com/caty21/forex-dashboard.git
synced 2026-08-12 20:18:05 +00:00
37 lines
1.1 KiB
JavaScript
37 lines
1.1 KiB
JavaScript
const CACHE = "fxdash-v1";
|
|||
|
|
const STATIC = ["/", "/manifest.json", "/icons/icon-192.svg", "/icons/icon-512.svg"];
|
||
|
|
|
||
|
|
self.addEventListener("install", (e) => {
|
||
|
|
e.waitUntil(caches.open(CACHE).then((c) => c.addAll(STATIC)));
|
||
|
|
self.skipWaiting();
|
||
|
|
});
|
||
|
|
|
||
|
|
self.addEventListener("activate", (e) => {
|
||
|
|
e.waitUntil(
|
||
|
|
caches.keys().then((keys) =>
|
||
|
|
Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))
|
||
|
|
)
|
||
|
|
);
|
||
|
|
self.clients.claim();
|
||
|
|
});
|
||
|
|
|
||
|
|
self.addEventListener("fetch", (e) => {
|
||
|
|
// Only cache GET requests for same-origin static assets; pass API calls through
|
||
|
|
const url = new URL(e.request.url);
|
||
|
|
if (e.request.method !== "GET") return;
|
||
|
|
if (url.pathname.startsWith("/api/")) return; // always fresh for API
|
||
|
|
|
||
|
|
e.respondWith(
|
||
|
|
caches.match(e.request).then((cached) => {
|
||
|
|
const network = fetch(e.request).then((res) => {
|
||
|
|
if (res.ok && res.type === "basic") {
|
||
|
|
const clone = res.clone();
|
||
|
|
caches.open(CACHE).then((c) => c.put(e.request, clone));
|
||
|
|
}
|
||
|
|
return res;
|
||
|
|
});
|
||
|
|
return cached || network;
|
||
|
|
})
|
||
|
|
);
|
||
|
|
});
|