Move registerSW out
parent
57bd37ef2f
commit
0b918464c1
|
@ -58,6 +58,7 @@ export const darkTheme = {
|
|||
MuiPaper: {
|
||||
styleOverrides: {
|
||||
root: {
|
||||
// for the sidebar on narrow (xs) screens
|
||||
backgroundImage: "none",
|
||||
},
|
||||
},
|
||||
|
|
|
@ -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 />);
|
||||
|
|
|
@ -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;
|
|
@ -16,6 +16,7 @@ export default defineConfig(({ mode }) => ({
|
|||
react(),
|
||||
VitePWA({
|
||||
registerType: "autoUpdate",
|
||||
// see registerSW.js imported by index.jsx
|
||||
injectRegister: null,
|
||||
strategies: "injectManifest",
|
||||
devOptions: {
|
||||
|
|
Loading…
Reference in New Issue