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.