nextjs - 💡(How to fix) Fix Middleware is called multiple times if -hostname passed [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#84341Fetched 2026-04-08 02:19:47
View on GitHub
Comments
1
Participants
2
Timeline
10
Reactions
0
Author
Timeline (top)
subscribed ×4labeled ×2commented ×1issue_type_added ×1

Error Message

  1. open the browser on /fake - it throws hydration error, middleware called twice, /fake, /

Code Example

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.0.0: Mon Aug 25 21:17:56 PDT 2025; root:xnu-12377.1.9~3/RELEASE_ARM64_T6030
  Available memory (MB): 36864
  Available CPU cores: 12
Binaries:
  Node: 22.19.0
  npm: 10.9.3
  Yarn: 1.22.22
  pnpm: 10.17.1
Relevant Packages:
  next: 15.5.4 // Latest available version is detected (15.5.4).
  eslint-config-next: N/A
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.9.2
Next.js Config:
  output: N/A
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/felixmosh/bug-next-middleware

To Reproduce

There is weird weird issue, that I've tracked back to v13. (happens on v15.5.4 - latest version) For some reason, when I'm passing -H 127.0.0.1 to bind the server to localhost, the Rewrite behavior changes, instead of renderind the rewritten page, it proxy pass to it, which returns a wrong output (in my case it is related to the asPath in the render) and calls the middleware twice!.

Reproduction:

  1. run next dev
  2. open the browser on /fake - it should render, middleware called once
  3. stop the run
  4. run next dev -H 127.0.0.1
  5. open the browser on /fake - it throws hydration error, middleware called twice, /fake, /

Current vs. Expected behavior

It should behave the same when host is provided.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.0.0: Mon Aug 25 21:17:56 PDT 2025; root:xnu-12377.1.9~3/RELEASE_ARM64_T6030
  Available memory (MB): 36864
  Available CPU cores: 12
Binaries:
  Node: 22.19.0
  npm: 10.9.3
  Yarn: 1.22.22
  pnpm: 10.17.1
Relevant Packages:
  next: 15.5.4 // Latest available version is detected (15.5.4).
  eslint-config-next: N/A
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.9.2
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Dynamic Routes, Pages Router

Which stage(s) are affected? (Select all that apply)

next start (local), next dev (local)

Additional context

I've debugged this alot!

Looks like when -H is passed the parsedUrl has protocol, and these lines are triggering the proxyPass.

Eventhoght the only thing I've changed is the pathname in the middleware.

extent analysis

TL;DR

  • The issue can be mitigated by adjusting the middleware to handle the parsedUrl protocol difference when the -H flag is used.

Guidance

  • Investigate the router-server.ts lines 445-454 to understand how the proxyPass is triggered when the -H flag is used.
  • Verify that the pathname change in the middleware is the sole modification and that it's correctly handling the parsedUrl protocol.
  • Check if there are any existing issues or pull requests in the Next.js repository related to this behavior.
  • Consider creating a custom middleware to handle the proxyPass behavior when the -H flag is used.

Example

  • No code example is provided as the issue requires a deeper understanding of the Next.js internals and the custom middleware implementation.

Notes

  • The issue seems to be related to the next dev command and the -H flag, which might be specific to the development environment.
  • The provided environment information and package versions might be relevant to the issue, but further investigation is needed to determine the root cause.

Recommendation

  • Apply workaround: Adjust the middleware to handle the parsedUrl protocol difference when the -H flag is used, as it seems to be the most likely cause of the issue.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING