feat(pwa): auto-update service worker so new releases take effect without manual cache clearing
- app.js: reload once on SW controllerchange (guarded against loops / first install) and poll reg.update() hourly for long-open tabs - sw.js: bump cache to v24 so the new (network-first) worker replaces any stale cache-first worker and cleans old caches on activate Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -192,8 +192,23 @@ boot();
|
||||
|
||||
// ── Service Worker registration (PWA) ─────────────────────
|
||||
if ('serviceWorker' in navigator) {
|
||||
// Auto-update: when a new service worker takes control, reload once so the
|
||||
// page runs the fresh assets. Guarded so it never loops and never fires on
|
||||
// the very first install (when there was no previous controller).
|
||||
let refreshing = false;
|
||||
const hadController = !!navigator.serviceWorker.controller;
|
||||
navigator.serviceWorker.addEventListener('controllerchange', () => {
|
||||
if (refreshing || !hadController) return;
|
||||
refreshing = true;
|
||||
window.location.reload();
|
||||
});
|
||||
|
||||
window.addEventListener('load', () => {
|
||||
navigator.serviceWorker.register('/sw.js', { scope: '/' }).catch(err => {
|
||||
navigator.serviceWorker.register('/sw.js', { scope: '/' }).then(reg => {
|
||||
// Check for a new SW now and hourly, so long-open tabs pick up releases.
|
||||
reg.update();
|
||||
setInterval(() => reg.update(), 60 * 60 * 1000);
|
||||
}).catch(err => {
|
||||
console.warn('SW registration failed:', err);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user