Missing resource pages
A database or API lookup returns no matching record.
Next.js 16+ · Directive
Use notFound() in the Next.js App Router to handle missing resources. It stops the current route segment and renders the nearest custom or default 404 UI.
The notFound function is a control-flow helper for missing resources in the App Router. Calling it throws a special 404 error, stops rendering the current route segment, and lets a nearby not-found file provide the response UI.
notFound()never; calling notFound() throws a special 404 error and does not return normally.
import { notFound } from 'next/navigation'
import { getPostBySlug } from '@/lib/posts'
export default async function Page({
params,
}: {
params: Promise<{ slug: string }>
}) {
const { slug } = await params
const post = getPostBySlug(slug)
if (!post) {
notFound()
}
return <div>{post.title}</div>
}Call notFound when the requested resource is missing; Next.js renders the nearest not-found UI for the route segment.
A database or API lookup returns no matching record.
A server endpoint must signal that a requested entity does not exist.
A nested route needs its own branded missing-content screen.
notFound() throws a special 404 error and ends rendering for the route segment where it runs. A matching not-found file supplies the segment's UI.
Follow-up: How can a route handler use it? It can call notFound() to return a 404 response to the caller.
Create not-found.js or not-found.tsx in the relevant route segment. When notFound() is invoked there, Next.js renders that boundary's UI.
Follow-up: What happens if the segment has no file? The nearest parent not-found boundary is used, otherwise Next.js falls back to its default 404 page.
The function is available in Server Components, Server Functions, and Route Handlers. It should be called on the render path when a resource lookup shows that the resource is absent.
Follow-up: What metadata accompanies the result? Next.js adds a robots noindex meta tag to prevent indexing.
not-found.jsglobal-not-found.jserror.jsRoute HandlersServer Components