created: 2020-11-20T23:49:54.000Z

nextjs で 404 を返す方法

nextjs の SSR で 404 を返すには、getServerSideProps でこうする

const getServerSideProps: GetServerSideProps = async (context) => {
  context.res.statusCode = 404;
  return { props: { errorCode: 404 } };
};

あとはコンポーネント側でよしなに 404 ページをレンダリングすればよい

とりあえず動かす段階なら next ビルトインの Error ページを使ってもいいだろう

参考

数え方の辞典
[ad] 数え方の辞典
飯田 朝子, 町田 健 (単行本)