remix icon indicating copy to clipboard operation
remix copied to clipboard

[Bug]: Live Reload not working.

Open drewkill32 opened this issue 3 years ago • 64 comments
trafficstars

Which Remix packages are impacted?

  • remix (Remix core)
  • @remix-run/react
  • @remix-run/serve

What version of Remix are you using?

1.1.3

What version of Node are you using? Minimum supported version is 14.

14.17.4

Steps to Reproduce

  1. starting from the Quickstart template.
  2. create a posts route.
  3. navigate to route
  4. the route does not show up

Every step along the way I have to restart the dev server to see the changes in the web. For example I added


export default function Posts() {
    const posts = useLoaderData();
    console.log('test3');
    return (
        <div>
            <h1>Posts</h1>
        </div>
    );
}

and the console still shows that it reloaded but still shows test2 (the value from when the dev server started)

Navigated to http://10.202.57.104:3000/posts index.tsx:18 test2 posts:23 💿 File changed: app\routes\posts\index.tsx posts:23 💿 Rebuilding... posts:23 💿 Rebuilt in 201ms posts:26 💿 Reloading window ... Navigated to http://10.202.57.104:3000/posts index.tsx:18 test2

I'm on Windows using Chrome(97.0.4692.71) and Edge(Version 97.0.1072.62), both failed

Expected Behavior

the hot reload should show the changes without having to restart

Actual Behavior

I have to stop the dev server and restart to see any changes

drewkill32 avatar Jan 21 '22 18:01 drewkill32

Check that the <LiveReload /> component is making it onto your page. If it's not, the first thing to check is what the value of NODE_ENV is when you run. If it's not "development", set it before you start the dev command.

jacob-ebey avatar Jan 21 '22 19:01 jacob-ebey

The component is on the page. I just double-checked the NODE_ENV by adding <p>{process.env.NODE_ENV}</p> to the component and restarted the server. It is in development

drewkill32 avatar Jan 21 '22 20:01 drewkill32

I still have not found a solution other than it the hot-reloading it working on my Linux machine but refuses to work on Windows.

drewkill32 avatar Jan 27 '22 13:01 drewkill32

I'm also using Windows and I realized hot-reloads only work when I make changes inside the app directory of my Remix project. I don't know if this is how it is supposed to work or it's a bug!

kurdi-dev avatar Feb 06 '22 11:02 kurdi-dev

Same issue on MacOS. Nothing makes it work for me, even if i remove the process.env.NODE_ENV check around <LiveReload/>

I have reproduced the issue on 1.1.3 1.1.2 and 1.1.1 so downgrading does not appear to help.

I am using arc sandbox - so this may be affecting it.

Its dropped the Remix DX from a 10/10 down to a 1/10. I've had to put my project on hold until this is fixed. :(

All my components are inside the app directory

dan-cooke avatar Feb 09 '22 15:02 dan-cooke

I'm also using Windows and I realized hot-reloads only work when I make changes inside the app directory of my Remix project. I don't know if this is how it is supposed to work or it's a bug!

Interesting observation. I was struggling with this and your point made me realize I currently have all my components in a components/ folder outside of my app/ directory. I just did a brief experiment and moving a component into the app/ directory seems to fix it — i.e. causes changes to reflect on the hot reload live.

As a possible feature, could we provide paths to whatever is watching the filesystem for hot reload?

adamhp avatar Feb 10 '22 22:02 adamhp

In my case I have all my components inside the ./app folder. But hot-reload is still not work. I am on MacOS

seyaobey-dev avatar Feb 12 '22 15:02 seyaobey-dev

I still have not found a solution other than it the hot-reloading it working on my Linux machine but refuses to work on Windows.

I hit the same problems, then realised that WSL was sat in the middle. Live Reload started working perfectly once I moved my working directory to the Linux filesystem. https://docs.microsoft.com/en-us/windows/wsl/setup/environment#file-storage

outmost avatar Feb 15 '22 20:02 outmost

same problem here on Windows and create-remix, I'm trying to follow the QuickStart tutorial but it doesn't reload 😢

nippur72 avatar Feb 17 '22 09:02 nippur72

Live Reload started working perfectly once I moved my working directory to the Linux filesystem. https://docs.microsoft.com/en-us/windows/wsl/setup/environment#file-storage

It doesn't seem to work on my end, even though I have project files in \\wsl$\.... It doesn't work in WSL2 or Docker. I don't know if it's related, but I get an error in browser console:

