]> git.immae.eu Git - github/bastienwirtz/homer.git/blobdiff - src/components/ConnectivityChecker.vue
What's up docker custom service (#444)
[github/bastienwirtz/homer.git] / src / components / ConnectivityChecker.vue
index 7302e1f56a5ea91504c62e506736b3337a3aa752..5c7761815051bbc4f75a475d1ab87832c685b1e0 100644 (file)
@@ -17,6 +17,9 @@ export default {
     };
   },
   created: function () {
+    if (/t=\d+/.test(window.location.href)) {
+      window.history.replaceState({}, document.title, window.location.pathname);
+    }
     let that = this;
     this.checkOffline();
 
@@ -29,20 +32,46 @@ export default {
       },
       false
     );
+    window.addEventListener(
+      "online",
+      function () {
+        that.checkOffline();
+      },
+      false
+    );
+    window.addEventListener(
+      "offline",
+      function () {
+        this.offline = true;
+      },
+      false
+    );
   },
   methods: {
     checkOffline: function () {
+      if (!navigator.onLine) {
+        this.offline = true;
+        return;
+      }
+
+      // extra check to make sure we're not offline
       let that = this;
-      return fetch(window.location.href + "?alive", {
+      const urlPath = window.location.pathname.replace(/\/+$/, "");
+      const aliveCheckUrl = `${window.location.origin}${urlPath}/index.html?t=${new Date().valueOf()}`;
+      return fetch(aliveCheckUrl, {
         method: "HEAD",
         cache: "no-store",
+        redirect: "manual",
       })
         .then(function (response) {
-          if (response.status >= 200 && response.status < 300) {
-            that.offline = false;
-          } else {
-            that.offline = true;
+          // opaqueredirect means request has been redirected, to auth provider probably
+          if (
+            (response.type === "opaqueredirect" && !response.ok) ||
+            [401, 403].indexOf(response.status) != -1
+          ) {
+            window.location.href = aliveCheckUrl;
           }
+          that.offline = !response.ok;
         })
         .catch(function () {
           that.offline = true;