Next.js Discord

Discord Forum

How to return 404 status code from page (server component) or any other custom code?

Unanswered
Brown bear posted this in #help-forum
Open in Discord
Brown bearOP
I am using Next v15 and calling notFound() in page.tsx produces HTTP 200 status code while rendering 404 page. I expect to see 404 status code. How to accomplish that?

export default async function Page({
  params,
}: {
  params: Promise<{ modelId: string }>;
}) {
  const { modelId } = params;

  let model;
  try {
    model = await getModel({
      modelId,
    });
  } catch (error) {
    if (error instanceof TRPCError && error.code === "NOT_FOUND") {
      notFound();
    }
    throw error;
  }

  return (
    <PageComponent
      model={model}
    />
  );
}

0 Replies