next.js: Middleware + notFound is not working
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: 14.19.1 npm: N/A Yarn: N/A pnpm: N/A Relevant packages: next: 12.2.0 eslint-config-next: N/A react: 18.2.0 react-dom: 18.2.0
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
When using middleware and navigate to a page using next/link
that return in it’s getStaticProps
a { notFound:true }
the middleware returns the page itself instead of 404.
Expected Behavior
When the page returns { notFound: true }
the middleware should return 404 page.
Link to reproduction
https://github.com/Louai99k/next-not-found-bug
To Reproduce
Just install the repo above and run it on your machine. You’ll notice in the main page 2 links one normal anchor tag and a next/link
. if the middleware exist the link comp from next won’t work while the a tag will be working but after removing the middleware both of the links will work just fine.
Not: the api end point returns 404 response and that’s my condition in the project to return 404 page.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 25
- Comments: 34 (7 by maintainers)
Commits related to this issue
- Fix middleware notFound: true handling (#46759) This ensures we properly handle rendering the `404` page when `notFound: true` is returned and middleware is present. ## Bug - [x] Related issue... — committed to vercel/next.js by ijjk a year ago
- Fix middleware 404 prefetch case (#46782) Ensures we properly generate the 404 page when a path is being prefetched with middleware configured. ## Bug - [x] Related issues linked using `fixes #num... — committed to vercel/next.js by ijjk a year ago
Closing as this does appear to be working as expected in the latest version of Next.js
@ijjk this keeps happening on
13.1.6
, should be reopened.@ijjk The issue is present in the latest version, please reopen it.
My use-case specifically is
getStaticProps
which returns{ notFound: true, revalidate: 1 }
, withmiddleware.ts
returningNextResponse.next()
- works fine in dev mode, but fails when built for prod.One more detail - works fine when navigating directly to 404 link, but is broken when using client-side navigation via
Link
.I am on next@13.3.0 and still facing the issue.
Hey @samcx , just tested 13.2.4-canary.4. It seems to be fixed, thanks!
Had the same issue with
getServerSideProps
. Seems to be fixed in the most recent release12.3.1
.Yeah…well actually it helps downgrading to 12.1.6 ^^’ But that shouldnt be the end solution ^^’
I can confirm that issue described by me above was fixed in
13.2.4
. ThanksI am on next@13.3.1 and have the same issue: middleware + getServerSideProps + Vercel => 404. But it works in the dev local.
@joggienl Thanks for sharing, was able to reproduce that myself too—we’ll take a look!
Cool!
I’ve updated the example I added to this thread. My example is not ignoring the notFound anymore.
I do get a strange response on Vercel though: https://nextjs-middleware-and-redirects.vercel.app/de/post/2
I am not sure if it is related, I also do not have a clear reproduction path. Any thoughs @ijjk ?
edit: “no clear reproduction”, it only happens on Vercel when navigating directly to that page (or a refresh). Navigation via on page links works as expected. Also I could not “break” it locally both dev and build-start.
Hi, @emmgfx , I checked. Issue is still exists on
13.2.1
Any update on this issue?