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

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?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 2
  • Comments: 20 (15 by maintainers)

Most upvoted 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

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

@estevecastells You are aware that what I’m saying is based on what actually happens in Googlebot, right? It’s not “politeness” that we have this in our official guidance. Having a method to give the proper HTTP status is great but in situations where it isn’t feasible we deal with that. Don’t create FUD.

As @timneutkens said: There are ways to generate proper 404s. This issue is about a specific situation where that isn’t supported apparently and for that, we have other ways to deal with it in Googlebot.

Just FYI, asked Martin to weight in on this one, he’s a developer advocate at Google.

@timneutkens This would cause the page to be rendered with a 200, I would need to research if 200 + noindex would be equally good for SEO as 404. I doubt it.

Note that throwing a ENOENT error was never supported. You should render the 404 page as a state of your UI with the correct robots tag. eg like: https://github.com/zeit/next.js/discussions/10960#discussioncomment-1201