fix: ensure previous ws connection is closed in dev (#2154)
This commit is contained in:
@@ -46,38 +46,53 @@ function reconnect() {
|
|||||||
}, backoff[Math.min(backoffIdx, backoff.length - 1)]);
|
}, backoff[Math.min(backoffIdx, backoff.length - 1)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function onOpenWs() {
|
||||||
|
backoffIdx = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function onCloseWs() {
|
||||||
|
disconnect();
|
||||||
|
reconnect();
|
||||||
|
}
|
||||||
|
|
||||||
function connect() {
|
function connect() {
|
||||||
const url = new URL("/_frsh/alive", location.origin.replace("http", "ws"));
|
const url = new URL("/_frsh/alive", location.origin.replace("http", "ws"));
|
||||||
ws = new WebSocket(
|
ws = new WebSocket(
|
||||||
url,
|
url,
|
||||||
);
|
);
|
||||||
|
|
||||||
ws.addEventListener("open", () => {
|
ws.addEventListener("open", onOpenWs);
|
||||||
backoffIdx = 0;
|
ws.addEventListener("close", onCloseWs);
|
||||||
console.log(
|
|
||||||
`%c Fresh %c Connected to development server.`,
|
|
||||||
"background-color: #86efac; color: black",
|
|
||||||
"color: inherit",
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
ws.addEventListener("close", () => {
|
|
||||||
reconnect();
|
|
||||||
});
|
|
||||||
|
|
||||||
ws.addEventListener("message", handleMessage);
|
ws.addEventListener("message", handleMessage);
|
||||||
ws.addEventListener("error", handleError);
|
ws.addEventListener("error", handleError);
|
||||||
}
|
}
|
||||||
|
|
||||||
connect();
|
connect();
|
||||||
|
|
||||||
|
function disconnect() {
|
||||||
|
ws.removeEventListener("open", onOpenWs);
|
||||||
|
ws.removeEventListener("close", onCloseWs);
|
||||||
|
ws.removeEventListener("message", handleMessage);
|
||||||
|
ws.removeEventListener("error", handleError);
|
||||||
|
ws.close();
|
||||||
|
}
|
||||||
|
|
||||||
function handleMessage(e: MessageEvent) {
|
function handleMessage(e: MessageEvent) {
|
||||||
const data = JSON.parse(e.data);
|
const data = JSON.parse(e.data);
|
||||||
switch (data.type) {
|
switch (data.type) {
|
||||||
case "initial-state": {
|
case "initial-state": {
|
||||||
|
if (revision === 0) {
|
||||||
|
console.log(
|
||||||
|
`%c Fresh %c Connected to development server.`,
|
||||||
|
"background-color: #86efac; color: black",
|
||||||
|
"color: inherit",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (revision === 0) {
|
if (revision === 0) {
|
||||||
revision = data.revision;
|
revision = data.revision;
|
||||||
} else if (revision < data.revision) {
|
} else if (revision < data.revision) {
|
||||||
|
disconnect();
|
||||||
// Needs reload
|
// Needs reload
|
||||||
location.reload();
|
location.reload();
|
||||||
}
|
}
|
||||||
@@ -101,3 +116,13 @@ addEventListener("message", (ev) => {
|
|||||||
|
|
||||||
document.querySelector("#fresh-error-overlay")?.remove();
|
document.querySelector("#fresh-error-overlay")?.remove();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Disconnect when the tab becomes inactive and re-connect when it
|
||||||
|
// becomes active again
|
||||||
|
addEventListener("visibilitychange", () => {
|
||||||
|
if (document.hidden) {
|
||||||
|
disconnect();
|
||||||
|
} else {
|
||||||
|
connect();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user