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

Next [14.2.3] middleware is being by passed when using proxy/reverse proxy #65697

Open
AnasArafeh opened this issue May 13, 2024 · 0 comments
Open
Labels
bug Issue was opened via the bug report template. Middleware Related to Next.js Middleware

Comments

@AnasArafeh
Copy link

AnasArafeh commented May 13, 2024

Link to the code that reproduces this issue

https://github.com/AnasArafeh/Next14.2.3-Middleware-with-proxy-issue

To Reproduce

  1. Run the NextJs code
  2. Run the backend code which has the proxy in it (it is a dotnet app in this sample)
  3. Try to access a route that Next js file structure has, for example here /home (either on localhost:3000 or the backend localhost) a sample "http://localhost:3000/home"
  4. You will notice the console.log in the home page is invoked before the middleware and everything in the page is being read
    image
  5. The middleware will be invoked and will return what you are looking for (in this case I am just rewriting to 404 page)
  6. Also you will see that the middleware is being invoked multiple times for the same route along side the /home request

Current vs. Expected behavior

This behavior started after upgrading next to 14.2.3, before the upgrade I was working on next 14.0.3 and this issue did not occur. The middleware was being invoked first and everything worked normal.

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Enterprise
  Available memory (MB): 20323
  Available CPU cores: 8
Binaries:
  Node: 18.18.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.3.0-canary.59 // Latest available version is detected (14.3.0-canary.59).
  eslint-config-next: N/A
  react: 19.0.0-beta-4508873393-20240430
  react-dom: 19.0.0-beta-4508873393-20240430
  typescript: 5.1.3
Next.js Config:
  output: N/A

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

Middleware

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

next dev (local), next start (local)

Additional context

I have tested it in the latest canary version 14.3.0-canary.59 and the issue still exists.

Please be aware that the issue disappears when you remove this environment variable NODE_TLS_REJECT_UNAUTHORIZED=0

According to my knowledge this issue occurs when you have proxy + middleware + the API in the middleware returns 404 + having the environment variable mentioned above.

*Update
When I made the API as an HTTP instead of HTTPS and remove the environment variable NODE_TLS_REJECT_UNAUTHORIZED=0 the same issue happens. So probably the issue comes with proxy + middleware + API in the middleware returns 404 or so

**Update 2
When the API returns not found with empty response instead not found with HTML response, the issue is resolved

@AnasArafeh AnasArafeh added the bug Issue was opened via the bug report template. label May 13, 2024
@github-actions github-actions bot added the Middleware Related to Next.js Middleware label May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Middleware Related to Next.js Middleware
Projects
None yet
Development

No branches or pull requests

1 participant