Not-found handling
What you need to know
Use notFound() when a requested resource is absent, rather than returning an empty screen or throwing a generic failure. Next.js stops the relevant rendering path and uses the appropriate not-found UI. A book with a valid-looking but unknown slug is a typical case.
By the end, you can: distinguish missing data and unexpected errors; check status before streaming.
app/catalog/[slug]/page.tsx
import { notFound } from "next/navigation";const books = new Map([["moss", "Moss Garden"], ["fern", "Fern Field"]]);export default async function Page({ params }: { params: Promise<{ slug: string }> }) { const { slug } = await params; const title = books.get(slug); if (title === undefined) notFound(); return <h1>{title}</h1>;}Expected behavior (not framework-tested): /catalog/moss is expected to show Moss Garden; /catalog/unknown enters the missing-resource UI. Map lookup avoids accidental inherited property matches.
How it works
notFound() is a framework control-flow operation that throws internally and terminates the current path. It is not a boolean or a JSX component, and a broad catch can accidentally intercept it. The framework supplies a noindex directive for this outcome. The HTTP status must be tested separately: a response already streaming may remain 200, even when the displayed UI indicates missing content. If an actual 404 is required, arrange the existence check before streaming begins; verify against the exact route tree and runtime.
app/catalog/[slug]/not-found.tsx
import Link from "next/link";export default function Missing() { return <section><h1>Book not found</h1><p>This title is not in our catalog.</p> <Link href="/catalog">Browse available books</Link></section>;}Expected behavior (not framework-tested): Visitors receive a useful next action instead of a blank page. The fallback itself does not establish the response status.
Common mistake
Do not convert every database outage to notFound(). Absence and dependency failure require different diagnosis. Avoid returning a normal page with the words “404” and assuming it sets the status.
Practice and verification
Test a known slug, a missing slug and a synthetic repository failure. Record status, body, noindex where applicable, and which fallback appeared. Repeat the missing case with and without a streaming ancestor.
Scope: App Router, proposed Next.js 16.x, Cache Components disabled for these labs. Snippets assume a reviewed TypeScript app and are static examples; framework behavior has not been executed. Exact supported patch and dependency set must be verified before running.
Further reading: Notfound · Notfound File · Loading