Spaces:
Sleeping
Sleeping
File size: 2,042 Bytes
9bcadf3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | /**
* 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'))
);
});
|