Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Live Reload not working. #1601

Closed
drewkill32 opened this issue Jan 21, 2022 · 102 comments
Closed

[Bug]: Live Reload not working. #1601

drewkill32 opened this issue Jan 21, 2022 · 102 comments
Labels
bug Something isn't working renderer:react

Comments

@drewkill32
Copy link

drewkill32 commented Jan 21, 2022

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 drewkill32 added the bug Something isn't working label Jan 21, 2022
@jacob-ebey
Copy link
Member

Check that the 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.

@drewkill32
Copy link
Author

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
Copy link
Author

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.

@kurdi-dev
Copy link

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!

@dan-cooke
Copy link

dan-cooke commented Feb 9, 2022

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

@adamhp
Copy link

adamhp commented Feb 10, 2022

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?

@seyaobey-dev
Copy link

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

@outmost
Copy link

outmost commented Feb 15, 2022

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

@nippur72
Copy link

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

@Tymek
Copy link

Tymek commented Mar 15, 2022

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)

@machour
Copy link
Collaborator

machour commented Mar 18, 2022

@Tymek the warning is unrelated, check #1077

@Tymek
Copy link

Tymek commented Mar 20, 2022

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.

@mayank99
Copy link

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: microsoft/WSL#4739

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

Maybe remix should also offer a polling option?

@mikestopcontinues
Copy link

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

@Devr-pro
Copy link

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.

@kiliman
Copy link
Collaborator

kiliman commented Mar 25, 2022

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.

@mikestopcontinues
Copy link

mikestopcontinues commented Mar 26, 2022

@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.

@BiffBish
Copy link

Any known fixes? Running remix on WSL Ubuntu 20.04

@kiliman
Copy link
Collaborator

kiliman commented Mar 28, 2022

