const CACHE_NAME = "3d-constructs-v3"; const SHELL_ASSETS = [ "/", "/index.html", "/favicon.svg", "/logo.png", "/manifest.webmanifest" ]; self.addEventListener("install", (e) => { e.waitUntil( caches.open(CACHE_NAME).then((cache) => cache.addAll(SHELL_ASSETS)) ); self.skipWaiting(); }); self.addEventListener("activate", (e) => { e.waitUntil( caches.keys().then((keys) => { return Promise.all( keys.map((key) => { if (key !== CACHE_NAME) { return caches.delete(key); } }) ); }) ); self.clients.claim(); }); self.addEventListener("fetch", (e) => { if (e.request.method !== "GET" || !e.request.url.startsWith(self.location.origin)) { return; } e.respondWith( fetch(e.request) .then((networkResponse) => { if (networkResponse && networkResponse.status === 200) { const responseToCache = networkResponse.clone(); caches.open(CACHE_NAME).then((cache) => { cache.put(e.request, responseToCache); }); } return networkResponse; }) .catch(() => { return caches.match(e.request).then((cachedResponse) => { return cachedResponse || caches.match("/"); }); }) ); });