瀏覽代碼

Move registerSW out

nimbleghost 2 年之前
父節點
當前提交
0b918464c1
共有 4 個文件被更改,包括 35 次插入27 次删除
  1. 1 0
      web/src/components/theme.js
  2. 2 27
      web/src/index.jsx
  3. 31 0
      web/src/registerSW.js
  4. 1 0
      web/vite.config.js

+ 1 - 0
web/src/components/theme.js

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

+ 2 - 27
web/src/index.jsx

@@ -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 - 0
web/src/registerSW.js

@@ -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;

+ 1 - 0
web/vite.config.js

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