feat: PWA-Updates erkennen und Nutzer zum Reload auffordern.
Wechselt auf prompt-Modus mit Update-Banner, periodischer SW-Prüfung und no-cache-Headern für Service Worker und index.html. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import { useRegisterSW } from 'virtual:pwa-register/react'
|
||||
|
||||
const UPDATE_CHECK_INTERVAL_MS = 60 * 60 * 1000
|
||||
|
||||
function scheduleUpdateChecks(registration: ServiceWorkerRegistration) {
|
||||
const checkForUpdate = () => {
|
||||
registration.update().catch(() => {})
|
||||
}
|
||||
|
||||
document.addEventListener('visibilitychange', () => {
|
||||
if (document.visibilityState === 'visible') {
|
||||
checkForUpdate()
|
||||
}
|
||||
})
|
||||
|
||||
window.setInterval(checkForUpdate, UPDATE_CHECK_INTERVAL_MS)
|
||||
}
|
||||
|
||||
export function usePwaUpdate() {
|
||||
const {
|
||||
needRefresh: [needRefresh],
|
||||
updateServiceWorker
|
||||
} = useRegisterSW({
|
||||
immediate: true,
|
||||
onRegisteredSW(_swUrl: string, registration: ServiceWorkerRegistration | undefined) {
|
||||
if (registration) {
|
||||
scheduleUpdateChecks(registration)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const updateApp = async () => {
|
||||
await updateServiceWorker(true)
|
||||
}
|
||||
|
||||
return { needRefresh, updateApp }
|
||||
}
|
||||
Reference in New Issue
Block a user