next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Middleware + notFound is not working

Open Louai99k opened this issue 2 years ago • 8 comments

Verify canary release

  • [X] 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.

Louai99k avatar Jul 01 '22 15:07 Louai99k

Is there any solution or workaround yet? Cause we are now having big problems with our web projects, which result in empty pages instead of showing the 404 page :c

We use NextJS in connection with TinaCMS. We download the content from a database and the page json is compiled into a page with next build && next export. The thing is, when I have a dead link on the page now, I get an empty page without any error or something else. I tried to build a workaround there, to manipulate the content of the page, but the problem is, that no next-function (getStaticProps) is called for the empty page....so i can't get my customized 404 page, cause that is also stored in the database ^^'

What I also noticed is, that when I compile my projects with node 16, everything seems to work. But our web server, which is the main compiler uses node 14....maybe it has something to do with that? I think I ask my colleague, if we have the possibility to update that....then I will tell you, if it works now or not :3

AkitoItashi avatar Aug 30 '22 07:08 AkitoItashi

Same problem here. When I put the middleware.js file, 404 not work properly and try to enter on the page. If I put just an clean middleware file, Link component doesn't work properly.

Erickinhou avatar Sep 09 '22 11:09 Erickinhou

Well I now had the time to upgrade to Node 16, but it didn't work either. Still getting an empty page ^^'

It looks like when i export or build the project, the problem seems to come up ^^' When i use the local dev server everything works ^^'

AkitoItashi avatar Sep 14 '22 13:09 AkitoItashi

I guess they did great job by ruining the old system by replacing legacy _middleware.js with this crab.

Louai99k avatar Sep 14 '22 13:09 Louai99k

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

AkitoItashi avatar Sep 14 '22 13:09 AkitoItashi

Had the same issue with getServerSideProps. Seems to be fixed in the most recent release 12.3.1.

fkapsahili avatar Sep 23 '22 20:09 fkapsahili

I'll give it a try if it's resolved I'll close this issue.

Louai99k avatar Sep 23 '22 21:09 Louai99k

Any update on this issue?

iscekic avatar Nov 09 '22 12:11 iscekic

Closing as this does appear to be working as expected in the latest version of Next.js

ijjk avatar Dec 01 '22 03:12 ijjk

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

iscekic avatar Dec 18 '22 20:12 iscekic

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.

oleggrishechkin avatar Jan 12 '23 13:01 oleggrishechkin

@ijjk Any update here? It still appears to be broken.

iscekic avatar Jan 12 '23 13:01 iscekic

https://github.com/vercel/next.js/issues/43953 - same

oleggrishechkin avatar Jan 12 '23 13:01 oleggrishechkin

Any updates? +1 : Same issue

cha2hyun avatar Jan 19 '23 09:01 cha2hyun

@ijjk this keeps happening on 13.1.6, should be reopened.

emmgfx avatar Feb 07 '23 12:02 emmgfx

Can confirm that this is also occurring for me on 13.1.6.

benjamin-p-newton avatar Feb 10 '23 10:02 benjamin-p-newton

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?

emmgfx avatar Feb 27 '23 14:02 emmgfx

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

oleggrishechkin avatar Feb 27 '23 15:02 oleggrishechkin

In checking some other behaviour (https://github.com/vercel/next.js/discussions/46613) I also stumbled upon this.

I've got an example with current 13.2.3 version.

On dev I notice that the first click it seems to ignore the notFound completely. If you refresh the page or go directly to it, it will show the 404 page.

But more strange (and concerning to me) is that on production, going to the link will just render {}.

Repo: https://github.com/joggienl/nextjs-middleware-and-redirects Running "production" demo: https://nextjs-middleware-and-redirects.vercel.app

Direct link to the page with getStaticProps and a notFound set for the specific id: https://nextjs-middleware-and-redirects.vercel.app/post/2

joggienl avatar Mar 01 '23 11:03 joggienl

Hi, this has been updated in v13.2.4-canary.3 of Next.js, please update and give it a try!

ijjk avatar Mar 04 '23 23:03 ijjk

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

joggienl avatar Mar 05 '23 00:03 joggienl

@joggienl Thanks for sharing, was able to reproduce that myself too—we'll take a look!

samcx avatar Mar 05 '23 01:03 samcx

Hey @samcx , just tested 13.2.4-canary.4. It seems to be fixed, thanks!

joggienl avatar Mar 05 '23 21:03 joggienl

I still facing the issue with v13.2.4-canary.3 And my issue is only resolved by deleting middleware.js from the root level.

Also on vercel there is duplicate _next/data in the request URL (In the Network tab).

@ijjk @samcx @joggienl

hafizasadabbas avatar Mar 07 '23 09:03 hafizasadabbas

@hafizasadabbas There was an additional fix on one canary version above the one you tested. Can you test the latest canary and see if the issue persists?

samcx avatar Mar 07 '23 09:03 samcx

@samcx Still not working with 13.2.4-canary.4. This is my middleware file

import { NextResponse } from "next/server";

export function middleware() { return NextResponse.next(); }

No issue on dev but on vercel preview URL.

Also, the issue only appears when I land on a dynamic URL through the Next link, and when I reload the page appears. Statically generated pages working fine.

I have noticed 1 more thing in the network tab the request URL looks like https://abc-xyz.vercel.app/xyz/_next/data/NxzzOiyVVB5tOx/en/xyz/xyz/_next/data/NxzzOiyVVB5tOx/en/post.json?slug=post

When I delete middleware.js the request URL becomes https://abc-xyz.vercel.app/xyz/_next/data/NxzzOiyVVB5tOx/en/post.json.json?slug=post

hafizasadabbas avatar Mar 07 '23 10:03 hafizasadabbas

@hafizasadabbas can you provide the actual deployment? Looks like the one you shared is deleted. Also we have tests covering this specific handling with notFound: true which are passing currently.

ijjk avatar Mar 07 '23 21:03 ijjk

Screenshot 2023-03-08 at 10 12 34 AM

Also, I set notFound: true in getStaticProps else case.

hafizasadabbas avatar Mar 08 '23 05:03 hafizasadabbas

@hafizasadabbas Is it possible to create a public, minimal reproduction repository that showcases this page+issue so we can take a closer look?

samcx avatar Mar 10 '23 05:03 samcx

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

oleggrishechkin avatar Mar 30 '23 13:03 oleggrishechkin