StatistaAmeri/frontend/public/sw.js

32 lines
918 B
JavaScript

// Minimal service worker — enables PWA install + an offline fallback for
// navigations. Live market data is never cached (always hits the network).
const SHELL = "yz-shell-v1";
self.addEventListener("install", (e) => {
e.waitUntil(
caches
.open(SHELL)
.then((c) => c.add("/"))
.catch(() => {}),
);
self.skipWaiting();
});
self.addEventListener("activate", (e) => {
e.waitUntil(
caches
.keys()
.then((keys) => Promise.all(keys.filter((k) => k !== SHELL).map((k) => caches.delete(k))))
.then(() => self.clients.claim()),
);
});
self.addEventListener("fetch", (e) => {
const req = e.request;
// Only handle top-level navigations; let everything else (API, assets) pass through.
if (req.mode !== "navigate") return;
e.respondWith(
fetch(req).catch(() => caches.match("/").then((r) => r || Response.error())),
);
});