NextJS Tips Series: Custom 500 error page

Said BADAOUI
Jan 10, 2021

The 500 errors are handled both client-side and server-side by the Error component.

If you wish to override it, define the file pages/_error.js and add the following code.

// pages/_error.js
function Error({ statusCode }) {
return (
<p>
{statusCode
? `An error ${statusCode} occurred on server`
: 'An error occurred on client'}
</p>
)
}

Error.getInitialProps = ({ res, err }) => {
const statusCode = res ? res.statusCode : err ? err.statusCode : 404
return { statusCode }
}

export default Error;

Thank you :)

You can get full access to every story on Medium for just $5/month by signing up through this link.

--

--

Said BADAOUI
Said BADAOUI

Written by Said BADAOUI

Full-stack developer & passionate blogger, using technology to bring ideas to life and sharing knowledge through writing. Constantly learning & improving skills