SSR metadata and i18n gotcha
What this page is about: A real production outage where the site looked “down” for hours while the health API stayed fine. The failure was server-side metadata loading huge locale files — not the client language picker.
What went wrong (plain English)
Think of every page request as two doors:
- Door A — metadata (title, Open Graph, etc.): Next.js runs this before it can send the first byte of HTML.
- Door B — the page UI (header, home content, client React): runs after Door A.
We wired full localization into Door A: generateMetadata → getServerLL() → initIdlingI18nAsync(locale). That downloads/parses multi‑megabyte locale JSON on the Node server (often twice per request: root layout + page). With a non‑English cookie or slow chunk I/O, the server sent 0 bytes for 30+ seconds. The browser showed a blank hang; React never got a chance to run.
Meanwhile /api/test/health never opens Door A — so ops thought “the app is up” while every marketing page was dead.
Client-only fixes (provider bus, quote shards, Accept-Language, etc.) cannot fix a request that never starts streaming HTML.
Symptoms we saw
| Signal | Typical value | Meaning |
|---|---|---|
GET /api/test/health | 200, < 1s | App process healthy |
GET / | time_starttransfer=0, 0 bytes, 25–30s timeout | Stuck in metadata / SSR, not client |
Local next dev | Often “fine” | Dev bundles differ; prod code-splits locales |
| Client errors | “Connection closed”, re-render storms | After paint — separate from 0-byte TTFB |
Quick prod check (Windows):
curl.exe -sS -w "start=%{time_starttransfer}s total=%{time_total}s code=%{http_code}\n" -o NUL --max-time 15 https://idling.app/
curl.exe -sS -w "health code=%{http_code} time=%{time_total}s\n" -o NUL --max-time 10 https://idling.app/api/test/health
Healthy: home start and total are a few seconds, not start=0 with a timeout.
Root defects (do not reintroduce)
1. Async locale load in metadata (primary)
Bad (caused outage):
export async function generateMetadata()on rootlayout.tsxand hot routes (page.tsx, etc.)getServerLL()callingawait initIdlingI18nAsync(locale)orloadLocaleAsyncresolveServerIdlingLocale()usingAccept-Languageor locale cookie for metadata (any non-en→ huge import)
Good (current policy):
- Static metadata on shell routes:
export const metadata = { ...buildRootSiteMetadata(), ... }fromMETA_ENviaidlingMeta.ts getServerLL(): syncinitIdlingI18n('en')only — bundled English viaidling-sync-en.ts- Localized UI: client only via
IdlingLocaleProvider
2. Client provider blocking the shell (secondary)
Bad:
useEffect([localeId])that always runsloadLocaleAsync('en')+loadLocaleAsync(target)on every navigationresolveIdlingLocale()on mount (storage + navigator) then immediately settinglocaleIdbefore chunks load- Global
notify*/localeRevisionforcing full-tree remounts
Good: Sync English first paint; async locales only on picker or deferred localStorage (storage only, not navigator). See Site localization overview § Runtime.
3. Duplicate SSR auth() (minor)
Bad: Nav, NavAuth, and AuthAvatarServer each calling await auth() on every page.
Good: Nav calls auth() once and passes session into children.
Forbidden vs allowed (server metadata)
| Do not | Do instead |
|---|---|
async generateMetadata + getServerLL() on root layout / home | export const metadata + buildRootSiteMetadata() |
initIdlingI18nAsync in idling-server-i18n.ts | initIdlingI18n('en') sync |
| Cookie / Accept-Language for metadata locale | META_EN or sync English getServerLL() |
“Fix prod” by only changing IdlingLocaleProvider | Fix server metadata path first; verify TTFB |
Dynamic metadata is still allowed when it does not async-import locale shards (e.g. profile metadata that hits Postgres only on /profile/*).
Files that must stay aligned
| File | Contract |
|---|---|
layout.tsx | export const metadata — no async metadata |
page.tsx | export const metadata = buildHomePageMetadata() |
idling-server-i18n.ts | Sync English only |
idlingMeta.ts | buildRootSiteMetadata() uses META_EN, not async locale |
IdlingLocaleProvider.tsx | Nonblocking client orchestration |
Related merges (history)
Incident fixes landed across #365–#375. Partial client-only PRs left prod broken until static metadata + sync server getServerLL shipped (#375). Do not “revert to generateMetadata for translated titles” without an approved server i18n design that avoids loadLocaleAsync on the critical path.
When adding localized page titles later
- Keep shell metadata English/static.
- Prototype one route with measured TTFB under production-like build (
pnpm exec next build --webpack). - Never use
loadLocaleAsyncin root layout metadata. - Update this page and
.cursor/rules/i18n-ssr-metadata.mdcif the design changes.
Cross-links
- Site localization overview
- Message ticker (client-only quote shards)