question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Middleware + notFound is not working

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:17
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
ijjkcommented, Dec 1, 2022

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

2reactions
fkapsahilicommented, Sep 23, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Express 4 middleware when route is not found (finalhandler ...
You are sending the response in the routes files, I think, while I'm sending the response with the last middleware function. In your...
Read more >
Use Custom Middleware to Detect and Fix 404s in ASP.NET ...
Displaying Not Found Requests​​ For matching paths, the middleware will return a page with a table containing the NotFound requests, ordered by their...
Read more >
FAQ - Express.js
Add routes dynamically at runtime on an instance of express.Router() so the routes are not superseded by a middleware function. How do I...
Read more >
Handling errors in ASP.NET Core Web API - DevTrends
The main problem is that by using a string in the NotFound method, ... In this example, we configure both pieces of middleware...
Read more >
Express handling 404 errors: 404 middleware not working
app.use((req, res, next) => { console.log("404 error"); var err = new Error('Not Found'); err.status = 404; next(err); });.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found