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 09 / Topic 5 of 5

Handling a product that doesn't exist

generateStaticParams lists Mandi Fresh's four real ids (g1–g4); a request for anything else is findProduct(id) returning undefined, handled with the same notFound() import from next/navigation Part 2 already introduced:

const product = findProduct(id);
if (!product) {
  notFound();
}

This is deliberately the simple half of the "map a missing read to a clean 404, throw for everything else" rule the reference curriculum names — Mandi Fresh's local data source can only ever return "found" or "not found," never a timeout or a malformed response, so there's nothing here to throw. A real upstream's error handling (mapping a 503 to a retry, a 401 to a redirect) is beyond what a local, simulated module can honestly demonstrate, and is flagged here rather than faked.

🔍 Going deeper — a genuinely surprising, fully reproduced finding: the HTTP status code for an unlisted id depends on whether this is the first request to that exact URL since the server started, or a later one. Verified against a real next build && next start, hitting a brand-new unlisted id (/mandi/products/zzz, never requested before):

$ curl -s -D - -o /dev/null http://localhost:PORT/mandi/products/zzz   # first request ever
HTTP/1.1 200 OK
$ grep -o 'NEXT_HTTP_ERROR_FALLBACK[^"]*' response-1.html
NEXT_HTTP_ERROR_FALLBACK;404
$ curl -s -D - -o /dev/null http://localhost:PORT/mandi/products/zzz   # second request, same id
HTTP/1.1 404 Not Found
$ curl -s -D - -o /dev/null http://localhost:PORT/mandi/products/zzz   # third request
HTTP/1.1 404 Not Found

The first response really is 200, carrying the <Suspense> fallback text (Loading product…) plus a <template data-dgst="NEXT_HTTP_ERROR_FALLBACK;404"> marker for the client to swap in the real not-found UI after hydration — because the response's status line is already committed the moment streaming starts, and at that moment Next doesn't yet know the eventual render will call notFound(). Every request after the first gets a clean, direct 404. This matches — and is explained by — the caching docs' own description of Incremental Static Regeneration for unknown dynamic params: "Any other URL is served the App Shell instantly, then upgraded in the background with its now-known params and cached for the next visitor." The "upgrade" here is Next.js resolving that this id will always 404, caching that fact, and serving the resolved status directly from then on. A crawler or monitoring script that only checks the very first hit against a brand-new path could see a false 200 — worth knowing, not worth panicking over, since Part 10 is where this course actually gets into what "cached for the next visitor" means.

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

Search the guide