/* VistAi — Service Worker v1.0 */ const CACHE = 'vistai-v1'; const SHELL = ['/', '/index.html', '/manifest.json', '/icon-192.png', '/icon-512.png']; 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 => { // API — always network if (e.request.url.includes('api.php')) { e.respondWith( fetch(e.request).catch(() => new Response(JSON.stringify({ success: false, message: '// OFFLINE — network unavailable' }), { headers: { 'Content-Type': 'application/json' } }) ) ); return; } // Shell — cache first e.respondWith( caches.match(e.request).then(cached => { if (cached) return cached; return fetch(e.request).then(res => { if (e.request.method === 'GET' && res.status === 200) { caches.open(CACHE).then(c => c.put(e.request, res.clone())); } return res; }).catch(() => caches.match('/index.html')); }) ); });