diff --git a/activities.html b/activities.html index d227e52..b555a4d 100644 --- a/activities.html +++ b/activities.html @@ -3,6 +3,7 @@ + diff --git a/castle.html b/castle.html index fe3f567..cee9c6f 100644 --- a/castle.html +++ b/castle.html @@ -3,6 +3,7 @@ + diff --git a/chat.html b/chat.html index 56634bb..fb83dee 100644 --- a/chat.html +++ b/chat.html @@ -3,6 +3,7 @@ + diff --git a/forum.html b/forum.html index 13e5e8a..8646936 100644 --- a/forum.html +++ b/forum.html @@ -3,6 +3,7 @@ + diff --git a/index.html b/index.html index c1f84b2..76e7922 100644 --- a/index.html +++ b/index.html @@ -3,6 +3,7 @@ + diff --git a/market.html b/market.html index 52feccb..3fc32d5 100644 --- a/market.html +++ b/market.html @@ -3,6 +3,7 @@ + diff --git a/src/accounting-app/main.ts b/src/accounting-app/main.ts index 22efcbc..ed477b2 100644 --- a/src/accounting-app/main.ts +++ b/src/accounting-app/main.ts @@ -1,7 +1,10 @@ import { startApp } from './app' import { registerSW } from 'virtual:pwa-register' +import { cleanupStaleDevServiceWorkers } from '@/lib/dev-sw-cleanup' import 'vue-sonner/style.css' +cleanupStaleDevServiceWorkers() + // PWA service worker with periodic updates const intervalMS = 60 * 60 * 1000 // 1 hour registerSW({ diff --git a/src/activities-app/main.ts b/src/activities-app/main.ts index c9c8429..e3bb49d 100644 --- a/src/activities-app/main.ts +++ b/src/activities-app/main.ts @@ -1,7 +1,10 @@ import { startApp } from './app' import { registerSW } from 'virtual:pwa-register' +import { cleanupStaleDevServiceWorkers } from '@/lib/dev-sw-cleanup' import 'vue-sonner/style.css' +cleanupStaleDevServiceWorkers() + // PWA service worker with periodic updates const intervalMS = 60 * 60 * 1000 // 1 hour registerSW({ diff --git a/src/chat-app/main.ts b/src/chat-app/main.ts index 472cdf2..7493491 100644 --- a/src/chat-app/main.ts +++ b/src/chat-app/main.ts @@ -1,7 +1,10 @@ import { startApp } from './app' import { registerSW } from 'virtual:pwa-register' +import { cleanupStaleDevServiceWorkers } from '@/lib/dev-sw-cleanup' import 'vue-sonner/style.css' +cleanupStaleDevServiceWorkers() + const intervalMS = 60 * 60 * 1000 registerSW({ onRegistered(r) { diff --git a/src/forum-app/main.ts b/src/forum-app/main.ts index 900623b..8b7df15 100644 --- a/src/forum-app/main.ts +++ b/src/forum-app/main.ts @@ -1,7 +1,10 @@ import { startApp } from './app' import { registerSW } from 'virtual:pwa-register' +import { cleanupStaleDevServiceWorkers } from '@/lib/dev-sw-cleanup' import 'vue-sonner/style.css' +cleanupStaleDevServiceWorkers() + const intervalMS = 60 * 60 * 1000 registerSW({ onRegistered(r) { diff --git a/src/lib/dev-sw-cleanup.ts b/src/lib/dev-sw-cleanup.ts new file mode 100644 index 0000000..fd3de93 --- /dev/null +++ b/src/lib/dev-sw-cleanup.ts @@ -0,0 +1,42 @@ +/** + * Unregister any service worker that was registered on this origin during + * a previous dev session (when VitePWA's devOptions.enabled was true). + * + * Once devOptions.enabled was turned off, Vite stopped registering SWs in + * dev — but the browser keeps the previously-registered SWs alive across + * server restarts. They then intercept navigation and serve cached, often + * stale, bundles. This call clears them out at app boot. + * + * Production builds skip this entirely so the legitimate SW from + * `registerSW()` survives. + */ +export async function cleanupStaleDevServiceWorkers(): Promise { + if (!import.meta.env.DEV) return + if (!('serviceWorker' in navigator)) return + + try { + const regs = await navigator.serviceWorker.getRegistrations() + if (regs.length === 0) return + + console.warn( + `[dev-sw-cleanup] Unregistering ${regs.length} stale service worker(s) from a previous dev session.` + ) + await Promise.all(regs.map(r => r.unregister())) + + // Also clear any cache the dev SW left behind. + if ('caches' in window) { + const keys = await caches.keys() + await Promise.all(keys.map(k => caches.delete(k))) + } + + // Reload once so the next request hits the network instead of the + // about-to-be-removed SW. Guard with a sessionStorage flag so we don't + // loop on browsers that take an extra tick to release the controller. + if (!sessionStorage.getItem('dev-sw-cleanup-reloaded')) { + sessionStorage.setItem('dev-sw-cleanup-reloaded', '1') + window.location.reload() + } + } catch (err) { + console.warn('[dev-sw-cleanup] failed to unregister:', err) + } +} diff --git a/src/main.ts b/src/main.ts index ee1eff7..a12e2a8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,8 +1,12 @@ // New modular application entry point import { startApp } from './app' import { registerSW } from 'virtual:pwa-register' +import { cleanupStaleDevServiceWorkers } from '@/lib/dev-sw-cleanup' import 'vue-sonner/style.css' +// Clean up any leftover dev-mode service workers from a previous session +cleanupStaleDevServiceWorkers() + // Simple periodic service worker updates const intervalMS = 60 * 60 * 1000 // 1 hour registerSW({ diff --git a/src/market-app/main.ts b/src/market-app/main.ts index 58c349a..4965337 100644 --- a/src/market-app/main.ts +++ b/src/market-app/main.ts @@ -1,7 +1,10 @@ import { startApp } from './app' import { registerSW } from 'virtual:pwa-register' +import { cleanupStaleDevServiceWorkers } from '@/lib/dev-sw-cleanup' import 'vue-sonner/style.css' +cleanupStaleDevServiceWorkers() + const intervalMS = 60 * 60 * 1000 registerSW({ onRegistered(r) { diff --git a/src/tasks-app/main.ts b/src/tasks-app/main.ts index 7d31c16..25e84da 100644 --- a/src/tasks-app/main.ts +++ b/src/tasks-app/main.ts @@ -1,7 +1,10 @@ import { startApp } from './app' import { registerSW } from 'virtual:pwa-register' +import { cleanupStaleDevServiceWorkers } from '@/lib/dev-sw-cleanup' import 'vue-sonner/style.css' +cleanupStaleDevServiceWorkers() + const intervalMS = 60 * 60 * 1000 registerSW({ onRegistered(r) { diff --git a/src/wallet-app/main.ts b/src/wallet-app/main.ts index c7c8987..2039d3f 100644 --- a/src/wallet-app/main.ts +++ b/src/wallet-app/main.ts @@ -1,7 +1,10 @@ import { startApp } from './app' import { registerSW } from 'virtual:pwa-register' +import { cleanupStaleDevServiceWorkers } from '@/lib/dev-sw-cleanup' import 'vue-sonner/style.css' +cleanupStaleDevServiceWorkers() + const intervalMS = 60 * 60 * 1000 registerSW({ onRegistered(r) { diff --git a/tasks.html b/tasks.html index 7330599..6a5b49a 100644 --- a/tasks.html +++ b/tasks.html @@ -3,6 +3,7 @@ + diff --git a/wallet.html b/wallet.html index 81b2524..249f51d 100644 --- a/wallet.html +++ b/wallet.html @@ -3,6 +3,7 @@ +