Skip to content

_error.js getInitialProps is not called for an error thrown at the top level of a module #8592

@WestonThayer

Description

@WestonThayer

Bug report

Describe the bug

Error.getInitialProps is not called when an exception is thrown at the top level of a page while it is being loaded. This is problematic because getInitialProps would have provided access to the Error object. I want the Error so that I can log it and potentially display it to the user in render().

To Reproduce

I've created a minimal reproduction here: https://github.com/WestonThayer/bug-nextjs-error-getinitialprops

Expected behavior

The Error that was thrown should be made available to _error.js. I would expect to receive it via getInitialProps, but I supposed having it as a prop passed in to render would work too.

Additional context

I think this was introduced by #4764, specifically the lines that use the props from the server. I can see how that makes sense if the Error was thrown on the server side and _error.js's getInitialProps ran on the server, but it doesn't account for code that works fine on the server, but throws on the client.

As a workaround, the exception is passed as a prop to App, so I'm simply passing it on:

class MyApp extends App {
  render() {
    const { Component, pageProps, err } = this.props;
    return <Component {...pageProps} err={err} />;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Linking and NavigatingRelated to Next.js linking (e.g., <Link>) and navigation.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions