mirror of
https://github.com/wu736139669/hapi.git
synced 2026-08-05 06:24:37 +00:00
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:
+7
-9
@@ -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) {
|
||||
|
||||
@@ -44,6 +44,8 @@ export default defineConfig({
|
||||
]
|
||||
},
|
||||
workbox: {
|
||||
skipWaiting: true,
|
||||
clientsClaim: true,
|
||||
globPatterns: ['**/*.{js,css,html,ico,png,svg,woff,woff2}'],
|
||||
runtimeCaching: [
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user