<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)})`,
            }}
          />
        );
      };

@BiffBish
Copy link

<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

@nilobarp
Copy link

nilobarp commented Apr 4, 2022

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}"],
};

@mikestopcontinues
Copy link

mikestopcontinues commented Apr 4, 2022

@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. 😁

@kiliman
Copy link
Collaborator

kiliman commented Apr 4, 2022

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.

@nilobarp
Copy link

nilobarp commented Apr 4, 2022

@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.

@mikestopcontinues
Copy link

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

@LukeXF
Copy link

LukeXF commented Apr 7, 2022

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?

@dreymoreau
Copy link

@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"

@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"

this helped!!! thanks so much :D

@rodolphoasb
Copy link

If anyone tried everything that was suggested here and it still didn't work, I just restarted my MacBook and it's working now, seriously.

@BoilingSoup
Copy link

The official remix tutorial runs straight into this problem. Not a good first experience with the framework

@adaboese
Copy link

What's the latest here?

@karolis-sh
Copy link

I had previously removed both app/entry.server.tsx and app/entry.client.tsx (as I thought they were redundant based on generated file comments), regenerating them via npx remix reveal seems to have fixed issues wit live reloading

@enBonnet
Copy link
Contributor

I fix this by adding the components directory that I have outside of the app directory in the watchPaths array, my remix.conf.js file looks like this:

/** @type {import('@remix-run/dev').AppConfig} */
export default {
  ignoredRouteFiles: ["**/.*"],
  watchPaths: ["./components"],
  // appDirectory: "app",
  // assetsBuildDirectory: "public/build",
  // publicPath: "/build/",
  // serverBuildPath: "build/index.js",
};

@rodolphoasb
Copy link

rodolphoasb commented Oct 31, 2023

Reminder: you can use Vite now, which should improve this 🥳 https://remix.run/docs/en/main/future/vite#migrating

@camdowney
Copy link

camdowney commented Nov 1, 2023

Guess I should've actually read the docs first... fixed everything by changing my root.tsx from this:

export default () => {

To this:

export default function Root() {

I have no idea if this is a common limitation across frameworks, but the docs clearly state not to do what I was doing: https://remix.run/docs/en/main/discussion/hot-module-replacement.

@enBonnet
Copy link
Contributor

enBonnet commented Nov 1, 2023

Guess I should've actually read the docs first... fixed everything by changing my root.tsx from this:

export default () => {

To this:

export default function Root() {

I have no idea if this is a common limitation across frameworks, but the docs clearly state not to do what I was doing: https://remix.run/docs/en/main/discussion/hot-module-replacement.

Good to know.

@adaboese
Copy link

adaboese commented Nov 2, 2023

Good to know!

@atalayozmen
Copy link

atalayozmen commented Nov 4, 2023

I was having the same issue with the blues stack, I started with the template that had LiveReload after Scripts. I changed the order now where LiveReload comes first, and now it is fixed. This is important if you are using vitest.
Source: https://remix.run/docs/en/main/future/vite#livereload-before-scripts

From root.tsx:

      <body className="h-full">
          <Outlet />
          <ScrollRestoration />
          <LiveReload /> //comes before <Scripts/>
          <Scripts />
      </body>

@alisnino
Copy link

This is the most basic feature a frontend dev framework should have; it's not working in Nov/2023 so I'll report to my team Remix is not worth considering yet 😇

@enBonnet
Copy link
Contributor

This is the most basic feature a frontend dev framework should have; it's not working in Nov/2023 so I'll report to my team Remix is not worth considering yet 😇

It is working, but we have to improve the documentation because it'd work properly if you keep your code inside the /app directory.

@adaboese
Copy link

My structure is as follows:

└── src
    ├── app
    │   ├── defer
    │   └── routes
    │       ├── ...
    ├── components
    │   ├── previews
    │   │   └── data-table
    │   └── ui
    │       └── toast
    ├── hooks
    ├── routines
    ├── services
    ├── styled-system
    │   ├── css
    │   ├── jsx
    │   ├── patterns
    │   ├── recipes
    │   ├── tokens
    │   └── types
    └── utilities

Does this mean I need to move src/hooks/, src/components/, ... to src/app/?

Do I need to use named function for everything?

@pcattori
Copy link
Contributor

pcattori commented Nov 23, 2023

There seem to be multiple different issues people are running into.

Named exports

Named exports are required for HMR. Note that this is a limitation of React Fast Refresh, not Remix. Thus the same limitation is present in Vite and Next.

Code outside of app/ dir

Expecting changes outside of app/ to trigger hot updates. As already mentioned, you can add those paths to watchPaths, but we'd recommend keeping all your app code within app/ anyway.

LiveReload before Scripts (Vite)

This limitation has been removed from unstable Vite support. The order of LiveReload and Scripts no longer matters.

Quickstart/Tutorial not working

@BoilingSoup I couldn't reproduce this error, but if you are still running into it could you file a separate issue?


This issue was already too broad when it was first created, so moving forward please file separate issues with your specific problem so that we can make sure we prioritize a fix.

@pcattori
Copy link
Contributor

@alisnino are you facing a different issue than the ones I mentioned?

@rodolphoasb
Copy link

rodolphoasb commented Nov 23, 2023

Hey, @pcattori, first, I'd like to thank you for your amazing work integrating Vite.

Here's an issue that I opened related to the Vite live reload: #7853 (there's a repo where you can reproduce it), and tbh it makes the DX really bad.

Not sure how complex it is, but happy to take a look at the weekend if you could give me some pointers...

@pcattori
Copy link
Contributor

pcattori commented Nov 23, 2023

@rodolphoasb let's keep the discussion for that on your issue since this issue is focused on the current (non-Vite) compiler/dev server

@adaboese
Copy link

Can anyone recommend a workaround for naming functions? Using arrow functions would have quite a few downsides

@BoilingSoup
Copy link

@BoilingSoup I couldn't reproduce this error, but if you are still running into it could you file a separate issue?

#7466

It is here.

@pcattori
Copy link
Contributor

@adaboese you can use function keyword or arrow functions as long as they are named. Examples of this are in the docs.

@dharmendra94
Copy link

dharmendra94 commented Dec 6, 2023

Hey @pcattori, Even in the named exported functions from a package outside the app directory as below:

In repo/packages/home

export function Home() {
  return (<>Content here</>);
}

In repo/apps/remix-app/routes/home.tsx

import { Home, handle } from "@scope/home-package";
import { action, loader } "@scope/home-package/server";

export default Home;

export { action, loader, handle };

I am observing the HMR is not working when I make a change inside the Home package (I got it under watchPaths, and the connection from the dev server to the browser's LiveReload is working fine)

================

And the moment I change the route file as below, HMR starts working.

In repo/apps/remix-app/routes/home"

import { Home } from '@scope/home-package';

export default function HomePage() {
  return Home();
}

@pcattori
Copy link
Contributor

pcattori commented Dec 7, 2023

@dharmendra94 does this work in Vite? I'm wondering if this is just a limitation of the isLikelyComponent check from React Refresh

@dharmendra94
Copy link

Hey, @pcattori, I have tried Vite, and the issue no longer exists. Now I am curious, when will Vite be stable and ready for production? 🤔

@adaboese
Copy link

Code outside of app/ dir
Expecting changes outside of app/ to trigger hot updates. As already mentioned, you can add those paths to watchPaths, but we'd recommend keeping all your app code within app/ anyway.

I think watchPaths does not exist as an option when using with Vite plugin?

@kiliman
Copy link
Collaborator

kiliman commented Dec 17, 2023

@adaboese

I think watchPaths does not exist as an option when using with Vite plugin?

I haven't tried it yet, but you might try adding esbuild.include to your vite.config.js to add additional paths.

https://vitejs.dev/config/shared-options.html#esbuild

@pcattori
Copy link
Contributor

pcattori commented Dec 18, 2023

Hey, @pcattori, I have tried Vite, and the issue no longer exists. Now I am curious, when will Vite be stable and ready for production? 🤔

https://twitter.com/markdalgleish/status/1736240852308251053

#8266

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working renderer:react
Projects
None yet
Development

No branches or pull requests