Course map
Stage 1: App Router Foundations
Stage 2: The Rendering Model
Stage 3: Data, Caching and Mutations
Stage 4: Full-Stack Next.js
Stage 5: Production and Specialization

Part 02 / Topic 8 of 9

notFound(): the programmatic 404

Section 4's product page called a function, notFound(), imported from next/navigation, instead of returning some "not found" JSX itself. Reading directly from the installed package's own reference for that function: invoking notFound() throws a NEXT_HTTP_ERROR_FALLBACK;404 error and terminates rendering of the route segment where it was thrown. It is not a component you render — it is a function that throws, and because it throws, Next.js's own machinery catches it and swaps in the nearest not-found UI. With no custom not-found.tsx anywhere yet (that file is Part 3's topic), the "nearest" one is Next.js's own built-in default 404 page — the same page /store/products/p999 served in section 4, and the one every unmatched URL in this course has served since Part 1.

This course's own dev server confirms the exact exception Next.js throws internally, captured for real while testing /store/products/p999 in next dev:

Error: Route "/store/products/[id]": Could not validate `instant` because an error prevented the target segment from rendering.
    at ignore-listed frames {
  [cause]: Error: NEXT_HTTP_ERROR_FALLBACK;404
      at ProductPage (app/store/products/[id]/page.tsx:19:13)
    17 |
    18 |   if (!product) {
  > 19 |     notFound();
       |             ^
    20 |   }
  ...
  environmentName: 'Prefetch',
  digest: 'NEXT_HTTP_ERROR_FALLBACK;404'
}

That digest: 'NEXT_HTTP_ERROR_FALLBACK;404' is exactly the string the documentation names — this is not a bug in this harness, it is Next.js's internal signal working as designed, logged because next dev's prefetch check tries validating "would this navigation be instant" ahead of time (a Partial Prerendering diagnostic, Stage 2–3 territory) and finds a thrown 404 instead. The actual response your browser (or curl) receives is still a clean 404, as section 4 already showed.

notFound() only does anything useful inside a Server Component, a Server Function, or a Route Handler — the places code actually runs on the server, per the same reference. Calling it is enough; you never need return notFound(), because the throw itself stops execution right there, which is also why TypeScript narrows product to "definitely not undefined" on the line right after the check.

Done here? The practice set for Part 2 — including the solutions — is on the part page.

Search the guide