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

Most upvoted comments

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 }, with middleware.ts returning NextResponse.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 release 12.3.1.

Yeah…well actually it helps downgrading to 12.1.6 ^^’ But that shouldnt be the end solution ^^’

I can confirm that this issue is still exists on the latest next (v13.1.1). I use getStaticProps + getStaticPaths with fallback: blocking and middleware. 404 pages works incorrectlry with client-side navigation. I have [slug].js pages.

If I open exisiting page like /post-1 and then open 404 page like /post-not-found I see /post-1 content (what???).

If I open 404 page directlry (I see 404 error page) and then open another 404 page like /post-another-not-found I see correct 404 error page.

If I open some other page (not [siug].js and then open 404 page I see empty page without any content.

If I run next dev locally everything work as expected.

I can confirm that issue described by me above was fixed in 13.2.4. Thanks

I 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.

image

I’m not able to check this with 13.2.1 (I’ve removed middlewares because of this issue) but reading the changelog looks like there are some work about this issue. Anyone can check this?

Hi, @emmgfx , I checked. Issue is still exists on 13.2.1

Any update on this issue?