7d1649d278
Per-file content-hash versioning on every /static reference, immutable cache headers on versioned URLs, no-cache on HTML, auto-bumped service worker cache name with stale-while-revalidate for assets, and a controllerchange listener that silently reloads the page when a new SW takes control. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
61 lines
2.1 KiB
Django/Jinja
61 lines
2.1 KiB
Django/Jinja
const CACHE = 'nhl-scoreboard-{{ app_version }}';
|
|
const PRECACHE = {{ precache | tojson }};
|
|
|
|
self.addEventListener('install', event => {
|
|
event.waitUntil(caches.open(CACHE).then(c => c.addAll(PRECACHE)));
|
|
self.skipWaiting();
|
|
});
|
|
|
|
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();
|
|
});
|
|
|
|
self.addEventListener('fetch', event => {
|
|
if (event.request.method !== 'GET') return;
|
|
const { pathname } = new URL(event.request.url);
|
|
|
|
// Network-first for the live scoreboard API — stale data is useless
|
|
if (pathname === '/scoreboard') {
|
|
event.respondWith(
|
|
fetch(event.request).catch(() => caches.match(event.request))
|
|
);
|
|
return;
|
|
}
|
|
|
|
// Network-first for HTML pages (root, bracket, series detail) so the
|
|
// very next request after a deploy lands the new asset URLs
|
|
if (pathname === '/' || pathname === '/bracket' || pathname.startsWith('/series/')) {
|
|
event.respondWith(
|
|
fetch(event.request).then(response => {
|
|
if (response.ok) {
|
|
const clone = response.clone();
|
|
caches.open(CACHE).then(c => c.put(event.request, clone));
|
|
}
|
|
return response;
|
|
}).catch(() => caches.match(event.request))
|
|
);
|
|
return;
|
|
}
|
|
|
|
// Stale-while-revalidate for everything else (versioned static assets,
|
|
// manifest, icons): return cached bytes immediately, refresh in the
|
|
// background so the next load is current
|
|
event.respondWith(
|
|
caches.match(event.request).then(cached => {
|
|
const networkFetch = fetch(event.request).then(response => {
|
|
if (response.ok) {
|
|
const clone = response.clone();
|
|
caches.open(CACHE).then(c => c.put(event.request, clone));
|
|
}
|
|
return response;
|
|
}).catch(() => cached);
|
|
return cached || networkFetch;
|
|
})
|
|
);
|
|
});
|