next.js: [NEXT-435] Minimal middleware (only NextResponse.next()), generates error on error pages in dev mode
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System: Platform: win32 Arch: x64 Version: Windows 10 Pro Binaries: Node: 16.17.0 npm: N/A Yarn: N/A pnpm: N/A Relevant packages: next: 13.1.0 eslint-config-next: N/A react: 18.2.0 react-dom: 18.2.0
Which area(s) of Next.js are affected? (leave empty if unsure)
Routing (next/router, next/navigation, next/link)
Link to the code that reproduces this issue
https://github.com/w4zZz4p/issue-nextjs-error-page
To Reproduce
npm install
npm run dev
open http://localhost:3000/page-does-not-exist
Describe the Bug
When there is empty global middleware created:
import { NextResponse } from 'next/server';
export function middleware(request) {
// some rewrite logic for specific requests
// all other requests pass-trough
return NextResponse.next();
}
and when you visit any 404 page, error occurs:
Unhandled Runtime Error
Error: Invariant: attempted to hard navigate to the same URL /page-does-not-exist http://localhost:3000/page-does-not-exist
The issue appeared in version v13.0.7-canary.5
.
Version v13.0.7-canary.4
works as expected.
My assumption its related to https://github.com/vercel/next.js/pull/43836
Expected Behavior
There should be no error as in any existing page for example http://localhost:3000/
Which browser are you using? (if relevant)
Chrome 108.0.5359.125 (Official Build) (64-bit)
How are you deploying your application? (if relevant)
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 45
- Comments: 24 (5 by maintainers)
Commits related to this issue
- Downgrade Next to 13.0.6 https://github.com/vercel/next.js/issues/44293 — committed to bratislava/mestskakniznica.sk by MarekBodinger a year ago
- Downgrade next (#215) * Fix link in 404 * Downgrade Next to 13.0.6 https://github.com/vercel/next.js/issues/44293 --------- Co-authored-by: Marek Bodinger <marek.bodinger@gmail.com> — committed to bratislava/mestskakniznica.sk by MarekBodingerBA a year ago
- fix #44293 — committed to vercel/next.js by shuding a year ago
- Fix error on 404 page when middleware exists (#46303) Closes #44293. ## Bug - [ ] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have a helpful link attached... — committed to AndyBitz/next.js by shuding a year ago
Same issue here from
v13.0.7
to latest (v13.1.1
). Any Middleware file even if justexport function middleware() {}
breaks 404 page.Adding the 404 page to exclude from the matcher does not work indeed.
As I commented before, it is not just on dev. A build is broken as well.
If you have a dynamic page with
getStaticProps
andgetStaticPaths
the 404 page will not get loaded. Even if you return{ notFound: true }
from thegetStaticProps
it will not work. After a refresh it sometimes does.If you remove the
middleware.js
file (or just rename it for testing sake) it will work as expected again.Reason for mentioning it is that I think some things around middleware and navigating are not going well. Not just on dev but also in a production environment.
@damien can you look at this issue? Seems more apps have encountered the issue
A poor workaround that worked for me is to just fallback rewrite to a custom error page and remove 404.tsx.
The only con here is that it won’t respond with 404, but with 200 instead.EDIT: To make it respond with 404, add getServerSideProps() to the custom error page and make it return
{ notFound: true }
Complete workaround
next.config.js
middleware.js
pages/404.jsx
pages/404bug.jsx
Seems to have been introduced at https://github.com/vercel/next.js/pull/43836
Namely, this code was removed.
It seems like that when you have a middleware file, it triggers internal routing handling more than once. I noticed when running nextjs from source the first run is “good” but the second triggers this behaviour. This will also happen in production builds obviously.
I am not good enough into the nextjs repo to know how to fix or say something more educated about it, but that is what I noticed. It might help 🙈 .