/* Netwall service worker: network-first so updates always win, cache as an offline fallback (solo play offline). */ const CACHE = 'netwall-v1'; const CORE = ['./', 'index.html', 'css/style.css', 'js/sim.js', 'js/audio.js', 'js/controllers.js', 'js/render.js', 'js/main.js', 'manifest.webmanifest', 'icons/icon.svg', 'icons/icon-192.png', 'assets/music/tracks.json']; self.addEventListener('install', (e) => { e.waitUntil(caches.open(CACHE).then((c) => c.addAll(CORE)).then(() => self.skipWaiting()).catch(() => self.skipWaiting())); }); self.addEventListener('activate', (e) => { e.waitUntil(caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))).then(() => self.clients.claim())); }); self.addEventListener('fetch', (e) => { const req = e.request; if (req.method !== 'GET') return; const url = new URL(req.url); if (url.origin !== self.location.origin) return; e.respondWith( fetch(req) .then((res) => { if (res.ok && res.type === 'basic') { const copy = res.clone(); caches.open(CACHE).then((c) => c.put(req, copy)).catch(() => {}); } return res; }) .catch(() => caches.match(req).then((r) => r || caches.match('index.html'))), ); });