Not-found UI
Not every URL maps to real content. When a resource is missing, call notFound()
from next/navigation:
import { notFound } from 'next/navigation';
export default async function Page({ params }: { params: Promise<{ slug: string }> }) {
const { slug } = await params;
const post = getPost(slug);
if (!post) notFound();
return <div>{post.title}</div>;
}
notFound() throws, and Next.js unwinds to the nearest not-found.tsx
boundary, rendering its default export instead. Place a not-found.tsx in a
route segment to scope it; the root app/not-found.tsx also handles any URL that
matches no route at all.
import Link from 'next/link';
export default function NotFound() {
return (
<div>
<h2>Not Found</h2>
<Link href="/">Return Home</Link>
</div>
);
}
A not-found component receives no props. Before streaming starts Next.js
returns a 404 status; after streaming starts it keeps 200 and renders the UI
inline. Next.js injects <meta name="robots" content="noindex" /> for 404s.
In short: notFound() throws to the nearest not-found.tsx, which renders your
404 UI.
You are about to answer a short quiz on notFound / not-found.tsx.