Missing database or API resource
A dynamic page loads a record by slug or ID and the lookup returns no result.
Next.js 16+ · Function
The App Router's notFound function handles missing resources by throwing a framework 404 error. It stops rendering the current route segment and displays the nearest not-found UI.
Import notFound from next/navigation and invoke it during route rendering when the requested resource cannot be found. The call throws a special 404 error, stops rendering the current route segment, and lets the nearest not-found UI handle the response.
notFound(): nevernever
import { notFound } from 'next/navigation'
async function getPost(slug: string) {
const response = await fetch(`https://example.com/api/posts/${slug}`)
return response.ok ? response.json() : null
}
export default async function Page({
params,
}: {
params: Promise<{ slug: string }>
}) {
const { slug } = await params
const post = await getPost(slug)
if (!post) notFound()
return <article><h1>{post.title}</h1></article>
}Calls notFound when the requested post is unavailable, allowing the matching not-found boundary to render.
A dynamic page loads a record by slug or ID and the lookup returns no result.
A Route Handler cannot resolve the requested entity.
A segment needs a tailored missing-resource screen.
notFound is a function imported from next/navigation that throws a special 404 error. It terminates rendering for the route segment where it is called, and the nearest not-found boundary supplies the UI.
Follow-up: How does it affect indexing? It causes Next.js to add a robots noindex meta tag, preventing the not-found response from being indexed.
Use it in the render path after loading a resource, such as when a database lookup returns no record. It works in Server Components, Server Functions, and Route Handlers.
Follow-up: What happens in a Route Handler? Calling it there serves a 404 response to the caller.
A route segment can provide a not-found file to customize the UI shown after notFound is called. Without a local file, the nearest parent boundary is used, eventually falling back to Next.js's default 404 page.
Follow-up: Does notFound need to be returned? No; it throws and stops execution, so a plain invocation is enough.
not-found.jsglobal-not-found.jserror.jsRoute HandlersServer Components