Dynamic APIs: cookies, headers, and searchParams
Request-time data is async in Next.js 16. How cookies(), headers(), params and searchParams work, why they need Suspense, and how to feed cached code.
Dynamic APIs: cookies, headers, and searchParams
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- cookies(), headers(), params, and searchParams are request-time APIs: their values exist only when a request arrives, so they cannot be known at build time.
- In Next.js 16 all of them are asynchronous; synchronous access from the Next.js 15 compatibility period has been removed, so you await them.
- With Cache Components, reading a request-time API outside a Suspense boundary fails the build; inside a boundary, only that subtree streams at request time.
- Without Cache Components, reading cookies() or headers() made the whole route dynamic, which is the older behavior most tutorials describe.
- Pass the promise down and await it deep in the tree, and use connection() before random or time-based code that must differ per request.
Data that only exists during a request
Most of a page can be worked out ahead of time: markup, imports, even database rows. Some values cannot, because they depend on who is asking.
cookies()reads the request’s cookies.headers()reads its headers.searchParamsis the query string.paramsholds a dynamic segment’s value, such asslugin/blog/[slug]. If you list the value ingenerateStaticParams, it is known at build time. Otherwise it is request data.
The docs group these as request-time APIs. If a component touches one, that component cannot be finished during the build.
They are all async now
In Next.js 14 these were synchronous. Version 15 made them async with a temporary compatibility shim. Starting with 16, synchronous access is removed, so you await each of them:
import { cookies, headers } from 'next/headers'
export default async function Page({
params,
searchParams,
}: PageProps<'/products/[id]'>) {
const { id } = await params
const { sort } = await searchParams
const theme = (await cookies()).get('theme')?.value ?? 'light'
const agent = (await headers()).get('user-agent')
// ...
}
PageProps<'/products/[id]'> is a global type helper generated during next dev, next build, or next typegen. There is a codemod for old code: the Version 16 upgrade guide gives npx @next/codemod@canary next-async-request-api . for any leftover synchronous access.
The async shape is a design decision, not a nuisance. An await is a place where rendering can pause. That gives the framework somewhere to stop prerendering and wait for the request.
What it does to the page: two models
Without Cache Components (the older model): reading cookies() or headers() anywhere in a layout or page opts the whole route into dynamic rendering. One cookie read and the entire route is rendered per request. I confirmed that with cacheComponents off on 16.4.6: a page that read cookies() built as ƒ (dynamic) while a plain page was ○ (static).
With Cache Components: the same read stops prerendering at that point only. Everything outside it stays in the static shell, and you must give the request-time part a <Suspense> boundary. Without one, next build fails with the blocking-route error: “Next.js encountered uncached or runtime data during prerendering”.
export default function Page() {
return (
<>
<h1>Dashboard</h1>
<Suspense fallback={<p>Loading…</p>}>
<UserGreeting />
</Suspense>
</>
)
}
Push the await down
The deeper you read the value, the more of the page stays static. A layout that does const { slug } = await params at the top makes the whole layout request-bound. The docs’ suggestion is to pass the params or searchParams promise down to a component inside <Suspense> and await it there:
function Results({ searchParams }: Pick<PageProps<'/search'>, 'searchParams'>) { /* awaits inside */ }
export default function Page(props: PageProps<'/search'>) {
return (
<Suspense fallback={<p>Loading results…</p>}>
<Results searchParams={props.searchParams} />
</Suspense>
)
}
Caching code that depends on a request
A use cache scope cannot read cookies() or headers() itself, as the previous lessons showed. The pattern is to read outside and pass a value in; it becomes part of the cache key. Rarely, for compliance or when you cannot refactor, there is a use cache: private variant.
Per-request values without request data
Some code differs on every call but reads no request API: Math.random(), Date.now(), crypto.randomUUID(). Under Cache Components, prerendering would freeze them, so Next.js makes you decide: cache the value so all users share it, or call await connection() first and wrap the component in <Suspense> so it runs per request.
Writing cookies
You read cookies in a render, but you set and delete them in a Server Action or Route Handler. Setting or deleting a cookie in a Server Action re-renders the current page so the UI reflects it.
What the video covers
Codevolution’s tutorial explains dynamic rendering (per-request rendering for personalized or request-time data), implements it by using cookies(), and inspects the build output. The chapters stop there: headers() and searchParams are not part of this one, so the lesson and the two runner-ups cover them. It was recorded for Next.js 15, before Cache Components, and describes the older model where a cookie read makes the whole route dynamic.
Where this fits
You now know which data cannot be cached ahead of time. Not all fetching has to happen on the server, though. Next: client-side data fetching.
Check yourself
Why are cookies() and headers() asynchronous in Next.js 16?
Their values only exist once a request arrives, so reading them is a point where prerendering must stop, and an async API lets the framework suspend there and keep the rest of the route static.
What is the difference in how cookies() affects a page with and without Cache Components?
Without Cache Components, reading cookies() anywhere in a layout or page makes the whole route dynamic, while with Cache Components only the Suspense-wrapped subtree that reads it streams at request time and the rest is still prerendered.
How can a cached component use a session id from a cookie?
A component outside the cache reads the cookie and passes the value as an argument, since a use cache scope cannot call cookies() itself, and the argument becomes part of the cache key.
When do you reach for connection()?
Before Math.random(), Date.now(), crypto.randomUUID(), or a synchronous database query when the value must differ for each request, wrapped in Suspense.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.