Skip to content

Commit db88c96

Browse files
committed
fix: Firefox LiveReload
Firefox infinitely reloads the page as long as `<LiveReload>` is rendering. Closes #4692
1 parent 3d81516 commit db88c96

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

.changeset/thin-oranges-boil.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
"@remix-run/react": patch
3+
---
4+
5+
Fixed a problem with live reload and firefox infinitely reloading the page
6+
7+
The problem is:
8+
9+
1. Firefox is calling `ws.onclose` immediately upon connecting (?!)
10+
2. Then we’re trying to reconnect, and upon reconnection, we reload the page.
11+
3. Firefox then calls `ws.onclose` again after reconnecting and the loop starts over
12+
13+
This fix is to check `event.code === 1006` before actually trying to reconnect and the reload the page. 1006 means the connection was closed abnormally (https://www.rfc-editor.org/rfc/rfc6455#section-7.4.1). In our case, that means the server was shut down in local dev and then the socket can reconnect again when the server is back up.
14+
15+
It’s unclear to me why Firefox is calling `onclose` immediately upon connecting to the web socket, but it does.

packages/remix-react/components.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,7 +1646,6 @@ export const LiveReload =
16461646
let socketPath = protocol + "//" + host + ":" + ${String(
16471647
port
16481648
)} + "/socket";
1649-
16501649
let ws = new WebSocket(socketPath);
16511650
ws.onmessage = (message) => {
16521651
let event = JSON.parse(message.data);
@@ -1663,15 +1662,17 @@ export const LiveReload =
16631662
config.onOpen();
16641663
}
16651664
};
1666-
ws.onclose = (error) => {
1665+
ws.onclose = (event) => {
16671666
console.log("Remix dev asset server web socket closed. Reconnecting...");
1668-
setTimeout(
1669-
() =>
1670-
remixLiveReloadConnect({
1671-
onOpen: () => window.location.reload(),
1672-
}),
1673-
1000
1674-
);
1667+
if (event.code === 1006) {
1668+
setTimeout(
1669+
() =>
1670+
remixLiveReloadConnect({
1671+
onOpen: () => window.location.reload(),
1672+
}),
1673+
1000
1674+
);
1675+
}
16751676
};
16761677
ws.onerror = (error) => {
16771678
console.log("Remix dev asset server web socket error:");

0 commit comments

Comments
 (0)