mirror of
https://github.com/ourongxing/newsnow.git
synced 2025-01-19 03:09:14 +08:00
feat: PWA site scheduled update detection, just for testing.
This commit is contained in:
parent
f98d78b4c2
commit
68f9ca5469
@ -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(() => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user