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

404 Template Throws 500 Error when Using Middleware (v14.2.7) #69428

Closed
tjdunklee opened this issue Aug 28, 2024 · 5 comments · Fixed by #69799
Closed

404 Template Throws 500 Error when Using Middleware (v14.2.7) #69428

tjdunklee opened this issue Aug 28, 2024 · 5 comments · Fixed by #69799
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked Middleware Related to Next.js Middleware.

Comments

@tjdunklee
Copy link

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/jolly-taussig-tzm55f

To Reproduce

  1. View the repro CodeSandbox
  2. Try to visit any page that has a invalid url
  3. Notice the 500 error.

Current vs. Expected behavior

I would expect the custom 404 template to be displayed, instead a 500 error is thrown. This only seems to happen if a Middleware file is present. This seemed to start with v14.2.7.

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023
  Available memory (MB): 4102
  Available CPU cores: 2
Binaries:
  Node: 20.9.0
  npm: 9.8.1
  Yarn: 1.22.19
  pnpm: 8.10.2
Relevant Packages:
  next: 14.2.7 // Latest available version is detected (14.2.7).
  eslint-config-next: N/A
  react: 18.3.1
  react-dom: 18.3.1
  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 build (local), Other (Deployed)

Additional context

No response

@tjdunklee tjdunklee added the bug Issue was opened via the bug report template. label Aug 28, 2024
@github-actions github-actions bot added the Middleware Related to Next.js Middleware. label Aug 28, 2024
@mauricedb
Copy link

Running into the same issue.
Another repro can be found here: https://github.com/mauricedb/next-pages-404

I can confirm this started with v14.2.7 and v14.2.6 works as expected.

Workaround for me
Adding a export const config with a matcher limiting the middleware to the known valid routes so the middleware doesn't execute on the route that causes the 404 errors.

@tjdunklee
Copy link
Author

Thanks for the additional repro @mauricedb. Could you upvote 👍 my initial comment just to raise the visibility of this issue?

@ztanner ztanner added the linear: next Confirmed issue that is tracked by the Next.js team. label Aug 29, 2024
@ztanner
Copy link
Member

ztanner commented Sep 6, 2024

Thanks for the report folks -- we're looking into this.

@tjdunklee
Copy link
Author

Thanks for the update @ztanner! Much appreciated.

ijjk added a commit that referenced this issue Sep 6, 2024
This ensures we properly set the request meta during our middleware
invoke so that the `NoFallbackError` bubbling is handled properly.
Without these fields set we bubble the error unexpectedly causing an
unexpected 500 instead of a 404.

x-ref: #66987
Closes: #69428
ijjk added a commit that referenced this issue Sep 6, 2024
This ensures we properly set the request meta during our middleware
invoke so that the `NoFallbackError` bubbling is handled properly.
Without these fields set we bubble the error unexpectedly causing an
unexpected 500 instead of a 404.

x-ref: #66987
Closes: #69428
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 Sep 21, 2024
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. linear: next Confirmed issue that is tracked by the Next.js team. locked Middleware Related to Next.js Middleware.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants