Preload web fonts to avoid flash
A late-loading webfont either hides text (FOIT) or swaps it (FOUT); both are visible jank. Preloading the file and matching the fallback's metrics removes most of it.
Procedure
- Self-host the font rather than calling a third-party CDN at runtime — it removes a round trip, an extra DNS/TLS handshake, and a privacy leak.
- Subset to the characters you actually ship (Latin, or Latin + the accents in use) with
glyphhangerorfonttools: pyftsubset Inter.woff2 --unicodes="U+0000-00FF,U+2000-206F" --flavor=woff2 --output-file=inter-subset.woff2 - Preload only the font used above the fold, and only the one weight/variant on the critical path. Preloading every weight competes with the CSS and images for bandwidth:
The
crossoriginattribute is required even for same-origin fonts; omit it and the browser fetches twice. - Choose
font-displaydeliberately: font-display: swap; /* text visible immediately in fallback, then swaps / font-display: optional;/ no swap if slow — best for near-zero CLS */ Avoidblock(up to 3 s invisible) andauto. - Match the fallback's metrics so the swap does not reflow. Tune with
size-adjust,ascent-overrideand friends: @font-face { font-family: "Inter Fallback"; src: local("Arial"); size-adjust: 107%; ascent-override: 90%; } body { font-family: Inter, "Inter Fallback", system-ui, sans-serif; } - Set
font-synthesis: noneif you ship the true italic/bold, so a missing weight does not get a smeared fake. - Use a variable font file for multiple weights to cut requests from four to one; weigh the larger single file against the savings.
- Re-check Cumulative Layout Shift after the change — a well-tuned swap should contribute under 0.02.
Pitfalls
preloadwithoutcrossorigin, causing a duplicate download and a worse waterfall than no preload at all.- Preloading four weights and three icon fonts, which starves the actual content and delays first paint.
font-display: blockon icon fonts so glyphs pop in late and shift every row.- Subsetting to the visible characters on the page, so a user typing an accent into a form sees tofu.
Verification
# Confirm the preload and the fallback declaration exist together:
grep -n 'rel="preload".*font' index.html
grep -n 'size-adjust\|font-display' src/styles/*.css
# And measure CLS after load:
npx lighthouse http://localhost:3000 --only-categories=performance --output=json | \
node -e "let d='';process.stdin.on('data',c=>d+=c).on('end',()=>console.log(JSON.parse(d).audits['cumulative-layout-shift'].numericValue))"
CLS below 0.1 (font contribution under ~0.02), one preloaded font on the critical path, font-display set explicitly. Report the subset size, the display mode, and the measured CLS.