Web font loading
Goal: text is readable immediately, the web font arrives early (one or two preloaded WOFF2 files at most), and the swap from fallback to web font does not move the layout.
Paths below are relative to this skill's folder (in Claude Code: ${CLAUDE_SKILL_DIR}).
When to use
- Lighthouse "Ensure text remains visible during webfont load", "Font display" insight
- FOIT (invisible text) or FOUT with a visible jump
- Self-hosting Google Fonts or Adobe Fonts alternatives, choosing weights, subsetting
- Text LCP with a large render delay
When not to use
- Fonts are fine and other CSS blocks rendering:
render-blocking-resources - Font files not cached:
caching-headers-review
Workflow
- Collect the HTML and the CSS that declares the fonts (theme stylesheet, Google
Fonts CSS):
curl -sL https://example.com/ -o page.html curl -sL https://example.com/css/site.css -o site.css - Run the audit (read-only):
python3 scripts/font_audit.py page.html site.css python3 scripts/font_audit.py page.html site.css --max-files 4 --json - Choose font-display per font:
- body text:
swap(always visible) oroptional(no swap after ~100 ms; best for CLS, the web font shows on the next page view from cache) - brand headings:
swapwith a metric-matched fallback - icon fonts:
blockis acceptable, but prefer inline SVG icons
- body text:
- Reduce files: keep 2-3 weights, or one variable font; WOFF2 only; subset to the
scripts the site uses (
unicode-rangesplits download only what a page needs). Commands in references/font-loading.md. - Self-host Google Fonts when possible: removes two origins (fonts.googleapis.com, fonts.gstatic.com) from the critical path. Download the WOFF2 files for the needed subsets and weights, write the @font-face rules (template in the reference), check the font license (Google Fonts are OFL/Apache and allow self-hosting).
- Preload only the 1-2 fonts used above the fold, with
type="font/woff2"andcrossorigin, using exactly the URL in @font-face. - Add metric overrides to a fallback face (
size-adjust,ascent-override,descent-override) so the swap does not shift text; values for common pairs and how to compute them are in references/font-loading.md. - Show the CSS/HTML diff, ask, apply, verify: re-run the script; in DevTools Network each font file should download once, early, with a 200 and long caching.
Output
Fonts: 4 @font-face rules, Google Fonts (Inter 6 weights), 1 preload
1. Preload brand.ttf has no crossorigin -> downloads twice [high]
2. "Brand Sans": no font-display, TTF/WOFF only -> WOFF2 + swap [medium]
3. Google Fonts without display=swap, 6 weights -> self-host Inter variable [medium]
4. Fallback "Brand Sans Fallback" with size-adjust: 104% to avoid swap shift [info]
Result: 2 font files (was 8), ~70 KB (was ~310 KB). CSS diff below, not applied yet.