Spaces:
Sleeping
Sleeping
| /** | |
| * 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')) | |
| ); | |
| }); | |