notFound
The notFound
function allows you to render the not-found file
within a route segment as well as inject a <meta name="robots" content="noindex" />
tag.
notFound()
Invoking the notFound()
function throws a NEXT_HTTP_ERROR_FALLBACK;404
error and terminates rendering of the route segment in which it was thrown. Specifying a not-found file allows you to gracefully handle such errors by rendering a Not Found UI within the segment.
app/user/[id]/page.js
import { notFound } from'next/navigation'asyncfunctionfetchUser(id) {constres=awaitfetch('https://...')if (!res.ok) returnundefinedreturnres.json()}exportdefaultasyncfunctionProfile({ params }) {const { id } =await paramsconstuser=awaitfetchUser(id)if (!user) {notFound() }// ...}
Good to know:
notFound()
does not require you to usereturn notFound()
due to using the TypeScriptnever
type.
Version History
Version | Changes |
---|---|
v13.0.0 | notFound introduced. |
Was this helpful?