next.js: Endless loop when _error render throws on client side

Bug report

Describe the bug

When using a custom _error page, Next can enter an infinite loop if _error render throws on client side (with a production build).

To Reproduce

Repro here : https://github.com/dorian-marchal/next-repro/tree/repro-6973

In a terminal:

$ git clone -b repro-6973 https://github.com/dorian-marchal/next-repro
Cloning into 'next-repro'...

$ cd next-repro/

$ node -v
v10.14.1

$ npm install

$ npm ls next
next-repro@1.0.0 /tmp/next-repro
└── next@8.0.4

$ npm run build && npm run start
...
> Ready on http://localhost:3000

In your browser:

  • Go to http://localhost:3000/
  • Open your browser devtools
  • Click on “Client side nav”
  • See the app entering an endless loop (stopped after 20 iterations).

Expected behavior

On client side, if _error throws, I expect an “Internal Error” page (this is what happens when _error throw during server side rendering).

I suppose that Next could force a page reload in this case.

System information

  • OS: Ubuntu 18.04
  • Version of Next.js: 8.0.4

Additional context

In our project, this error happens when _error getInitialProps fails to retrieve some dependencies (Error.render tries to access an undefined property).

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 16
  • Comments: 21 (5 by maintainers)

Most upvoted comments

Hi guy 👋

I did a bit of digging in the next.js/packages/next code and found that:

In this renderError function we can see why it does not crash in dev mode because it let the HMR handle the error. But in production what happens is that we go past that if, and load the “_error” page and re-render it right away.

There you have the error loop. If the error is thrown in a custom _error page, we reload it, re-render it, it throws again, we load it, etc. you get the idea.

One solution would be to check if we are on the _error page, then avoid the subsequent pageLoad calls and/or show another, non-customizable, error page (fallback on the next _error page maybe with a nice error message in the console ?) Unfortunately, as we are above the RouterProvider, I don’t see a reliable way to know if the currently rendered page is /_error or not. Or maybe by calling the getRouter().pathname in renderError, as it is a singleton, but this gives me goosebumps.

I’d love to address this issue if the above “solution” is a good lead, or any other better solution.

Hope it helps, Cheers

Happens in 10.0.5 in production (deployed to Vercel) when calling .includes() on a variable that’s missing server-side data.

I actually don’t have a custom /_error page, but this was the only existing issue that resembles what I’m experiencing. It matches in every way except for the use of custom /_error.

Well it’s not super worrisome, you’re customizing the error page and introducing another error (runtime error) into it, which is more like a pretty big edge case. We’ll fix it though (or you’re welcome to open a PR).

next version: 12.0.2 node version: 17.0.1 On client-side rendering when routing happens, and the page has some error then _error page goes into an infinite loop.

v9.4.2, still happens.

in Next 10.0.1 is still happens.

v10.0.1, still happens.