Selaa lähdekoodia

优化new service worker 检测

Michael Wang 5 vuotta sitten
vanhempi
commit
710109b78e
2 muutettua tiedostoa jossa 18 lisäystä ja 7 poistoa
  1. 13 5
      src/features/Login/Login.vue
  2. 5 2
      src/registerServiceWorker.js

+ 13 - 5
src/features/Login/Login.vue

@@ -186,11 +186,18 @@ export default {
     // fetch("/models/tiny_face_detector_model-shard1");
     // fetch("/models/face_landmark_68_model-shard1");
 
-    this.checkNewVersionInterval = setInterval(() => {
-      if (window.__newSWAvailable) {
+    // this.checkNewVersionInterval = setInterval(() => {
+    //   if (window.__newSWAvailable) {
+    //     this.newVersionAvailable = true;
+    //   }
+    // }, 1000);
+    this.checkNewVersionListener = document.addEventListener(
+      "__newSWAvailable",
+      () => {
         this.newVersionAvailable = true;
-      }
-    }, 1000);
+      },
+      { once: true }
+    );
     if (this.isEPCC) {
       const redirectUrl = new URLSearchParams(location.search).get(
         "redirectUrl"
@@ -271,7 +278,8 @@ export default {
   },
   beforeDestroy() {
     clearTimeout(this.loginTimeout);
-    clearInterval(this.checkNewVersionInterval);
+    // clearInterval(this.checkNewVersionInterval);
+    document.removeEventListener(this.checkNewVersionListener);
   },
   methods: {
     ...mapMutations(["updateUser", "updateTimeDifference", "updateQECSConfig"]),

+ 5 - 2
src/registerServiceWorker.js

@@ -19,9 +19,12 @@ if (process.env.NODE_ENV === "production") {
     updatefound() {
       console.log("New content is downloading.");
     },
-    updated() {
+    updated(registration) {
       console.log("New content is available; please refresh.");
-      window.__newSWAvailable = true;
+      // window.__newSWAvailable = true;
+      document.dispatchEvent(
+        new CustomEvent("__newSWAvailable", { detail: registration })
+      );
     },
     offline() {
       console.log(