react-dom.development.js:67 Warning: Did not expect server HTML to contain a <script> in <html>.
    at head
    at html
    at http://localhost:3000/build/_shared/chunk-ONGAJZNE.js:20292:45
    at App
    at RemixRoute (http://localhost:3000/build/_shared/chunk-ZOVL2M7D.js:2492:3)
    at Routes2 (http://localhost:3000/build/_shared/chunk-ZOVL2M7D.js:2475:7)
    at Router (http://localhost:3000/build/_shared/chunk-ZOVL2M7D.js:287:15)
    at RemixCatchBoundary (http://localhost:3000/build/_shared/chunk-ZOVL2M7D.js:1022:10)
    at RemixErrorBoundary (http://localhost:3000/build/_shared/chunk-ZOVL2M7D.js:947:5)
    at RemixEntry (http://localhost:3000/build/_shared/chunk-ZOVL2M7D.js:2372:12)
    at RemixBrowser (http://localhost:3000/build/_shared/chunk-ZOVL2M7D.js:3068:27)
    at ClientCacheProvider (http://localhost:3000/build/entry.client-SUATVO3Z.js:30:32)

Tymek avatar Mar 15 '22 19:03 Tymek

@Tymek the warning is unrelated, check #1077

machour avatar Mar 18 '22 09:03 machour

Thx.

FIY everybody, if you move to Remix Stack (Blues) there is no issue with hot reloading in WSL2. It is using pm2 server internally for that, AFAIK. That's what I did.

Tymek avatar Mar 20 '22 17:03 Tymek

Hot reload doesn't work for me on [email protected] with the starter templates. I'm on Windows with WSL2.

I dug a little deeper and found the root cause here: https://github.com/microsoft/WSL/issues/4739

This issue comes up across many tools, and the workaround is often to switch to polling.

  • For example, chokidar provides the usePolling flag: https://github.com/paulmillr/chokidar#performance
  • Vite also forwards the usePolling flag from chokidar for this exact reason: https://vitejs.dev/config/#server-watch
  • esbuild watch mode uses polling by default: https://esbuild.github.io/api/#watch

Maybe remix should also offer a polling option?

mayank99 avatar Mar 24 '22 00:03 mayank99

I don't mean to +1, but I'm also getting the issue on mac, with a fresh vercel/ts boilerplate.

mikestopcontinues avatar Mar 24 '22 21:03 mikestopcontinues

Same issue with me Am on Windows10 and all setup of remix was good but I then realized that My localhost isnt hotreloading on changes.., I have used this exp {process.env.NODE_ENV === 'development' ? <LiveReload/> : null} ..,But still my issue is persisting.

Devr-pro avatar Mar 25 '22 04:03 Devr-pro

They had started removing cross-env NODE_ENV=development from the package.json scripts. Due to the order in which things were occurring, the internal <LiveReload/> component wasn't getting the property environment.

Re-add the cross-env and it should work.

kiliman avatar Mar 25 '22 18:03 kiliman

@kiliman It doesn't work with or without manually setting NODE_ENV via CLI, .env, or in the config files. NODE_ENV is 'development' without any of them anyway.

mikestopcontinues avatar Mar 26 '22 14:03 mikestopcontinues

Any known fixes? Running remix on WSL Ubuntu 20.04

BiffBish avatar Mar 27 '22 15:03 BiffBish

<LiveReload/> is a simple component. You can simply inline it in your root route.

export const LiveReload =
  process.env.NODE_ENV !== "development"
    ? () => null
    : function LiveReload({
        port = Number(process.env.REMIX_DEV_SERVER_WS_PORT || 8002),
      }: {
        port?: number;
      }) {
        let setupLiveReload = ((port: number) => {
          let protocol = location.protocol === "https:" ? "wss:" : "ws:";
          let host = location.hostname;
          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.onerror = (error) => {
            console.log("Remix dev asset server web socket error:");
            console.error(error);
          };
        }).toString();

        return (
          <script
            suppressHydrationWarning
            dangerouslySetInnerHTML={{
              __html: `(${setupLiveReload})(${JSON.stringify(port)})`,
            }}
          />
        );
      };

kiliman avatar Mar 28 '22 15:03 kiliman

<LiveReload/> is a simple component. You can simply inline it in your root route.

export const LiveReload =
  process.env.NODE_ENV !== "development"
    ? () => null
    : function LiveReload({
        port = Number(process.env.REMIX_DEV_SERVER_WS_PORT || 8002),
      }: {
        port?: number;
      }) {
        let setupLiveReload = ((port: number) => {
          let protocol = location.protocol === "https:" ? "wss:" : "ws:";
          let host = location.hostname;
          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.onerror = (error) => {
            console.log("Remix dev asset server web socket error:");
            console.error(error);
          };
        }).toString();

        return (
          <script
            suppressHydrationWarning
            dangerouslySetInnerHTML={{
              __html: `(${setupLiveReload})(${JSON.stringify(port)})`,
            }}
          />
        );
      };

This doesn't seem to work? i start up the server and when i make a change to a document and do ctrl+s nothing happens int he console and the page doesn't recompile and build image

BiffBish avatar Mar 30 '22 22:03 BiffBish

Had the same issue. Fixed by changing remix.confix.js from:

module.exports = {
  ignoredRouteFiles: [".*"],
};

to:

module.exports = {
  cacheDirectory: "./node_modules/.cache/remix",
  ignoredRouteFiles: [".*", "**/*.css", "**/*.test.{js,jsx,ts,tsx}"],
};

nilobarp avatar Apr 04 '22 04:04 nilobarp

@nilobarp That's already in my remix.config.js. Why would it matter either way? My best guess is these "solutions" are all part of an elaborate April fools prank. 😁

mikestopcontinues avatar Apr 04 '22 20:04 mikestopcontinues

I believe there was a bug in the dev tool in that it wasn't passing the REMIX_DEV_SERVER_WS_PORT variable to the compiler. Have you tried the latest version? Remix now gets a random unused port for the WS port.

kiliman avatar Apr 04 '22 22:04 kiliman

@mikeybinnswebdesign wish it was a prank. I tried everything mentioned in this thread, the only thing that worked was the change in remix.config.js. I undid the change reload stopped working, bring it back reload works again.

This is too much magic for my brain.

nilobarp avatar Apr 04 '22 22:04 nilobarp

@nilobarp @kiliman — Just figured out, for me it was disabling devServerPort which doesn't seem to be working anyway. Thanks!

mikestopcontinues avatar Apr 05 '22 00:04 mikestopcontinues

LiveReload does not work for me,

I copied the code that @kiliman shared and added more console logs. The frontend never seems to connect to the WS server and is just constantly pending.

image

No ws.onmessage console.log is ever ran either.

Any suggestions?

LukeXF avatar Apr 07 '22 17:04 LukeXF

@LukeXF Are you on Mac or Windows?

Did you add/change devServerPort in remix.config?

Also check to see if REMIX_DEV_SERVER_WS_PORT environment variable is defined.

Worst case, just update your dev script to:

"cross-env NODE_ENV=development PORT=3000 REMIX_DEV_SERVER_WS_PORT=8002 remix dev"

Also, try to connect to the web socket server using your browser: http://localhost:8002

You should see the message "Upgrade Required"

kiliman avatar Apr 07 '22 18:04 kiliman

@drewkill32 is this issue fixed for you ?

machour avatar Apr 07 '22 23:04 machour

Just tried again with the latest from remix. No it hasn't. If I run it on my Linux machine or Linux Subsystem for Windows the hot reload works.

drewkill32 avatar Apr 08 '22 00:04 drewkill32

I tried adding "cross-env NODE_ENV=development PORT=3000 REMIX_DEV_SERVER_WS_PORT=8002 remix dev" but I get:

Error: remix dev is not supported for custom servers. Because I'm using CF Pages in prod, anyway I removed the custom server: "./server.js" code and I get error: ReferenceError: crypto is not defined

{
  error: ReferenceError: crypto is not defined
  at createKey (/Users/lukebrown/Projects/example/remix/node_modules/@remix-run/cloudflare/crypto.js:35:13)
  at unsign (/Users/lukebrown/Projects/example/remix/node_modules/@remix-run/cloudflare/crypto.js:27:19)
  at decodeCookieValue (/Users/lukebrown/Projects/example/remix/node_modules/@remix-run/server-runtime/cookies.js:87:33)
  at Object.parse (/Users/lukebrown/Projects/example/remix/node_modules/@remix-run/server-runtime/cookies.js:53:66)
  at Object.getSession (/Users/lukebrown/Projects/example/remix/node_modules/@remix-run/server-runtime/sessions/cookieStorage.js:36:67)
  at loader (/Users/lukebrown/Projects/example/remix/app/root.tsx:145:39)
  at Object.callRouteLoader (/Users/lukebrown/Projects/example/remix/node_modules/@remix-run/server-runtime/data.js:77:20)
  at /Users/lukebrown/Projects/example/remix/node_modules/@remix-run/server-runtime/server.js:259:113
  at Array.map (<anonymous>)
  at handleDocumentRequest (/Users/lukebrown/Projects/example/remix/node_modules/@remix-run/server-runtime/server.js:259:67)
}

Then after installing I get: It appears you're using a module that is built in to node, but you installed it as a dependency which could cause problems. Please remove crypto before continuing. very odd! Didn't think I'd need to install crypto.

Anyway, I'm using node v16.13.0 on a Mac M1 Chip

REMIX_DEV_SERVER_WS_PORT is not used, but I have tried it, doesn't change anything, same with devServerPort, not using either at the moment.

http://localhost:8002/ still returns 404

I will investigate further on a clean project!

LukeXF avatar Apr 08 '22 13:04 LukeXF