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

Hot reload not working with Next.js 13.4.9 in Custom Server development environment #52931

Closed
1 task done
knightrun opened this issue Jul 20, 2023 · 14 comments · Fixed by #67983
Closed
1 task done

Hot reload not working with Next.js 13.4.9 in Custom Server development environment #52931

knightrun opened this issue Jul 20, 2023 · 14 comments · Fixed by #67983
Labels
bug Issue was opened via the bug report template. locked

Comments

@knightrun
Copy link

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.3.0: Mon Jan 30 20:38:37 PST 2023; root:xnu-8792.81.3~2/RELEASE_ARM64_T6000
    Binaries:
      Node: 18.16.1
      npm: 9.5.1
      Yarn: 1.22.18
      pnpm: 8.4.0
    Relevant Packages:
      next: 13.4.9
      eslint-config-next: 12.3.4
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 4.7.4
    Next.js Config:
      output: standalone

Which area(s) of Next.js are affected? (leave empty if unsure)

App Router

Link to the code that reproduces this issue or a replay of the bug

.

To Reproduce

  1. Clone the project using Next.js 13.4.9 with the app directory structure.
  2. Start the Custom Server development environment using the command pnpm dev
  3. Access the application in a web browser.
  4. Make changes to the application code within the app directory, e.g. update a component or a page.
  5. Observe that the changes are not automatically reflected in the browser, and the hot reload does not occur.

Describe the Bug

I am using Next.js version 13.4.9 with the app directory structure and running my development environment with a Custom Server. However, I've noticed that the hot reload feature is not functioning as expected. Changes made to the code are not automatically reflected in the browser during development.

Expected Behavior

  1. When I make changes to my Next.js application code (e.g., components, pages, styles) in the app directory while the Custom Server is running, I should see the changes applied automatically without needing to manually refresh the browser.
  2. The development environment should detect file modifications and trigger a hot reload, ensuring a smooth and efficient development experience.

Which browser are you using? (if relevant)

Chrome 114.0.5735.198

How are you deploying your application? (if relevant)

No response

@knightrun knightrun added the bug Issue was opened via the bug report template. label Jul 20, 2023
@leeyc924
Copy link

There is the same issue when updated to version 13.4.11

@kacper-zylka
Copy link

Having the same issue. When I make a change in a file the terminal prints

- wait compiling...
- event compiled client and server successfully in 403 ms (2429 modules)

but the change is not reflected in the browser until I refresh manually.

I see this error which could be related:
image

@leeyc924
Copy link

leeyc924 commented Aug 1, 2023

It seems that I encountered an issue while modifying "revalidate" during the upgrade to version 13.3.xx. When I tested it with version 13.2.4, it worked correctly.

v13.2.4-canary.4...v13.2.4-canary.5#diff-4c54e369ddb9a2db1eed95fe1d678f94c8e82c540204475d42c78e49bf4f223a

@Inzendis
Copy link

Inzendis commented Sep 25, 2023

Having the same issue. When I make a change in a file the terminal prints

- wait compiling...
- event compiled client and server successfully in 403 ms (2429 modules)

but the change is not reflected in the browser until I refresh manually.

I see this error which could be related: image

So im having this issue right now. Any news?

I tried using nodemon as well and it is detecting changes but I still can't see the changes even if it refreshes.

I think its just not capable currently. There is also no support or enough documentaion. I like app router but I need to use my own server for cron jobs that are coded in the actual repo.

@nwankwo-ikemefuna
Copy link

Upgrading my node version from 16.x to >= 20 fixed it for me. I didn't try with v18.16.1 though.

@sheisacreative
Copy link

Thanks, @nwankwo-ikemefuna!
It solved for me too. :)

I just installed node version 20.9.0 and it worked like a charm.

@alanhamlett
Copy link

alanhamlett commented Dec 4, 2023

I'm on node v20.6.1 and also experiencing this bug. Also seeing the websocket.js:77 WebSocket connection to 'ws://localhost:3000/_next/webpack-hmr' failed: init @ websocket.js:77 error too.

Edit: Upgraded node to v22.0.0-nightly20231204951d00d36b and it's fixed.

Edit: Still happens with nightly node. The app only hot reloads components once, then never again until restarted.

Might be related to #51162.

@AoiTechDev
Copy link

Yeah, same issue, I have node v20.10.0 and next 14.0.4. In all cases which I tested I had pages folder with _app.js file. When I deleted it, it did not help, however, I started new project, and installed and configured all nessesery things but without _app.js file it got fixed. Got these errors in dev tools, idk if it helps.
image

@frankievx
Copy link

Upgrading node to v20.10.0 didn't help me. It seems as if my browser is defaulting to my prod version after running a local build than the dev server that's running.

@Sumansourabh14
Copy link

The following worked for me.

I had to create a nodemon.json file in my root directory with the following content:

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

After this, my Next.js app started reloading after any change made in the app.

Version:

"next": "14.1.0"
"node": "18.17.0"

@VenkateshSavai
Copy link

VenkateshSavai commented Feb 22, 2024

HMR WebSocket Connection Fails After Upgrading from Next.js 13 to 14

Description: I've recently upgraded my project from Next.js version 13 to 14 and started experiencing WebSocket connection failures for Hot Module Replacement (HMR). The console logs multiple errors indicating that the WebSocket connection to 'ws://localhost:3000/_next/webpack-hmr' failed.

Expected Behavior: HMR should establish a WebSocket connection successfully, allowing for live reloading of modules during development.

