Cannot return a 404 from getInitialProps anymore by throwing an error with code='ENOENT'
See original GitHub issueBug report
Describe the bug
Until next.js 9.2.1 I could return a 404 for a page when throwing an error with code = 'ENOENT'
in getInitialProps
. That no longer works in 9.3.2, now a 500 is returned. This is bad for SEO, we need to be able to throw 404s.
To Reproduce
- In getInitialProps, throw an error like this one:
export class Custom404Error extends Error {
code: string
constructor(message?: string) {
super(message)
this.code = 'ENOENT' // triggers a 404 in next.js
}
}
- Open that page in the browser
Expected behavior
Opening the page in the browser should return status code 404.
System information
- OS: macOS Catalina
- Browser (if applies) [e.g. chrome, safari]
- Version of Next.js: 9.3.2
Additional context
If there is another recommended way to return 404s from getInitialProps, I’d be happy about pointers.
Could the regression (or change of behavior, since ENOENT
was kind of an internal behavior) be caused by #10572?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:20 (15 by maintainers)
Top Results From Across the Web
Cannot return a 404 from getInitialProps anymore by throwing ...
Until next.js 9.2.1 I could return a 404 for a page when throwing an error with code = 'ENOENT' in getInitialProps .
Read more >Next.js return the 404 error page in getInitialProps
The problem is, if I want to return 404 like this, it will return a blank page instead of the usual Next.js 404...
Read more >How to return a 404 error in getServerSideProps with Next.js
Ever wondered how to redirect to a 404 error in getServerSideProps with Next.js? Follow our guide for the simples solution we've found!
Read more >Error 404 not found - What does it mean & how to fix it! - IONOS
Your browser displays the error message 404 Not Found instead the desired web page? The solution is easy - an HTTP 404 error...
Read more >Error 404: 4 Ways to Fix It - Hostinger
Error 404 is a response code, meaning the server could not locate the requested content. Check this article to learn 4 steps to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@fabb A soft 404 isn’t “worse”, but it could leak into search result pages…which is prevented by setting the page to “noindex”. The alternative way to deal with this would be to redirect to a page that is configured to return a 404 status code. Our guide has a section on soft 404
hi @fabb, How does it work in your site? I tried set res.status = 404 in home.js getInitialProps, and then return , but it doesn’t redirect to _error.js, neither to 404.js.
Oh, it shouldn’t render error page anymore, cause it doesn’t an error page, need to use
status === 404? <ErrorPage />
:<Home>
, right?Find it, should use UI to render a 404 component