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 4 of 9

Dynamic segments: [id]

A folder name wrapped in square brackets, [id], captures whatever the URL puts in that position. harness/app/store/products/[id]/page.tsx matches /store/products/p1, /store/products/p2, anything — the captured value arrives as id.

The one fact that trips up everyone coming from a synchronous router: in Next.js 16, params is a Promise, not a plain object. You must await it before reading anything off it.

import { notFound } from "next/navigation";
import { findProduct, products } from "../../lib/products";

export function generateStaticParams() {
  return products.map((product) => ({ id: product.id }));
}

export default async function ProductPage({ params }: PageProps<"/store/products/[id]">) {
  const { id } = await params;
  const product = findProduct(id);

  if (!product) {
    notFound();
  }

  return (
    <main style={{ fontFamily: "system-ui", padding: "2rem", maxWidth: 560 }}>
      <h1>{product.name}</h1>
      <p>Price: ₹{product.price.toLocaleString("en-IN")}</p>
      <p>Category: {product.category}</p>
      <p>Made in: {product.city}</p>
    </main>
  );
}

generateStaticParams is not this part's main topic (Stage 3 covers caching and prerendering in depth), but it earns one paragraph here because leaving it out breaks the build under this repo's verified toolchain. next.config.ts's cacheComponents: true (Part 1, section 8) does not just skip caching silently — it actively requires every dynamic route to say how its params are known. Reproduced for real by deleting the generateStaticParams function above and rebuilding:

$ npm run build
...
Error: Route "/store/products/[id]": Next.js encountered uncached or runtime data during prerendering.

`fetch(...)`, `cookies()`, `headers()`, `params`, `searchParams`, or `connection()` accessed outside of `<Suspense>` prevents the route from being prerendered, blocking the page load and leading to a slower user experience.

Ways to fix this:
  - [stream] Provide a placeholder with `<Suspense fallback={...}>` around the data access
  - [cache] For uncached data (`fetch`, database calls): cache the access with `"use cache"` (does not apply to `connection()`)
  - [block] Set `export const instant = false` to allow a blocking route

<Suspense> and "use cache" are Stage 2 and Stage 3 topics respectively — not yet taught. generateStaticParams sidesteps the whole problem for a storefront with a known catalog: list the ids you already know, Next.js prerenders exactly those at build time, and the route table above shows the proof — /store/products/p1, p2, p3 all come back ○ (Static), fully baked at build time, while the parameterized route itself shows ◐ (Partial Prerender) for any id outside that list, served at request time instead.

What happens for an id that is not in the list, like /store/products/p999? It is still served, just dynamically rather than at build time, and findProduct returns undefined, so notFound() runs (section 8 explains exactly what that does):

$ curl -s -o /dev/null -w "%{http_code}\n" http://localhost:PORT/store/products/p999
404

Full file: examples/03-product-detail-dynamic-segment-page.tsx.

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

Search the guide