diff options
author | Bastien Wirtz <bastien.wirtz@gmail.com> | 2022-06-04 09:07:15 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-04 09:07:15 +0200 |
commit | 2b48d1c057bf24776fb0538c7db0a0b156e667c5 (patch) | |
tree | 47a6b64226c3d3707b285a6de3838a2d7270b59f /src/components/ConnectivityChecker.vue | |
parent | 95d73348e5dd8fd0fbb1b6747c58b346ecf772ad (diff) | |
parent | 95249e11255beb8b85f904c54429b353fca40b9e (diff) | |
download | homer-2b48d1c057bf24776fb0538c7db0a0b156e667c5.tar.gz homer-2b48d1c057bf24776fb0538c7db0a0b156e667c5.tar.zst homer-2b48d1c057bf24776fb0538c7db0a0b156e667c5.zip |
Merge pull request #448 from bemble/main
feat(pwa): enhance connectivity checks
Diffstat (limited to 'src/components/ConnectivityChecker.vue')
-rw-r--r-- | src/components/ConnectivityChecker.vue | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/src/components/ConnectivityChecker.vue b/src/components/ConnectivityChecker.vue index 02cbd7f..f2be652 100644 --- a/src/components/ConnectivityChecker.vue +++ b/src/components/ConnectivityChecker.vue | |||
@@ -17,6 +17,9 @@ export default { | |||
17 | }; | 17 | }; |
18 | }, | 18 | }, |
19 | created: function () { | 19 | created: function () { |
20 | if (/t=\d+/.test(window.location.href)) { | ||
21 | window.history.replaceState({}, document.title, window.location.pathname); | ||
22 | } | ||
20 | let that = this; | 23 | let that = this; |
21 | this.checkOffline(); | 24 | this.checkOffline(); |
22 | 25 | ||
@@ -29,15 +32,41 @@ export default { | |||
29 | }, | 32 | }, |
30 | false | 33 | false |
31 | ); | 34 | ); |
35 | window.addEventListener( | ||
36 | "online", | ||
37 | function () { | ||
38 | that.checkOffline(); | ||
39 | }, | ||
40 | false | ||
41 | ); | ||
42 | window.addEventListener( | ||
43 | "offline", | ||
44 | function () { | ||
45 | this.offline = true; | ||
46 | }, | ||
47 | false | ||
48 | ); | ||
32 | }, | 49 | }, |
33 | methods: { | 50 | methods: { |
34 | checkOffline: function () { | 51 | checkOffline: function () { |
52 | if (!navigator.onLine) { | ||
53 | this.offline = true; | ||
54 | return; | ||
55 | } | ||
56 | |||
57 | // extra check to make sure we're not offline | ||
35 | let that = this; | 58 | let that = this; |
36 | return fetch(window.location.href + "?alive", { | 59 | const aliveCheckUrl = window.location.href + "?t="+(new Date().valueOf()); |
60 | return fetch(aliveCheckUrl, { | ||
37 | method: "HEAD", | 61 | method: "HEAD", |
38 | cache: "no-store", | 62 | cache: "no-store", |
63 | redirect: "manual" | ||
39 | }) | 64 | }) |
40 | .then(function (response) { | 65 | .then(function (response) { |
66 | // opaqueredirect means request has been redirected, to auth provider probably | ||
67 | if ((response.type === "opaqueredirect" && !response.ok) || [401, 403].indexOf(response.status) != -1) { | ||
68 | window.location.href = aliveCheckUrl; | ||
69 | } | ||
41 | that.offline = !response.ok; | 70 | that.offline = !response.ok; |
42 | }) | 71 | }) |
43 | .catch(function () { | 72 | .catch(function () { |