'use strict'; // Cache name is injected by build.ps1 (token replaced with git version). // When the binary is built without the script, __BUILDVER__ stays literal // and the SW still works — it just won't auto-bust on updates. const CACHE = 'roadamp-__BUILDVER__'; const SHELL = ['/', '/app.js', '/style.css', '/manifest.json', '/icon.svg']; self.addEventListener('install', e => { e.waitUntil( caches.open(CACHE) .then(c => c.addAll(SHELL)) .then(() => 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 url = new URL(e.request.url); // Pass through API calls and WebSocket upgrades uncached. if (url.pathname.startsWith('/api/') || url.pathname === '/ws') return; e.respondWith( caches.match(e.request).then(cached => { if (cached) return cached; return fetch(e.request).then(res => { const clone = res.clone(); caches.open(CACHE).then(c => c.put(e.request, clone)); return res; }); }) ); });