Skip to content
Book a call

Reading one cookie makes the whole page dynamic

· 3-minute read · Engineering · by Direct Softworks

Four content pages were being rebuilt from scratch on every request to read a cookie that nothing had written in a year. Nothing warns you, and the cost is invisible until you look.

There is a category of performance problem that no profiler finds, because nothing is slow. The work is simply being done far more often than it needs to be, and the only symptom is a number in a build log that nobody reads.

What happened

Four pages on this site read a cookie called siteLang to decide which language variant of the content to serve.

The cookie had not been written by anything for about a year. The language chooser had been removed, the site was English only, and the first line of the i18n module said so. The read always returned the same value.

But calling cookies() in a Next.js server component is a dynamic API. Touching it tells the framework that this page depends on the incoming request, so it cannot be prerendered and must be rendered per request, forever.

Four content pages, rebuilt from scratch on every single visit, to read a value that could not change.

Why the cost is invisible

Nobody notices because nothing looks wrong. The pages are fast enough. There is no error. The only place it shows is the build output, where a page is marked as server-rendered on demand rather than static, and that mark looks like a decision somebody made rather than a consequence they did not intend.

The cost is real, though, and it compounds in three places:

Time to first byte on every request, because there is no cached HTML to send.

Database load, because each render re-queries. On a small box with a connection pool of three, that matters more than it would elsewhere.

Crawl budget. A slower response to a crawler on a site with little authority is not free, and Googlebot's own crawl-stats report measures average response time explicitly.

The other half of the problem

There was a second issue hiding inside the first, and it was worse than the performance.

A URL whose content varies by cookie is a URL that serves different things to different visitors without changing address. For a search engine that is, at the margins, a cloaking signal. Googlebot does not carry cookies, so it would only ever see one variant, and any content published under the other variant would be invisible to search while appearing perfectly fine to anyone who tested it in a browser with the cookie set.

That is a nasty shape of bug: it produces content that exists, renders, and cannot be found, with no error anywhere.

The fix

Delete the read. The value became a constant in one module, imported by all four pages, with a comment explaining what the cookie used to do and why a language variant now needs a URL rather than a cookie.

Then the pages became statically renderable again, and each got a revalidation window so that content published from the panel still appears without a deploy.

That last part is not optional and it is easy to miss. Removing the dynamic API makes the page static, which means it is rendered once, at build time. Our build runs in CI where the database is unreachable, so the prerender is empty. Without a revalidate window, the page would have shipped empty and stayed empty until the next deploy, which is a considerably worse bug than the one being fixed.

How to find yours

Look at the build output. Next prints a symbol per route: static, prerendered with params, or dynamic. Go through the dynamic ones and ask, for each, why. Some genuinely must be. The interesting ones are those where nobody can say.

Then search the codebase for the dynamic APIs: cookies(), headers(), searchParams, connection(). Every call site is a page that cannot be cached, and each deserves the question of whether the value it reads actually varies.

The general lesson is one about defaults rather than about Next. A framework will not tell you that you have opted out of an optimisation. It will quietly do the more expensive correct thing, forever, and the only way to notice is to periodically read what the build says about your own application and be surprised by it.

More notes