feat: optimize PWA service worker update strategy for better caching

Improve service worker update handling to fix caching issues during development:
- Add skipWaiting and clientsClaim in Workbox config for immediate SW activation
- Remove user confirmation prompt and switch to silent automatic updates
- Replace hourly polling with visibility-based update checks via visibilitychange event

This resolves the issue where web updates would not take effect without manual cache clearing.
This commit is contained in:
weishu
2025-12-18 18:18:35 +08:00
parent 80b8d2e34f
commit 5856db6781
2 changed files with 9 additions and 9 deletions
+7 -9
View File
@@ -4,20 +4,18 @@ import { App } from './App'
import './index.css'
import { registerSW } from 'virtual:pwa-register'
const updateSW = registerSW({
onNeedRefresh() {
if (confirm('New version available! Reload to update?')) {
updateSW(true)
}
},
registerSW({
immediate: true,
onOfflineReady() {
console.log('App ready for offline use')
},
onRegistered(registration) {
if (registration) {
setInterval(() => {
registration.update()
}, 60 * 60 * 1000)
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') {
registration.update()
}
})
}
},
onRegisterError(error) {
+2
View File
@@ -44,6 +44,8 @@ export default defineConfig({
]
},
workbox: {
skipWaiting: true,
clientsClaim: true,
globPatterns: ['**/*.{js,css,html,ico,png,svg,woff,woff2}'],
runtimeCaching: [
{