reuse built in error page nextjs

reuse built in error page nextjs

If you want to render the built-in error page you can by importing the Error component. The Error component also takes title as a property if you want to pass in a text message along with a statusCode. #javascript #webdev

Copy Snippet
import Error from 'next/error'
import fetch from 'node-fetch'

export async function getServerSideProps() {
  const res = await fetch('https://api.github.com/repos/zeit/next.js')
  const errorCode = res.ok ? false : res.statusCode
  const json = await res.json()

  return {
    props: { errorCode, stars: json.stargazers_count },
  }
}

export default function Page({ errorCode, stars }) {
  if (errorCode) {
    return <Error statusCode={errorCode} />
  }

  return <div>Next stars: {stars}</div>
}