feat: PWA site scheduled update detection, just for testing.

This commit is contained in:
Ou 2024-11-22 02:00:45 +08:00
parent f98d78b4c2
commit 68f9ca5469

View File

@ -2,11 +2,33 @@ import { useEffect } from "react"
import { useRegisterSW } from "virtual:pwa-register/react" import { useRegisterSW } from "virtual:pwa-register/react"
import { useToast } from "./useToast" import { useToast } from "./useToast"
const intervalMS = 60 * 60 * 1000
export function usePWA() { export function usePWA() {
const { const {
needRefresh: [needRefresh, setNeedRefresh], needRefresh: [needRefresh, setNeedRefresh],
updateServiceWorker, 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() const toaster = useToast()
useEffect(() => { useEffect(() => {