mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-01 17:39:39 +02:00
38 lines
1.1 KiB
JavaScript
38 lines
1.1 KiB
JavaScript
|
|
// Beep Graph service worker — minimal cache-first for app shell
|
||
|
|
const CACHE_NAME = "beepgraph-v1";
|
||
|
|
const APP_SHELL = ["/", "/index.html"];
|
||
|
|
|
||
|
|
self.addEventListener("install", (event) => {
|
||
|
|
event.waitUntil(
|
||
|
|
caches.open(CACHE_NAME).then((cache) => cache.addAll(APP_SHELL))
|
||
|
|
);
|
||
|
|
self.skipWaiting();
|
||
|
|
});
|
||
|
|
|
||
|
|
self.addEventListener("activate", (event) => {
|
||
|
|
event.waitUntil(
|
||
|
|
caches.keys().then((keys) =>
|
||
|
|
Promise.all(keys.filter((k) => k !== CACHE_NAME).map((k) => caches.delete(k)))
|
||
|
|
)
|
||
|
|
);
|
||
|
|
self.clients.claim();
|
||
|
|
});
|
||
|
|
|
||
|
|
self.addEventListener("fetch", (event) => {
|
||
|
|
// Only cache GET requests for same-origin navigation/assets
|
||
|
|
if (event.request.method !== "GET") return;
|
||
|
|
const url = new URL(event.request.url);
|
||
|
|
if (url.origin !== self.location.origin) return;
|
||
|
|
|
||
|
|
// Network-first for HTML (always get fresh app), cache-first for assets
|
||
|
|
if (event.request.mode === "navigate") {
|
||
|
|
event.respondWith(
|
||
|
|
fetch(event.request).catch(() => caches.match("/index.html"))
|
||
|
|
);
|
||
|
|
} else {
|
||
|
|
event.respondWith(
|
||
|
|
caches.match(event.request).then((cached) => cached || fetch(event.request))
|
||
|
|
);
|
||
|
|
}
|
||
|
|
});
|