Skip to content

Self-host default fonts and inline only the ones a site uses (RND-12524) - #4522

Draft
nolannbiron wants to merge 3 commits into
mainfrom
nolann/rnd-12524-self-host-default-fonts
Draft

Self-host default fonts and inline only the ones a site uses (RND-12524)#4522
nolannbiron wants to merge 3 commits into
mainfrom
nolann/rnd-12524-self-host-default-fonts

Conversation

@nolannbiron

@nolannbiron nolannbiron commented Aug 19, 2026

Copy link
Copy Markdown
Member

Supersedes #4519, rebased standalone on main (no longer stacked on #4518, which measured no paint improvement on its own and stays closed).

Every page currently ships 3 render-blocking stylesheets containing 507 @font-face rules for all 23 default font families, while a site uses at most two. On production (cross-origin static-2v.gitbook.com), the font CSS → woff2 chain is the largest render-blocking item Lighthouse reports (794 ms of ~1,300 ms on mariadb.com/docs), gating the text LCP element.

Changes:

  • scripts/generate-font-faces.ts generates a per-family manifest from the Google Fonts CSS API (same UA as next/font — byte-identical faces, verified 491/491), committed as src/fonts/generated/
  • scripts/download-fonts.ts (wired into generate:assets) downloads the woff2 files to public/~gitbook/static/fonts/, served first-party via getAssetURL
  • src/fonts/default.ts replaces the 23 next/font/google calls with the generated manifest; CustomizationRootLayout inlines the @font-face rules for the site's content + mono fonts and emoji only
  • Fallback font metrics (size-adjust/ascent-override) preserved via next/dist/server/font-utils, so no CLS regression

Measured on preview (/url/mariadb.com/docs): Lighthouse's modeled render-blocking savings drop from 400–500 ms (baseline, n=13) to 160–330 ms (n=7). Same-origin previews cannot reproduce the production topology, where the removed font-CSS → woff2 chain is the largest render-blocking item (794 ms of ~1,300 ms); production impact to be measured after merge.

Note: font assets 404 on aliased *.workers.dev preview hosts because getAssetURL resolves against the deployment's configured GITBOOK_URL — same as existing generated assets (icons, math, scalar); validate on the Vercel preview or the canonical 2c preview URL.

`src/fonts/default.ts` instantiated 23 `next/font` families in one module, so the
507 `@font-face` rules for all of them shipped to every page across two or three
render-blocking stylesheets (195 KB) — for the two families a site actually uses.

Resolve the faces from the Google Fonts CSS API at generate time, vendor the
woff2 files under `~gitbook/static/fonts`, and inline the picked families'
rules in the head, reusing the path custom fonts already took. The generated
data reproduces next/font's 491 faces and all 16 metric-adjusted fallback faces
exactly, so rendering and CLS are unchanged.

Published pages are now down to a single render-blocking stylesheet, plus ~3 KB
compressed of inline font rules. Builds also no longer depend on Google Fonts.
The unicode-ranges were repeated identically on every weight of a family, and
sources.json held 220 full URLs where 24 per-family prefixes cover them.

226 KB -> 106 KB committed, and the runtime JSON that ends up in the server
bundle drops from 190 KB to 90 KB. The emitted faces are unchanged: same 491
faces, same fallback faces, same CSS variable values.
@linear-code

linear-code Bot commented Aug 19, 2026

Copy link
Copy Markdown

RND-12524

@changeset-bot

changeset-bot Bot commented Aug 19, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4149e1a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
gitbook Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@argos-ci

argos-ci Bot commented Aug 19, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Argos notifications ↗︎

Build Status Details Updated (UTC)
customers-v2-cloudflare (Inspect) ⚠️ Changes detected (Review) 7 changed, 2 ignored Aug 19, 2026, 11:18 PM
customers-v2-vercel (Inspect) ⚠️ Changes detected (Review) 4 changed, 3 ignored Aug 19, 2026, 11:15 PM
v2-cloudflare (Inspect) ⚠️ Changes detected (Review) 1 changed Aug 19, 2026, 11:19 PM
v2-vercel (Inspect) ⚠️ Changes detected (Review) 3 changed, 4 ignored Aug 19, 2026, 11:18 PM

@github-actions

Copy link
Copy Markdown
Contributor

Style invalidation on a large API reference

Elements restyled by opening one popup on the Snyk API reference. A share near or above 100% means the insertion restyles the whole document.

interaction restyled page share budget
openapi-select 580 10,847 5.3% 25%
search 1,084 10,847 10.0% 125%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant