PWA periodic register

This commit is contained in:
mrjvs 2023-12-06 19:24:43 +01:00
parent 7b896c201b
commit bcb2e02b85
2 changed files with 32 additions and 4 deletions

View File

@ -1,3 +1,4 @@
import "@/setup/pwa";
import "core-js/stable"; import "core-js/stable";
import "./stores/__old/imports"; import "./stores/__old/imports";
import "@/setup/ga"; import "@/setup/ga";
@ -10,7 +11,6 @@ import { HelmetProvider } from "react-helmet-async";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { BrowserRouter, HashRouter } from "react-router-dom"; import { BrowserRouter, HashRouter } from "react-router-dom";
import { useAsync } from "react-use"; import { useAsync } from "react-use";
import { registerSW } from "virtual:pwa-register";
import { Button } from "@/components/buttons/Button"; import { Button } from "@/components/buttons/Button";
import { Icon, Icons } from "@/components/Icon"; import { Icon, Icons } from "@/components/Icon";
@ -40,9 +40,6 @@ if (key) {
(window as any).initMW(conf().PROXY_URLS, key); (window as any).initMW(conf().PROXY_URLS, key);
} }
initializeChromecast(); initializeChromecast();
registerSW({
immediate: true,
});
function LoadingScreen(props: { type: "user" | "lazy" }) { function LoadingScreen(props: { type: "user" | "lazy" }) {
const mapping = { const mapping = {

31
src/setup/pwa.ts Normal file
View File

@ -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);
},
});