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

Hot-reloading of custom servers #791

Closed
sedubois opened this Issue Jan 16, 2017 · 15 comments

Comments

@sedubois
Copy link
Contributor

sedubois commented Jan 16, 2017

I think the custom server examples (e.g dynamic routing: https://github.com/zeit/next.js/tree/master/examples/parameterized-routing) don't hot-reload when the server.js itself changes. How is that done? It would be great if the example showed that.

@timneutkens

This comment has been minimized.

Copy link
Member

timneutkens commented Jan 16, 2017

I'm guessing it's easiet to do it the usual node server way, with nodemon 😄

@markthethomas

This comment has been minimized.

Copy link

markthethomas commented Jan 17, 2017

I use nodemon for that myself =>

nodemon -w server.js -w src -x env-cmd develop babel-node server.js
@sedubois

This comment has been minimized.

Copy link
Contributor Author

sedubois commented Jan 17, 2017

Thanks, that works 🙂

"dev": "nodemon -w server.js server.js"

sedubois/relate@5b7d8b2

@sedubois sedubois closed this Jan 17, 2017

@newswim

This comment has been minimized.

Copy link

newswim commented Apr 16, 2017

note that just doing nodemon server.js sends the process into a tailspin.

@brandones

This comment has been minimized.

Copy link

brandones commented Aug 14, 2017

It seems like the next dev server modifies source files while serving, which makes it so that nodemon can't watch sources without getting confused. My server went into a tailspin when I tried nodemon -w pages -w components -w server.js server.js. With just nodemon -w server.js server.js I have to manually kill and restart the server in order for it to pick up changes to non-server files.

@jakewies

This comment has been minimized.

Copy link

jakewies commented Aug 16, 2017

Just installed nodemon hoping to get reloading of a custom server routing nextjs apps and ran with nodemon server.js too. Got the tailspin. Def not recommended. Is there a better solution?

@pgsandstrom

This comment has been minimized.

Copy link

pgsandstrom commented Aug 25, 2017

Im not too familiar with nodemon, but for some reason nodemon server.js sent me into a tailspin but nodemon -w server.js server.j worked. I get a redundant compile each swap, but it works.

@jimmylee

This comment has been minimized.

Copy link

jimmylee commented Feb 12, 2018

Just so you guys know, you can configure a nodemon.json file in your project root folder:

{
  "verbose": true,
  "ignore": ["node_modules", ".next"],
  "watch": ["server/**/*", "index.js"],
  "ext": "js json"
}

And for those who are using babel-node and presets

nodemon index.js --exec babel-node --presets es2015,stage-1,stage-2,stage-3

However just like @pgsandstrom's suggestion, you won't get a tailspin but you still get the redundant compile each swap. Better than manual server restarting though! 😎

@msmichellegar

This comment has been minimized.

Copy link

msmichellegar commented Feb 20, 2018

Worked great, thanks @jimmylee 👍

@itaditya

This comment has been minimized.

Copy link

itaditya commented Jul 2, 2018

@timneutkens if I use nodemon with custom server then I loose all the benefits of webpack HMR. And I don't want that. Can I help in some way to make the custom server experience better?

@brainkim

This comment has been minimized.

Copy link
Contributor

brainkim commented Jul 18, 2018

If you’re still having problems, next.js (on OSX at least) has a habit of touching files in the pages directory, so you have to make nodemon ignore your pages directory as well. next.js handles changes in those files anyways.

@guiihlopes

This comment has been minimized.

Copy link

guiihlopes commented Dec 4, 2018

@timneutkens if I use nodemon with custom server then I loose all the benefits of webpack HMR. And I don't want that. Can I help in some way to make the custom server experience better?

I'm also facing issues with custom server and webpack HMR... Is there any solution, or nothing yet?

@sergiodxa

This comment has been minimized.

Copy link
Contributor

sergiodxa commented Dec 4, 2018

The nodemon example solves the HMR issue. It will only reload the server when the server files change and not the Next app files.

@guiihlopes

This comment has been minimized.

Copy link

guiihlopes commented Dec 4, 2018

@sergiodxa I'm using nodemon example, but the issue is that when I change something in my client app, the console logs [HMR] bundle rebuilding, but it didn't change anything. Only when I refresh the page...

I inspected what was going on, and I have figure out that if there's any warning from TSlint the HMR not work as expected... 🤔

@glenjamin

This comment has been minimized.

Copy link

glenjamin commented Dec 16, 2018

Restarting the server process via nodemon means there'll need to be a fresh client build after the restart, which can take a bit longer than an in-memory refresh.

It is possible to do "hot reloading" of the server code in a few different ways, with varying degrees of reliability - but it's hard to do in the general case.

If there was a middleground between using the built-in next server and the fully custom server where routes could be added via an injection point, it might be possible to support reloading of the custom routes module on the server without restarting the whole process - but if there were any stateful objects in that module then this can be difficult to deal with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment