Skip to content

Commit

Permalink
feat: PWA site scheduled update detection, just for testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
ourongxing committed Nov 21, 2024
1 parent f98d78b commit 68f9ca5
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/hooks/usePWA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down

0 comments on commit 68f9ca5

Please sign in to comment.