FIX: Attempt to reconnect to server when browser visibilty changes (#22259)

This commit is contained in:
Mark VanLandingham 2023-06-23 11:32:34 -05:00 committed by GitHub
parent a984a807fd
commit 36b9572bb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 17 deletions

View File

@ -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;

View File

@ -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") %>";