Compare commits

...

2 Commits

Author SHA1 Message Date
elpatron 646d316a36 chore: release v0.1.0.5 2026-05-29 17:45:27 +02:00
elpatron 593d1aea20 fix: Memory-Leak bei PWA-Update-Checks durch Cleanup beheben.
Entfernt Listener und Intervalle beim erneuten SW-Register und beim Unmount des Hooks.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-29 17:44:31 +02:00
2 changed files with 25 additions and 8 deletions
+1 -1
View File
@@ -1 +1 @@
0.1.0.5
0.1.0.6
+24 -7
View File
@@ -1,34 +1,51 @@
import { useEffect, useRef } from 'react'
import { useRegisterSW } from 'virtual:pwa-register/react'
const UPDATE_CHECK_INTERVAL_MS = 60 * 60 * 1000
function scheduleUpdateChecks(registration: ServiceWorkerRegistration) {
function scheduleUpdateChecks(registration: ServiceWorkerRegistration): () => void {
const checkForUpdate = () => {
registration.update().catch(() => {})
}
document.addEventListener('visibilitychange', () => {
const onVisibilityChange = () => {
if (document.visibilityState === 'visible') {
checkForUpdate()
}
})
}
window.setInterval(checkForUpdate, UPDATE_CHECK_INTERVAL_MS)
document.addEventListener('visibilitychange', onVisibilityChange)
const intervalId = window.setInterval(checkForUpdate, UPDATE_CHECK_INTERVAL_MS)
return () => {
document.removeEventListener('visibilitychange', onVisibilityChange)
window.clearInterval(intervalId)
}
}
export function usePwaUpdate() {
const cleanupRef = useRef<(() => void) | null>(null)
const {
needRefresh: [needRefresh],
updateServiceWorker
} = useRegisterSW({
immediate: true,
onRegisteredSW(_swUrl: string, registration: ServiceWorkerRegistration | undefined) {
if (registration) {
scheduleUpdateChecks(registration)
}
if (!registration) return
cleanupRef.current?.()
cleanupRef.current = scheduleUpdateChecks(registration)
}
})
useEffect(() => {
return () => {
cleanupRef.current?.()
cleanupRef.current = null
}
}, [])
const updateApp = async () => {
await updateServiceWorker(true)
}