const CACHE_NAME = "macros-v12"; const ASSETS = [ "/", "/index.html", "/styles.css", "/app.js", "/foods.json", "/manifest.json", "/icons/logo.svg", "/icons/icon-192.svg", "/icons/icon-512.svg" ]; self.addEventListener("install", (event) => { event.waitUntil( caches.open(CACHE_NAME).then((cache) => cache.addAll(ASSETS)) ); self.skipWaiting(); }); self.addEventListener("activate", (event) => { event.waitUntil( caches.keys().then((keys) => Promise.all( keys.map((key) => { if (key !== CACHE_NAME) return caches.delete(key); return null; }) ) ) ); self.clients.claim(); }); self.addEventListener("fetch", (event) => { if (event.request.method !== "GET") return; event.respondWith( caches.match(event.request).then((cached) => { if (cached) return cached; return fetch(event.request).then((response) => { const copy = response.clone(); caches.open(CACHE_NAME).then((cache) => { cache.put(event.request, copy); }); return response; }); }) ); });