Part 08 / Topic 4 of 7
How Next.js decides: the three request-time APIs, and params
A second page, harness/app/status/page.tsx, reads three different dynamic APIs side by side — searchParams, cookies(), headers() — each in its own component, each behind its own <Suspense> boundary, following the installed package's own "push dynamic access down" pattern from the streaming guide (Part 7 already used this for params; this page is where the other three show up):
async function SearchedPnr({ searchParamsPromise }: { searchParamsPromise: Promise<{ pnr?: string }> }) {
const { pnr } = await searchParamsPromise;
return <p id="searched-pnr">{pnr ? `You searched for PNR ${pnr}.` : "Enter a PNR above to check its status."}</p>;
}
async function PreferredLanguage() {
const store = await cookies();
const lang = store.get("lang")?.value ?? "en";
return <p id="preferred-language">Showing results in: {lang === "hi" ? "Hindi" : "English"}</p>;
}
async function RequestOrigin() {
const requestHeaders = await headers();
const userAgent = requestHeaders.get("user-agent") ?? "unknown";
return <p id="request-origin">Request user-agent: {userAgent.slice(0, 48)}</p>;
}
Each one proven to actually vary per request, against this course's harness:
$ curl -s "http://localhost:PORT/status?pnr=2458001234" | grep -o '<p id="searched-pnr">[^<]*'
<p id="searched-pnr">You searched for PNR 2458001234.
$ curl -s -H "Cookie: lang=hi" http://localhost:PORT/status | grep -o 'Hindi\|English'
Hindi
$ node -e "fetch('http://localhost:PORT/status').then(r=>r.text()).then(t=>console.log(t.match(/user-agent: ([^<]*)/)[1]))"
node
None of these three values exist until a specific request arrives — there is no build-time value for "what did this particular visitor search for," so Next.js has no choice but to treat anything that reads them as request-time work. That's the actual mechanism behind "how Next.js decides": it isn't a setting you flip, it's a consequence of which APIs a component's code happens to call. params is the one exception on this list — Part 2 and section 3 above both show it behaving like a build-time-knowable value whenever generateStaticParams lists it, and like these other three APIs whenever it isn't listed.
Full file: examples/03-pnr-search-dynamic-apis-page.tsx.
Done here? The practice set for Part 8 — including the solutions — is on the part page.