Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

314
Views
Nextjs: custom error page with correct status code

I followed the next docs regarding a custom error page. I want to reuse the error page if certain errors occur at getStaticProps. Something like:

const Page: NextPage<Props> = ({ pageData, error }: Props) => {
  if (error) return <Error statusCode={500} />;
  return <Page data={pageData} />;
};

export const getStaticProps: GetStaticProps = async ({
  params,
}: {
  params: { [prop: string]: string[] };
}) => {
  const { slug } = params;
  const {pageData, error} = getPageData(slug)

  return {
    props: {
      pageData: page || null,
      error: error || null,
    },
  };
};

export default Page;

The error page is just like in the docs:

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;

This works but the status code ist wrong. Nextjs still answers a request to this page with a status code 200. I need it to set the status code to 500 as if there was a server error.

enter image description here

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!