diff --git a/src/index.tsx b/src/index.tsx index 79daec72..ac372fee 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,3 +1,4 @@ +import "@/setup/pwa"; import "core-js/stable"; import "./stores/__old/imports"; import "@/setup/ga"; @@ -10,7 +11,6 @@ import { HelmetProvider } from "react-helmet-async"; import { useTranslation } from "react-i18next"; import { BrowserRouter, HashRouter } from "react-router-dom"; import { useAsync } from "react-use"; -import { registerSW } from "virtual:pwa-register"; import { Button } from "@/components/buttons/Button"; import { Icon, Icons } from "@/components/Icon"; @@ -40,9 +40,6 @@ if (key) { (window as any).initMW(conf().PROXY_URLS, key); } initializeChromecast(); -registerSW({ - immediate: true, -}); function LoadingScreen(props: { type: "user" | "lazy" }) { const mapping = { diff --git a/src/setup/pwa.ts b/src/setup/pwa.ts new file mode 100644 index 00000000..44b4b776 --- /dev/null +++ b/src/setup/pwa.ts @@ -0,0 +1,31 @@ +/* eslint-disable no-console */ +import { registerSW } from "virtual:pwa-register"; + +const intervalMS = 60 * 60 * 1000; + +registerSW({ + immediate: true, + onRegisteredSW(swUrl, r) { + console.log(`SW registered at: ${swUrl}`); + if (!r) return; + setInterval(async () => { + console.log(`attempting SW update`); + 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) { + console.log(`SW update successfully triggered`); + await r.update(); + } + }, intervalMS); + }, +});