'use strict'; /* Minimal service worker: caches the shell so the PWA is installable. Never touches /api/* (live data + SSE streams). Network-first, so open dashboards always run the latest shell when the server is reachable. */ const CACHE = 'proddash-v1'; const SHELL = [ '/', '/style.css', '/shell.js', '/manifest.webmanifest', '/icons/icon-192.png', '/icons/icon-512.png', ]; self.addEventListener('install', (event) => { event.waitUntil( caches.open(CACHE).then((cache) => cache.addAll(SHELL)).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 url = new URL(event.request.url); if (event.request.method !== 'GET' || url.pathname.startsWith('/api/')) return; // Network first, cache fallback event.respondWith( fetch(event.request) .then((res) => { const copy = res.clone(); caches.open(CACHE).then((cache) => cache.put(event.request, copy)); return res; }) .catch(() => caches.match(event.request, { ignoreSearch: true })) ); });