← Binderdex dev log

A session read defeated every cache

#web#infra#product

A tall stack of identical sheets being remade with a small red ticket on top, beside a single amber sheet resting on a shelf and being handed out again and again

Binderdex, my Pokémon card price and collection app, has public pages that declare revalidate, a Next.js setting that lets the server reuse a rendered page instead of rebuilding it each time. Most have declared it for months. Almost none of them had ever been cached. A single session lookup in the root layout silently overrode all of it.

The directive was decorative

Both the app layout and the marketing layout called the session API on every request. In Next.js that makes every page under them dynamic, and dynamic beats the revalidate a page declares. So each view of a public page paid for a session lookup and a fresh render. Field vitals, which are Chrome's speed numbers from real visitors, looked fine, with mobile LCP (how long the main content takes to appear) at 1.7 seconds at the 75th percentile, which is exactly why I had never chased it. Nothing was slow enough to feel broken. The site was doing far more work than it needed to on every request.

The plan went in as a checklist against Chrome's web guidance, ordered by impact, and most of it ran overnight.

BEFORE every public page Request Layout readsthe session Fresh render,every view revalidate is ignored AFTER card, sealed and pokedex pages Request In-processcache lookup Cached pagein 4 ms first request renders in 1.16 s
The pages and their revalidate setting did not change. Only the second path lets the cache answer.

A build that lied and a 500 that told the truth

Step one was making the layouts synchronous, so the marketing pages, the blog and the home page could prerender into a static shell. That went cleanly. The interesting failure came when I turned on caching for the card pages, the sealed product pages (booster boxes and the like) and the pokedex pages (one per Pokémon, listing its cards), roughly 100,000 card pages among them.

Two loaders fetched my own API through the request's host header. A build can't know a host header, so reading one makes a page dynamic at runtime. The production build passed and listed every route as static. Then the first real card request returned a 500: page changed from static to dynamic at runtime, reason: headers. Only running the built server showed it.

The loaders now read card details and search results in-process, through the same cache layer the API route already uses:

// the card page reads details directly, like the API route does
// (no fetch to our own host, so the render stays cacheable)
export async function getCardPage(slug: string) {
  return cacheAside(`card:${slug}`, () => loadCardDetails(slug));
}

// turning on render-once-then-cache for ~100k pages
export async function generateStaticParams() {
  return []; // first request renders and caches, the rest are hits
}

After that fix a card page is a miss in 1.16 seconds and a hit in 4 milliseconds. The cache is memory-only and capped at 128 MB per task, so it can't fill the disk. I first set the cap at 256 MB. Then I remembered that the web task also runs the image optimizer and had an out-of-memory kill in October, and I cut it in half. I'll raise it once I can see the task's memory graphs.

The rest of the night, in bytes

The checklist turned up inventory most sites carry without noticing, so I burned it down:

  • Fonts: six TTF files, 809 KB on disk, all six preloaded on every page. They're woff2 now at 287 KB after a lossless conversion. Four sans weights preload and the mono face loads on demand.
  • Scripts: two analytics scripts had returned 404 on every page load since the app left Vercel, and an animation library loaded just to fade eleven blocks in by 8 pixels. Both are gone. The reveal is a CSS scroll-driven animation behind feature detection, so a browser without it shows plain content.
  • Card images: every tile shipped a 180 KB JPEG because of an old decision to skip the image optimizer. Tiles now get a 25 KB WebP at the right size, with the first row eager and the rest lazy.
  • One interaction fix, measured before and after: ticking a card on a 216-tile set checklist re-rendered every tile, 100 to 257 ms of a 160 to 376 ms click. With the tile memoized, the same tick went from about 750 ms to about 160 ms on a 220-tile fixture set, and the tiles gained the aria-pressed state they lacked.

A few steps remain: some marketing callers and one shared image module to finish. The static shell and the 100,000 cacheable pages are what shipped.

Trust the built server

next build listing routes as static proves nothing about runtime, and the host-header trap sits in every codebase that fetches its own API. Healthy field vitals aren't evidence that a cache works either. I had a session lookup on every public page view and a 1.7-second LCP at the same time, and only one of those showed up on a dashboard.

This is the build log for Binderdex (www.binderdex.com) · All entries · Weekly essays