Nextjs Fundamentals · Optimization & Metadata

Not-found UI

`notFound` / `not-found.tsx`

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.

  1. notFound() throws, and Next.js renders the nearest not-found.tsx.
  2. You import notFound from ___.
  3. A not-found.tsx component receives props describing the missing route.
  4. The root app/not-found.tsx also handles URLs that match no route at all.
  5. Next.js injects a robots meta tag with ___ for pages that return a 404.