mirror of
https://github.com/discourse/discourse.git
synced 2025-01-18 10:52:45 +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);
|
||||
|
||||
window.addEventListener("offline", () => {
|
||||
this.handleConnectivityChangeEvent(false);
|
||||
this.setConnectivity(false);
|
||||
});
|
||||
|
||||
window.addEventListener("online", () => {
|
||||
this.handleConnectivityChangeEvent(true);
|
||||
});
|
||||
window.addEventListener(
|
||||
"online",
|
||||
this.pingServerAndSetConnectivity.bind(this)
|
||||
);
|
||||
|
||||
window.addEventListener("visibilitychange", this.onFocus.bind(this));
|
||||
}
|
||||
|
||||
handleConnectivityChangeEvent(connected) {
|
||||
if (connected) {
|
||||
// Make a super cheap request to the server. If we get a response, we are connected!
|
||||
return ajax("/srv/status", { dataType: "text" })
|
||||
.then((response) => {
|
||||
this.setConnectivity(response === "ok");
|
||||
})
|
||||
.catch(() => {
|
||||
this.setConnectivity(false);
|
||||
});
|
||||
} else {
|
||||
this.setConnectivity(false);
|
||||
onFocus() {
|
||||
if (!this.connected && document.visibilityState === "visible") {
|
||||
this.pingServerAndSetConnectivity();
|
||||
}
|
||||
}
|
||||
|
||||
async pingServerAndSetConnectivity() {
|
||||
let response = await ajax("/srv/status", { dataType: "text" }).catch(() => {
|
||||
this.setConnectivity(false);
|
||||
});
|
||||
|
||||
this.setConnectivity(response === "ok");
|
||||
}
|
||||
|
||||
setConnectivity(connected) {
|
||||
this.connected = connected;
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ workbox.setConfig({
|
|||
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 inlineReplyIcon = "<%= UrlHelper.absolute("/images/push-notifications/inline_reply.png") %>";
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user