Skip to content

Commit

Permalink
feat(remix-react): add WebSocket reconnect to LiveReload (#859)
Browse files Browse the repository at this point in the history
Co-authored-by: Logan McAnsh <logan@mcan.sh>
  • Loading branch information
garand and mcansh committed Jul 11, 2022
1 parent 7a4e17f commit 2d46b91
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/fast-yaks-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"remix": patch
"@remix-run/react": patch
---

feat(remix-react): add WebSocket reconnect to `LiveReload`
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,4 @@
- zachdtaylor
- zainfathoni
- zhe
- garand
22 changes: 19 additions & 3 deletions packages/remix-react/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1491,7 +1491,7 @@ export const LiveReload =
suppressHydrationWarning
dangerouslySetInnerHTML={{
__html: js`
(() => {
function remixLiveReloadConnect(config) {
let protocol = location.protocol === "https:" ? "wss:" : "ws:";
let host = location.hostname;
let socketPath = protocol + "//" + host + ":" + ${String(
Expand All @@ -1509,13 +1509,29 @@ export const LiveReload =
window.location.reload();
}
};
ws.onopen = () => {
if (config && typeof config.onOpen === "function") {
config.onOpen();
}
};
ws.onclose = (error) => {
console.log("Remix dev asset server web socket closed. Reconnecting...");
setTimeout(
() =>
remixLiveReloadConnect({
onOpen: () => window.location.reload(),
}),
1000
);
};
ws.onerror = (error) => {
console.log("Remix dev asset server web socket error:");
console.error(error);
};
})();
}
remixLiveReloadConnect();
`,
}}
/>
);
};
};

0 comments on commit 2d46b91

Please sign in to comment.