File size: 1,590 Bytes
a077412 | 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 54 55 56 57 58 59 60 61 62 63 64 65 | // Recap Studio — Service Worker (PWA)
const CACHE = 'recap-v1';
const STATIC = [
'/',
'/manifest.json',
];
// Install: cache static shell
self.addEventListener('install', e => {
e.waitUntil(
caches.open(CACHE).then(c => c.addAll(STATIC))
);
self.skipWaiting();
});
// Activate: clean old caches
self.addEventListener('activate', e => {
e.waitUntil(
caches.keys().then(keys =>
Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k)))
)
);
self.clients.claim();
});
// Fetch: network-first for API, cache-first for shell
self.addEventListener('fetch', e => {
const url = new URL(e.request.url);
// API calls — always network, no cache
if (url.pathname.startsWith('/api/') ||
url.pathname.startsWith('/outputs/') ||
url.pathname.startsWith('/auth/')) {
return;
}
// Navigation (HTML pages) — network first, fallback to cache
if (e.request.mode === 'navigate') {
e.respondWith(
fetch(e.request)
.then(r => {
const clone = r.clone();
caches.open(CACHE).then(c => c.put(e.request, clone));
return r;
})
.catch(() => caches.match('/'))
);
return;
}
// Static assets — cache first
e.respondWith(
caches.match(e.request).then(cached => {
if (cached) return cached;
return fetch(e.request).then(r => {
if (r && r.status === 200 && r.type !== 'opaque') {
const clone = r.clone();
caches.open(CACHE).then(c => c.put(e.request, clone));
}
return r;
});
})
);
});
|