Skip to content
Shahariar Niloy, home

Only the words a page needs

A multilingual Next.js app was sending its entire translation dictionary — thousands of strings — inside every page, to show a few hundred of them. I rebuilt how text reaches the browser.

Type
Engineering case study
Focus
Internationalisation and performance
Stack
Next.js App Router, React Server Components, TypeScript
Scale
Dozens of locales, thousands of strings

By the numbers

~90%less translation data in each page's HTML
~70%less over a typical five-page first visit
~90%less on every visit after that
1 yearhow long browsers can cache the full dictionary

The problem

Interactive components need their text on the very first render — on the server and again while React hydrates — or visitors see raw keys or a flash of English. The simplest way to guarantee that was to embed the whole dictionary in every page.

It worked, but it put tens of kilobytes of compressed text into every HTML response, before the page could render. No single page used more than a small fraction of it.

Before and after

Before

The whole dictionary, embedded in every page.

After

Just the page's own words. The rest arrives later, once, and stays cached.

How it works

  1. Find the words at build timeA generator reads the code with the TypeScript compiler, following every import — dynamic ones too — from each page and layout, and records which strings could be translation keys. A simple regex scan missed hundreds of keys; parsing the code didn't.
  2. Pick them per requestWhen a page renders, the server keeps only the candidates that really exist in that language's dictionary. Over-collecting at build time costs nothing: a string that isn't a key never reaches the page.
  3. Inherit instead of copyingEach page lists only the words its layouts don't already provide. In the browser, nested providers link to their parent through the prototype chain, so a missing key is found one level up without duplicating anything.
  4. Send the rest laterAfter the page has loaded and the browser is idle, the full dictionary arrives in the background and applies inside a React transition, so the page stays responsive. Its URL is content-hashed, so browsers cache it for a year and every country that shares a language shares one copy.

The mistake that taught me the most

My first version preloaded the full dictionary at low priority. On a throttled phone it still competed with the page’s largest image and pushed Largest Contentful Paint back by more than a second and a half. Moving it to after load, at the next idle moment, removed the cost completely.

Guardrails

The rest of the locale system

What I took from it

Measure what’s inside the HTML, not just the JavaScript bundle. The bytes that arrive before the page can render matter most — and they’re the easiest to forget.

Next highlightTaming memory