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.

Cannot return a 404 from getInitialProps anymore by throwing an error with code='ENOENT'

See original GitHub issue

Bug 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

  1. 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
    }
}
  1. 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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:20 (15 by maintainers)

github_iconTop GitHub Comments

2reactions
AVGPcommented, Mar 30, 2020

@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

1reaction
annewanghycommented, Apr 9, 2020

setting res.status = 404 in getInitialProps works fine

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?

so could I also use res.status = 404 in getInitialProps? (+ render the visual 404 component as part of the page with some helper props on the page).

Find it, should use UI to render a 404 component

Read more comments on GitHub >

github_iconTop 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 >

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