focustiki's picture
Upload 12 files
9bcadf3 verified
Raw
History Blame Contribute Delete
2.04 kB
/**
* 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'))
);
});