/** * Service Worker — DE Knowledge Assistant PWA * Strategy: Cache-first for static assets, network-first for API calls * This enables "Add to Home Screen" on iOS Safari and offline shell loading */ const CACHE = 'de-assistant-v1'; const STATIC_ASSETS = ['/', '/index.html', '/manifest.json']; // ── Install ────────────────────────────────────────────────────────────────── self.addEventListener('install', event => { event.waitUntil( caches.open(CACHE).then(cache => cache.addAll(STATIC_ASSETS)) ); self.skipWaiting(); }); // ── Activate ───────────────────────────────────────────────────────────────── 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(); }); // ── Fetch ───────────────────────────────────────────────────────────────────── self.addEventListener('fetch', event => { const { request } = event; const url = new URL(request.url); // API calls → always network (never cache LLM responses) if (url.pathname.startsWith('/api/')) { event.respondWith(fetch(request)); return; } // Static assets → cache-first, fall back to network event.respondWith( caches.match(request).then(cached => { if (cached) return cached; return fetch(request).then(response => { if (response.ok) { const clone = response.clone(); caches.open(CACHE).then(cache => cache.put(request, clone)); } return response; }); }).catch(() => caches.match('/index.html')) ); });