A Next.js App Router build has two execution contexts that look identical from inside a helper function and behave nothing alike. One of them has a request. The other does not. A helper that reads a cookie works perfectly in the first and throws in the second, and because prerendering happens in the second, the throw takes the whole build with it.
This is the writeup of that failure on this site's repository: what the helper did, why the failure was total rather than partial, and what the fix actually changed.
The helper
The preview integration needs a Cosmic client that knows whether the reader is in preview mode. The signal is an httpOnly cosmic_preview cookie set by middleware. So the helper read the cookie, and if a token was present, passed it into the SDK client:
export function getCosmic() {
const token = cookies().get('cosmic_preview')?.value
return createBucketClient({
bucketSlug: process.env.COSMIC_BUCKET_SLUG!,
readKey: process.env.COSMIC_READ_KEY!,
...(token ? { previewToken: token } : {}),
})
}
That is correct code for a request. cookies() resolves against the request store, the token is either there or it is not, and the client is built accordingly.
The problem is that getCosmic() is not only called during requests. It is the only client factory in the codebase, so every data helper goes through it, including getAllArticles(). And getAllArticles() is called from generateStaticParams() in the article route and from sitemap.ts. Both of those run at build time, where there is no request and therefore no request store for cookies() to resolve against. It does not return empty. It throws.
{{preview-cookie-call-paths /}}
The two columns are the same three frames. The only difference is what is at the top, and that difference is invisible from inside the helper.
Why nothing survived
The instinct with a build error is to look for the page that broke. There was no such page. Every route that gets prerendered reaches the database through the same factory, so the failure was not distributed across some routes and not others. It was one call site, shared by all of them.
{{preview-cookie-blast-radius /}}
A full bar here is the bad outcome, which is worth saying out loud because bar charts usually read the other way. Nothing partial got through. The build aborted before emitting output, so there was no degraded site to fall back to, and the first deployment of the project never produced anything servable.
The fix
The wrong fix is to thread a flag through every caller so that build-time paths ask for a non-preview client explicitly. That spreads knowledge of preview mode into sitemap.ts, which has no business knowing about it, and it breaks again the next time someone adds a prerendered route.
The right fix is for the helper to treat the absence of a request as a normal condition rather than an error, because that is what it is. During a build, there is no reader, so there is no preview mode, so the correct client is the plain one:
function readPreviewToken(): string | undefined {
try {
return cookies().get('cosmic_preview')?.value
} catch {
return undefined
}
}
The catch is not swallowing a bug. The throw is Next.js reporting a fact about the execution context, and the fact has an unambiguous interpretation: no request means no preview. Inside a real request the behaviour is byte-for-byte unchanged, which is the property that makes this safe to merge without retesting the preview flow.
{{preview-cookie-deploy-sequence /}}
One commit, one guarded read, red to green.
What generalises
The lesson is not about cookies. It is that App Router request APIs are ambient rather than passed in, so a function's dependency on a request does not appear in its signature. getCosmic() takes no arguments and returns a client. Nothing about that type tells you it can only be called during a request, and nothing stops a build-time caller from reaching it through three layers of perfectly reasonable data helpers.
Two habits fall out of that:
Any helper reachable from generateStaticParams, generateMetadata, sitemap, or a statically rendered segment has to tolerate the absence of a request, because you cannot see from the call site whether one exists. And the tolerance belongs at the bottom, in the function that touches the ambient API, not spread across every caller that might someday be prerendered.
The cost of getting this wrong is not proportional to the size of the mistake. One unguarded line failed 98 pages, because shared infrastructure fails with the blast radius of the thing that shares it.
More news

September 10, 2026
Stripe Treasury | Business Account to Unify Your Finances
Open a financial account with Stripe Treasury to convert currencies, send payouts, and manage spending. No fees, no minimum balance.

September 8, 2026
Stripe helps Muse, Meta’s new personal AI agent, shop across the internet with Link
Stripe announced that Meta has integrated Link’s wallet for agents with Muse, its new personal AI agent

August 25, 2026