Actual Behavior: The browser console logs WebSocket connection errors repeatedly, stating: "WebSocket connection to 'ws://localhost:3000/_next/webpack-hmr' failed".

Environment Information:

Next.js version: 13 (before upgrade), 14 (after upgrade)
Node.js version: 20.10.0
OS: windows
No custom Webpack configuration was used prior to the upgrade.

Attempted Solutions:

Checked next.config.js for any HMR or WebSocket related configurations that might need updating.
Verified that there are no network issues or blockers such as browser extensions or firewall settings.
Searched through the Next.js GitHub issues and discussions for similar problems post-upgrade.
Additional Context:

The issue occurs consistently after the upgrade and was not present in the previous version of Next.js.
Attaching a screenshot of the console errors for reference.
The development server is running on http://localhost:3000 as usual, with no other apparent issues besides HMR.
Screenshot:
image

@StreetGUY32

This comment has been minimized.

@VenkateshSavai
Copy link

Hey @StreetGUY32 ,

Yeah! Issue resolved. It was because of the assetPrefix in the next config file.
After removing it, the issue got resolved!

Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 13, 2024
ztanner pushed a commit that referenced this issue Aug 26, 2024
<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

Closes NEXT-
Fixes #

-->

fixes #63820
fixes #55320
fixes #52931

In one of the issues, there was a suggestion to delete `assetPrefix`
from `next.config`. It helps if you have `assetPrefix: '/'`. Otherwise,
you probably won't face this problem.

**Explanation:**

The problem lies where the browser tries to connect and where websocket
is available on the server.

Adjust the condition for `prefix` in `get-socket-url` as it can return
`''` which will lead to `//` in the url.

The browser wants to connect to `ws://localhost:3000/_next/webpack-hmr`
While internally next exposes the web socket under
`ws://localhost:3000//_next/webpack-hmr` - you can connect to it via
wscat, postman or whatever.
As the path is different it does not handle HMR requests in the browser.

In addition to that - Reuse logic and extract separate files as a helper
in shared/lib.

| Before | After |
| ------ | ------- |
| ![before - latest canary
branch](https://github.com/user-attachments/assets/c26c8b20-1352-49c6-a099-101394438ba0)
before - latest canary branch - we can't establish a connection to HMR
ws |
![after](https://github.com/user-attachments/assets/26500e68-bc4d-44ca-b418-f9bda6bc9aa6)
same example with local changes - connected to ws |

---------

Co-authored-by: Jiwon Choi <devjiwonchoi@gmail.com>
Co-authored-by: JJ Kasper <jj@jjsweb.site>
ztanner pushed a commit that referenced this issue Aug 26, 2024
<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

Closes NEXT-
Fixes #

-->

fixes #63820
fixes #55320
fixes #52931

In one of the issues, there was a suggestion to delete `assetPrefix`
from `next.config`. It helps if you have `assetPrefix: '/'`. Otherwise,
you probably won't face this problem.

**Explanation:**

The problem lies where the browser tries to connect and where websocket
is available on the server.

Adjust the condition for `prefix` in `get-socket-url` as it can return
`''` which will lead to `//` in the url.

The browser wants to connect to `ws://localhost:3000/_next/webpack-hmr`
While internally next exposes the web socket under
`ws://localhost:3000//_next/webpack-hmr` - you can connect to it via
wscat, postman or whatever.
As the path is different it does not handle HMR requests in the browser.

In addition to that - Reuse logic and extract separate files as a helper
in shared/lib.

| Before | After |
| ------ | ------- |
| ![before - latest canary
branch](https://github.com/user-attachments/assets/c26c8b20-1352-49c6-a099-101394438ba0)
before - latest canary branch - we can't establish a connection to HMR
ws |
![after](https://github.com/user-attachments/assets/26500e68-bc4d-44ca-b418-f9bda6bc9aa6)
same example with local changes - connected to ws |

---------

Co-authored-by: Jiwon Choi <devjiwonchoi@gmail.com>
Co-authored-by: JJ Kasper <jj@jjsweb.site>
ztanner pushed a commit that referenced this issue Aug 26, 2024
<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

Closes NEXT-
Fixes #

-->

fixes #63820
fixes #55320
fixes #52931

In one of the issues, there was a suggestion to delete `assetPrefix`
from `next.config`. It helps if you have `assetPrefix: '/'`. Otherwise,
you probably won't face this problem.

**Explanation:**

The problem lies where the browser tries to connect and where websocket
is available on the server.

Adjust the condition for `prefix` in `get-socket-url` as it can return
`''` which will lead to `//` in the url.

The browser wants to connect to `ws://localhost:3000/_next/webpack-hmr`
While internally next exposes the web socket under
`ws://localhost:3000//_next/webpack-hmr` - you can connect to it via
wscat, postman or whatever.
As the path is different it does not handle HMR requests in the browser.

In addition to that - Reuse logic and extract separate files as a helper
in shared/lib.

| Before | After |
| ------ | ------- |
| ![before - latest canary
branch](https://github.com/user-attachments/assets/c26c8b20-1352-49c6-a099-101394438ba0)
before - latest canary branch - we can't establish a connection to HMR
ws |
![after](https://github.com/user-attachments/assets/26500e68-bc4d-44ca-b418-f9bda6bc9aa6)
same example with local changes - connected to ws |

---------

Co-authored-by: Jiwon Choi <devjiwonchoi@gmail.com>
Co-authored-by: JJ Kasper <jj@jjsweb.site>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked
Projects
None yet
Development

Successfully merging a pull request may close this issue.