stremio/apps/admin-ui/public/sw.js
Jos Vooges | STH d53a4f9297 Make admin UI mobile-first with bottom nav and installable PWA support.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-29 02:11:40 +02:00

58 lines
1.6 KiB
JavaScript

/* Media Cluster Ops — lightweight PWA service worker */
const CACHE = "mc-ops-v1";
const PRECACHE = ["/", "/dashboard", "/manifest.webmanifest", "/icons/icon.svg"];
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(CACHE).then((cache) => cache.addAll(PRECACHE)).then(() => self.skipWaiting())
);
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(keys.filter((k) => k !== 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;
// Never cache API / auth — always network
if (url.pathname.startsWith("/api/")) return;
// Navigations: network-first, fallback cache
if (req.mode === "navigate") {
event.respondWith(
fetch(req)
.then((res) => {
const copy = res.clone();
caches.open(CACHE).then((c) => c.put(req, copy));
return res;
})
.catch(() => caches.match(req).then((r) => r || caches.match("/dashboard")))
);
return;
}
// Static assets: stale-while-revalidate
event.respondWith(
caches.match(req).then((cached) => {
const network = fetch(req)
.then((res) => {
if (res.ok) {
const copy = res.clone();
caches.open(CACHE).then((c) => c.put(req, copy));
}
return res;
})
.catch(() => cached);
return cached || network;
})
);
});