Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error component rendered 2 times when server rendering in production #2573

Closed
timneutkens opened this issue Jul 16, 2017 · 5 comments
Closed
Assignees

Comments

@timneutkens
Copy link
Member

timneutkens commented Jul 16, 2017

Expected Behavior

1 error component is rendered

Current Behavior

2 error components are rendered below eachother. 1 through SSR and 1 through client side rendering.

Steps to Reproduce (for bugs)

import Link from 'next/link'

export default class extends React.Component {
  static async getInitialProps() {
   // Just throw an error in getInitialProps
    throw new Error('test');
  }
  render() {
    return (
      <div>
        <h1>Hello world</h1>
      </div>
    )
  }
}

Context

Issue can be seen here: https://create-next-example-app-olyuogqcxe.now.sh/ (deployment with the above code). Scroll down cause the component itself is screen filling 😅

@harshmaur surfaced this.

Your Environment

Tech Version
next 2.x+ (so beta too)
mode production
@stevegeek
Copy link

stevegeek commented Aug 11, 2017

@timneutkens I think I have run into this too. Any update on a fix?

Also I am wondering why here https://github.com/zeit/next.js/blob/v3-beta/server/render.js#L223 the error object in production is converted into a simply object containing only a message regardless of the error , thus the client sees this 'err' object. However if my custom error page renders differently depending on the error and say its status code this line masks the true error and makes everything into an unhandled exception.

For example if I simply want to render a 404 page for a resource that doesnt exist, in my application code i can throw an Error containing a status code to indicate this and handle that in _error page, the server will render correctly as it has the 'true' Error object, however, on the client the serialised object will be this masked object.

What is the recommended way to do error handling in next.js with a custom server?

If I handle the error at the page rendering level (wrap the getInitialProps or render method in a try/catch then render a '404 Not Found' page , what can I then do to make the response from the server have a 404 status code, as in this case for all intents and purposes the page has rendered correctly and results in a 200 status code. https://github.com/zeit/next.js/blob/v3-beta/server/render.js#L193.

(Edit: actually I just realised in the above case I can set res.statusCode inside getInitialProps when on the server)

Thanks for any advice!

@coluccini
Copy link
Contributor

@timneutkens, the example is no longer rendering the error page 2 times, did you solved it? Cause I'm seeing this issue as well and, cause you can only see it in prod, I'm having hard times to find the root issue…

@kochis
Copy link

kochis commented Sep 25, 2017

We are also seeing this behavior whenever an un-caught exception is thrown on the server (404's behave as expected). The server renders the error page as expected, but the client also renders the error page.

My guess at what is happening, is that the server error is somehow being surfaced to the client, which causes the next handling of client-runtime errors to also be rendered (the one that gets rendered to __next-error), which is why you see it below the original error.

Does __next-error assume the content in __next is always empty? Because I think that is not the case when the error page is rendered on the server.

I've created an example that replicates the issue here: https://github.com/kochis/next.js/tree/duplicate-error-example/examples/duplicate-error-pages

If there is also a more appropriate way to handle the error to avoid this, any help would be appreciated.

@kochis
Copy link

kochis commented Sep 26, 2017

I believe this was introduced in #1800

And the problem code is here:
https://github.com/zeit/next.js/blob/v3-beta/server/render.js#L114

If there is an error on the server, it's sent to the client through the err prop, which is what the client is using to determine whether it should render the error page: https://github.com/zeit/next.js/blob/master/client/index.js#L96

So it seems like any uncaught exception will render both server & client error pages. I'm not sure what the correct behavior for this should be, but one solution I could think of would be to add .ignore to the error object created server-side.

@lock
Copy link

lock bot commented May 10, 2018

This thread has been automatically locked because it has not had recent activity. Please open a new issue for related bugs and link to relevant comments in this thread.

@lock lock bot locked as resolved and limited conversation to collaborators May 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants