Custom Error Page

404 Page

A 404 page may be accessed very often. Server-rendering an error page for every visit increases the load of the Next.js server. This can result in increased costs and slow experiences.

To avoid the above pitfalls, Next.js provides a static 404 page by default without having to add any additional files.

Customizing The 404 Page

To create a custom 404 page you can create a pages/404.js file. This file is statically generated at build time.

  1. // pages/404.js
  2. export default function Custom404() {
  3. return <h1>404 - Page Not Found</h1>
  4. }

500 Page

By default Next.js provides a 500 error page that matches the default 404 page’s style. This page is not statically optimized as it allows server-side errors to be reported. This is why 404 and 500 (other errors) are separated.

Customizing The Error Page

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:

  1. function Error({ statusCode }) {
  2. return (
  3. <p>
  4. {statusCode
  5. ? `An error ${statusCode} occurred on server`
  6. : 'An error occurred on client'}
  7. </p>
  8. )
  9. }
  10. Error.getInitialProps = ({ res, err }) => {
  11. const statusCode = res ? res.statusCode : err ? err.statusCode : 404
  12. return { statusCode }
  13. }
  14. export default Error

pages/_error.js is only used in production. In development you’ll get an error with the call stack to know where the error originated from.

Reusing the built-in error page

If you want to render the built-in error page you can by importing the Error component:

  1. import Error from 'next/error'
  2. export async function getServerSideProps() {
  3. const res = await fetch('https://api.github.com/repos/vercel/next.js')
  4. const errorCode = res.ok ? false : res.statusCode
  5. const json = await res.json()
  6. return {
  7. props: { errorCode, stars: json.stargazers_count },
  8. }
  9. }
  10. export default function Page({ errorCode, stars }) {
  11. if (errorCode) {
  12. return <Error statusCode={errorCode} />
  13. }
  14. return <div>Next stars: {stars}</div>
  15. }

The Error component also takes title as a property if you want to pass in a text message along with a statusCode.

If you have a custom Error component be sure to import that one instead. next/error exports the default component used by Next.js.