aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/components/ConnectivityChecker.vue
blob: 7302e1f56a5ea91504c62e506736b3337a3aa752 (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
<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 () {
    let that = this;
    this.checkOffline();

    document.addEventListener(
      "visibilitychange",
      function () {
        if (document.visibilityState == "visible") {
          that.checkOffline();
        }
      },
      false
    );
  },
  methods: {
    checkOffline: function () {
      let that = this;
      return fetch(window.location.href + "?alive", {
        method: "HEAD",
        cache: "no-store",
      })
        .then(function (response) {
          if (response.status >= 200 && response.status < 300) {
            that.offline = false;
          } else {
            that.offline = true;
          }
        })
        .catch(function () {
          that.offline = true;
        })
        .finally(function () {
          that.$emit("network-status-update", that.offline);
        });
    },
  },
};
</script>