/* * SiMS POS app-shell cache (S4 deliverable 4). Dead-simple cache-first for * same-origin GET so /pos/ still loads when the store-server is down. * * Deliberate limitations (documented, not bugs): * - The data API is NEVER cached: any /api/* request goes straight to the network * and is allowed to fail when the server is down, so the app's offline queue and * the health-ping drain logic stay in control (a cached /api/health would break * the drain). Only GET is handled; bill POSTs pass through untouched. * - Hashed assets are cached on first fetch (runtime), not precached at build time * — the first load must happen online. The navigation root is best-effort * precached on install. * - Cache-first means a new deploy is picked up only after CACHE is bumped below; * for a counter that values offline resilience over instant updates this is the * right trade, and hashed asset names make stale bundles self-correcting. */ const CACHE = 'sims-pos-shell-v1' self.addEventListener('install', (event) => { event.waitUntil( caches.open(CACHE).then((c) => c.addAll(['./', './index.html']).catch(() => undefined)), ) self.skipWaiting() }) self.addEventListener('activate', (event) => { event.waitUntil( caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))), ) self.clients.claim() }) self.addEventListener('fetch', (event) => { const req = event.request if (req.method !== 'GET') return const url = new URL(req.url) if (url.origin !== self.location.origin) return if (url.pathname.startsWith('/api/')) return // never cache the data/print/health API event.respondWith( caches.match(req).then((hit) => { if (hit !== undefined) return hit return fetch(req) .then((res) => { if (res.ok && res.type === 'basic') { const copy = res.clone() void caches.open(CACHE).then((c) => c.put(req, copy)) } return res }) .catch(() => hit ?? Response.error()) }), ) })