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) ─────────────────────
|
// ── Service Worker registration (PWA) ─────────────────────
|
||||||
if ('serviceWorker' in navigator) {
|
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', () => {
|
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);
|
console.warn('SW registration failed:', err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
// Increment APP_VERSION with every code change
|
// Increment APP_VERSION with every code change
|
||||||
export const APP_VERSION = 'v65';
|
export const APP_VERSION = 'v66';
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
// the entry HTML / version files). New releases take effect on the next
|
// the entry HTML / version files). New releases take effect on the next
|
||||||
// reload, no manual SW unregister required.
|
// reload, no manual SW unregister required.
|
||||||
|
|
||||||
const CACHE_VERSION = 'calendarr-v23';
|
const CACHE_VERSION = 'calendarr-v24';
|
||||||
const OFFLINE_SHELL = ['/', '/index.html'];
|
const OFFLINE_SHELL = ['/', '/index.html'];
|
||||||
|
|
||||||
self.addEventListener('install', event => {
|
self.addEventListener('install', event => {
|
||||||
|
|||||||
Reference in New Issue
Block a user