Files
forex-dashboard/public/sw.js
T

37 lines
1.1 KiB
JavaScript
Raw Normal View History

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;
})
);
});