-
Notifications
You must be signed in to change notification settings - Fork 27.8k
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
Hot-reloading of custom servers #791
Comments
I'm guessing it's easiest to do it the usual node server way, with nodemon 😄 |
I use nodemon for that myself => nodemon -w server.js -w src -x env-cmd develop babel-node server.js |
Thanks, that works 🙂
|
note that just doing |
It seems like the next dev server modifies source files while serving, which makes it so that |
Just installed |
Im not too familiar with nodemon, but for some reason |
Just so you guys know, you can configure a {
"verbose": true,
"ignore": ["node_modules", ".next"],
"watch": ["server/**/*", "index.js"],
"ext": "js json"
} And for those who are using 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! 😎 |
Worked great, thanks @jimmylee 👍 |
@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? |
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. |
I'm also facing issues with custom server and webpack HMR... Is there any solution, or nothing yet? |
The nodemon example solves the HMR issue. It will only reload the server when the server files change and not the Next app files. |
@sergiodxa I'm using nodemon example, but the issue is that when I change something in my client app, the console logs 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... 🤔 |
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. |
I wonder if we can use this while moving the |
any better solution here? |
Yeah, normally with a default Next.js server running separate from the Node.js API, any server-side changes cause the server to reload and get up and running pretty quick. But with a custom server, you'll trigger a new Next.js build, and your dev site will be inaccessible for much longer. This has been my biggest pain working with custom servers -- perhaps there's some way to communicate to Next.js to just use the same build it previously outputted? Would that not solve the issues we're having? |
An alternative solution that was released with Next.js v9 is API routes. They solve many of the use cases for having a custom server in the first place. |
The API routes feature won't work if you need https locally. A few instances when https is useful:
|
@kevlened could you write a RFC for that, I'm definitely willing to consider it at this point in time (the comment you're referring to is over 2 years old). |
However I configure this file, it only seems to watch the server.js file. What about the /pages folder where I have .jsx files? I've tried this but it has no effect:
|
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
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.The text was updated successfully, but these errors were encountered: