diff --git a/src/hooks/usePWA.ts b/src/hooks/usePWA.ts index 6643b8c..7a7752b 100644 --- a/src/hooks/usePWA.ts +++ b/src/hooks/usePWA.ts @@ -2,11 +2,33 @@ import { useEffect } from "react" import { useRegisterSW } from "virtual:pwa-register/react" import { useToast } from "./useToast" +const intervalMS = 60 * 60 * 1000 export function usePWA() { const { needRefresh: [needRefresh, setNeedRefresh], updateServiceWorker, - } = useRegisterSW() + } + = useRegisterSW({ + onRegisteredSW(swUrl, r) { + if (r) { + setInterval(async () => { + if (r.installing || !navigator) return + + if ("connection" in navigator && !navigator.onLine) return + + const resp = await fetch(swUrl, { + cache: "no-store", + headers: { + "cache": "no-store", + "cache-control": "no-cache", + }, + }) + + if (resp?.status === 200) await r.update() + }, intervalMS) + } + }, + }) const toaster = useToast() useEffect(() => {