mirror of
https://github.com/discourse/discourse.git
synced 2025-01-18 13:52:50 +08:00
FIX: Attempt to reconnect to server when browser visibilty changes (#22259)
This commit is contained in:
parent
a984a807fd
commit
36b9572bb1
|
@ -13,29 +13,31 @@ export default class NetworkConnectivity extends Service {
|
||||||
this.setConnectivity(navigator.onLine);
|
this.setConnectivity(navigator.onLine);
|
||||||
|
|
||||||
window.addEventListener("offline", () => {
|
window.addEventListener("offline", () => {
|
||||||
this.handleConnectivityChangeEvent(false);
|
this.setConnectivity(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
window.addEventListener("online", () => {
|
window.addEventListener(
|
||||||
this.handleConnectivityChangeEvent(true);
|
"online",
|
||||||
});
|
this.pingServerAndSetConnectivity.bind(this)
|
||||||
|
);
|
||||||
|
|
||||||
|
window.addEventListener("visibilitychange", this.onFocus.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
handleConnectivityChangeEvent(connected) {
|
onFocus() {
|
||||||
if (connected) {
|
if (!this.connected && document.visibilityState === "visible") {
|
||||||
// Make a super cheap request to the server. If we get a response, we are connected!
|
this.pingServerAndSetConnectivity();
|
||||||
return ajax("/srv/status", { dataType: "text" })
|
|
||||||
.then((response) => {
|
|
||||||
this.setConnectivity(response === "ok");
|
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
this.setConnectivity(false);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
this.setConnectivity(false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async pingServerAndSetConnectivity() {
|
||||||
|
let response = await ajax("/srv/status", { dataType: "text" }).catch(() => {
|
||||||
|
this.setConnectivity(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
this.setConnectivity(response === "ok");
|
||||||
|
}
|
||||||
|
|
||||||
setConnectivity(connected) {
|
setConnectivity(connected) {
|
||||||
this.connected = connected;
|
this.connected = connected;
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ workbox.setConfig({
|
||||||
debug: false
|
debug: false
|
||||||
});
|
});
|
||||||
|
|
||||||
var authUrls = ["auth", "session/sso_login", "session/sso"].map(path => `<%= Discourse.base_path %>/${path}`);
|
var authUrls = ["auth", "session/sso_login", "session/sso", "srv/status"].map(path => `<%= Discourse.base_path %>/${path}`);
|
||||||
var chatRegex = /\/chat\/channel\/(\d+)\//;
|
var chatRegex = /\/chat\/channel\/(\d+)\//;
|
||||||
var inlineReplyIcon = "<%= UrlHelper.absolute("/images/push-notifications/inline_reply.png") %>";
|
var inlineReplyIcon = "<%= UrlHelper.absolute("/images/push-notifications/inline_reply.png") %>";
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user