Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger nextjs default 500 error page

I am unable to trigger the Next.js's 500 error page. Most resources talks about making a custom error page but the Next.js doc briefly mentions their default 500 error page. I would like to trigger this default page when the API responds with a 500 status.

Given an API response of 500 Next.js should display a 500 error page. So far it returns to the page where the request was made.

return res.status(500).end();
like image 469
Dan Avatar asked Feb 07 '26 18:02

Dan


1 Answers

The only solution I found is to throw an unhandled error from getServerSideProps:

res.statusCode = 500;
throw new Error('Internal Server Error');

I saw that NextJs handle this as a 500 status code, but only on the production/staging environment. npm run dev will not work, and you will not be able to see the default 500 page.

I needed to throw a 500 status code to prevent Google from deindexing my page because the page content suffered significant changes, and the products from this page were not available anymore.

like image 139
Ionut Enache Avatar answered Feb 09 '26 12:02

Ionut Enache



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!