aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/components/ConnectivityChecker.vue
blob: c58bfa7edf0648926b5db23a024bbf160477358a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<template>
  <div v-if="offline" class="offline-message">
    <i class="far fa-dizzy"></i>
    <h1>
      You're offline friend.
      <span @click="checkOffline"> <i class="fas fa-redo-alt"></i></span>
    </h1>
  </div>
</template>

<script>
export default {
  name: "ConnectivityChecker",
  data: function () {
    return {
      offline: false,
    };
  },
  created: function () {
    if (/t=\d+/.test(window.location.href)) {
      window.history.replaceState({}, document.title, window.location.pathname);
    }
    let that = this;
    this.checkOffline();

    document.addEventListener(
      "visibilitychange",
      function () {
        if (document.visibilityState == "visible") {
          that.checkOffline();
        }
      },
      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;
      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) {
          // 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;
        })
        .finally(function () {
          that.$emit("network-status-update", that.offline);
        });
    },
  },
};
</script>