Move registerSW out

This commit is contained in:
nimbleghost 2023-06-30 08:59:20 +02:00
parent 57bd37ef2f
commit 0b918464c1
4 changed files with 35 additions and 27 deletions

View File

@ -58,6 +58,7 @@ export const darkTheme = {
MuiPaper: {
styleOverrides: {
root: {
// for the sidebar on narrow (xs) screens
backgroundImage: "none",
},
},

View File

@ -1,34 +1,9 @@
import * as React from "react";
import { createRoot } from "react-dom/client";
// eslint-disable-next-line import/no-unresolved
import { registerSW } from "virtual:pwa-register";
import App from "./components/App";
import registerSW from "./registerSW";
// fetch new sw every hour, i.e. update app every hour while running
const intervalMS = 60 * 60 * 1000;
// https://vite-pwa-org.netlify.app/guide/periodic-sw-updates.html
registerSW({
onRegisteredSW(swUrl, registration) {
if (!registration) {
return;
}
setInterval(async () => {
if (registration.installing || navigator?.onLine === false) return;
const resp = await fetch(swUrl, {
cache: "no-store",
headers: {
cache: "no-store",
"cache-control": "no-cache",
},
});
if (resp?.status === 200) await registration.update();
}, intervalMS);
},
});
registerSW();
const root = createRoot(document.querySelector("#root"));
root.render(<App />);

31
web/src/registerSW.js Normal file
View File

@ -0,0 +1,31 @@
// eslint-disable-next-line import/no-unresolved
import { registerSW as viteRegisterSW } from "virtual:pwa-register";
// fetch new sw every hour, i.e. update app every hour while running
const intervalMS = 60 * 60 * 1000;
// https://vite-pwa-org.netlify.app/guide/periodic-sw-updates.html
const registerSW = () =>
viteRegisterSW({
onRegisteredSW(swUrl, registration) {
if (!registration) {
return;
}
setInterval(async () => {
if (registration.installing || navigator?.onLine === false) return;
const resp = await fetch(swUrl, {
cache: "no-store",
headers: {
cache: "no-store",
"cache-control": "no-cache",
},
});
if (resp?.status === 200) await registration.update();
}, intervalMS);
},
});
export default registerSW;

View File

@ -16,6 +16,7 @@ export default defineConfig(({ mode }) => ({
react(),
VitePWA({
registerType: "autoUpdate",
// see registerSW.js imported by index.jsx
injectRegister: null,
strategies: "injectManifest",
devOptions: {