The cache-first SW never revalidated the HTML, so a device that loaded the app once kept serving the old bundle forever (new deploys never landed). Now: network-first for navigations/HTML (fresh on every online load, cached shell as offline fallback), cache-first only for immutable hashed assets. Bumped CACHE to director-v2 so the stale v1 cache is purged on activate. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
42 lines
1.6 KiB
JavaScript
42 lines
1.6 KiB
JavaScript
const CACHE = 'director-v2';
|
|
// Precache the navigation shell so a cold, offline launch works.
|
|
const SHELL = ['/', '/index.html'];
|
|
|
|
self.addEventListener('install', (e) => {
|
|
e.waitUntil(caches.open(CACHE).then((c) => c.addAll(SHELL)).then(() => self.skipWaiting()));
|
|
});
|
|
self.addEventListener('activate', (e) => {
|
|
e.waitUntil(
|
|
caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k))))
|
|
.then(() => self.clients.claim())
|
|
);
|
|
});
|
|
self.addEventListener('fetch', (e) => {
|
|
if (e.request.method !== 'GET') return;
|
|
const url = new URL(e.request.url);
|
|
const isDoc = e.request.mode === 'navigate' || url.pathname === '/' || url.pathname.endsWith('.html');
|
|
|
|
if (isDoc) {
|
|
// Network-first for the page itself, so a new deploy lands immediately;
|
|
// fall back to the cached shell when offline.
|
|
e.respondWith(
|
|
fetch(e.request).then((res) => {
|
|
if (res.ok) { const copy = res.clone(); e.waitUntil(caches.open(CACHE).then((c) => c.put(e.request, copy))); }
|
|
return res;
|
|
}).catch(() => caches.match(e.request).then((hit) => hit || caches.match('/index.html')))
|
|
);
|
|
return;
|
|
}
|
|
|
|
// Cache-first for hashed, content-addressed assets (immutable), caching at runtime.
|
|
e.respondWith(
|
|
caches.match(e.request).then((hit) => hit || fetch(e.request).then((res) => {
|
|
if (res.ok && res.type === 'basic') {
|
|
const copy = res.clone();
|
|
e.waitUntil(caches.open(CACHE).then((c) => c.put(e.request, copy)));
|
|
}
|
|
return res;
|
|
}).catch(() => caches.match('/index.html')))
|
|
);
|
|
});
|