Files

18 lines
813 B
JavaScript
Raw Permalink Normal View History

// Network-first for the app shell so a deploy is picked up on next load; cached copy
// only when offline. API calls and files are never cached here.
const CACHE = 'wb-shell-v1';
self.addEventListener('install', (e) => self.skipWaiting());
self.addEventListener('activate', (e) => e.waitUntil(self.clients.claim()));
self.addEventListener('fetch', (e) => {
const url = new URL(e.request.url);
if (e.request.method !== 'GET' || url.origin !== location.origin) return;
if (url.pathname.startsWith('/api/') || url.pathname.startsWith('/files/')) return;
e.respondWith(
fetch(e.request).then((res) => {
const copy = res.clone();
caches.open(CACHE).then((c) => c.put(e.request, copy));
return res;
}).catch(() => caches.match(e.request).then((r) => r || caches.match('/')))
);
});