Support live reload behind HTTPS reverse proxy #4373
Replies: 2 comments 2 replies
-
Hello, After having encountered the same problem, I took inspiration from @kiliman on issue #2859 because it is not possible to force the
So, to get the websocket to work in Step 1
2nd step
Step 3Edit the
import {
Links,
Meta,
Outlet,
Scripts,
ScrollRestoration,
} from "@remix-run/react";
import LiveReload from '~/components/LiveReload'
export const meta = () => ({
charset: "utf-8",
title: "New Remix App",
viewport: "width=device-width,initial-scale=1",
});
export default function App() {
return (
<html lang="en">
<head>
<Meta />
<Links />
</head>
<body>
<Outlet />
<ScrollRestoration />
<Scripts />
<LiveReload />
</body>
</html>
);
} Step 4Add these lines of code in the file export default function LiveReload({
port = Number(process.env.REMIX_DEV_SERVER_WS_PORT || 8002),
nonce = undefined
}) {
let js = String.raw;
return (
<script
nonce={nonce}
suppressHydrationWarning
dangerouslySetInnerHTML = {{
__html: js`
function remixLiveReloadConnect(config) {
let protocol = location.protocol === "https:" ? "wss:" : "ws:";
let host = location.hostname;
/**
* If the protocole used is HTTPS, WSS use port 443 or
* if the protocole used is HTTP, WS will use the value of devServerPort
**/
let port = protocol === "wss:" ? "443" : ${String(port)};
let socketPath = protocol + "//" + host + ":" + port + "/socket";
let ws = new WebSocket(socketPath);
ws.onmessage = (message) => {
let event = JSON.parse(message.data);
if (event.type === "LOG") {
console.log(event.message);
}
if (event.type === "RELOAD") {
console.log("💿 Reloading window ...");
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();
`,
}}
/>
);
}; Step 5Configure the reverse proxy. In my case I use Nginx.
Step 6
Enjoy ! Thank you. |
Beta Was this translation helpful? Give feedback.
-
If you're using Caddy instead of nginx, you simply need:
Where 'snipbot' is just the name of my local service. The app and dev server are working fine on: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm currently building a small project using auth0 as an identity provider. For auth0 to work locally, the dev server must be served as
HTTPS
(source.As recommended by auth0, I set a caddy server to act as a reverse proxy:
# reverse proxy https://localhost to http://localhost:3000 caddy reverse-proxy --to :3000
This setup breaks the live reload functionality from remix. Since the website is served as HTTPS, the
<LiveReload />
component tries to connect using thewss
protocol. The remix dev server is listening with thews
protocol. Here is the relevant sources from the remix source code:What would you think about adding an environment variable
REMIX_DEV_SERVER_WS_PROTOCOL
(same convention asREMIX_DEV_SERVER_WS_PORT
) to force the protocol tows
?The use-case might be a bit weird but it would help me a lot to solve it in a elegant manner.
Thanks a lot!
edit: if the idea is accepted, I can do the PR implementing this
Beta Was this translation helpful? Give feedback.
All reactions