Skip to content

feat: vendor theme fonts so no page loads fonts from Google - #104

Merged
keyurgolani merged 9 commits into
nextfrom
feat/19.1-fonts
Oct 20, 2026
Merged

keyurgolani merged 9 commits into
nextfrom
feat/19.1-fonts

Conversation

@keyurgolani

@keyurgolani keyurgolani commented Oct 20, 2026 •

Copy link
Copy Markdown
Owner

Implements CHECKMATE-19.1 · fonts
Refs #83

What changed

  • public/fonts/<id>/ holds the latin-subset woff2 files, one @font-face stylesheet (<id>.css) and the OFL license for each of the 27 web fonts in src/lib/themes/font-themes.ts. That's 38 woff2 files at 985,292 bytes, and 1,123,670 bytes with the stylesheets and licenses.
  • scripts/fonts/fetch-fonts.mjs fetches them. The woff2 files come from the Google Fonts css2 API, for the queries the app already made, and each license and upstream repository from the family's directory in google/fonts at 47d54ca. scripts/fonts/manifest.json records each file's source URL and SHA-256, and --verify downloads everything again and compares. A run downloads every family into .cache/fetch-fonts before it touches anything, so a failed or interrupted run leaves public/fonts and the manifest as they were.
  • googleFontUrl now names the local stylesheet, /fonts/<id>/<id>.css, and the provider still links it only when the font is selected. The four layout.tsx fonts load through next/font/local over the vendored files, at the weight ranges next/font/google used.
  • next.config.ts serves /fonts with public, max-age=86400, stale-while-revalidate=604800, the cache Google Fonts gave its stylesheets, instead of the max-age=0 that public/ gets. Not immutable, since a refetch keeps the file names.
  • CI's build job points fonts.googleapis.com and fonts.gstatic.com at loopback before npm run build (scripts/fonts/block-google-fonts.sh, which fails if either still resolves elsewhere), then runs scripts/fonts/check-font-hosts.mjs on what the build serves. The job's printed repro notes the block, since it needs Linux and sudo.
  • CONTRIBUTING.md covers the fetch script, the cache rule and the build rule. docs/DEPLOYMENT.md says no font host is contacted, so an egress firewall or a font-src 'self' policy needs no exception for fonts.

Why

next build downloaded the four layout fonts from Google and failed without it, and the other 23 Theme Studio fonts linked stylesheets from fonts.googleapis.com in the browser. The theme axes leaf, #78 (CHECKMATE-17.2), then moves all 27 to next/font/local over these files.

Verification

Against origin/next at 41a1943, the merge of the storage adapter (#103):

  • eslint on the 8 changed source files (scripts/ci/lint-changed.mjs): clean, no eslint-disable.
  • npm run lint: 37 errors, 48 warnings, against 38 and 48 on origin/next. The one fewer is the theme provider's as any (decision 6).
  • npx tsc --noEmit: 0 errors, same as origin/next.
  • npm test on the fixture: 798 tests in 43 files pass, none skipped, 21 of them new.
  • npm run build with PocketBase at http://127.0.0.1:9: passes.

#83's test 11, theme fonts with Google out of reach:

  1. scripts/fonts/fonts.test.ts runs offline in the unit job. Every vendored file matches its manifest SHA-256, every font-themes.ts entry names the family its @font-face declares, the four localFont calls point at vendored files, /fonts gets the cache rule, and nothing in src/, public/ or next.config.ts names a third-party font host or imports next/font/google. With origin/next's font-themes.ts and layout.tsx swapped in, 3 of its tests fail. A fake fetch that fails partway leaves public/fonts and the manifest unchanged, and that test fails if each family is written as soon as it downloads.
  2. check-font-hosts.mjs on the build: 179 files, no third-party font host. Inter, Plus Jakarta Sans, Outfit and Manrope load from the built CSS, the other 23 from /fonts.
  3. next build in a Finch node:24 container with --network none, after npm ci. Google, gstatic and the npm registry fail with EAI_AGAIN, and the build and the scan pass at this PR's head, 0aa39e8. The same build of origin/next fails with Failed to fetch Inter from Google Fonts, and the same for the other three layout fonts.
  4. next start behind a proxy that logs violation reports and sets this CSP: default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self'; connect-src 'self' http://127.0.0.1:9; frame-src 'self'. The app's inline scripts and its injected style need 'unsafe-inline', and fonts and stylesheets can only come from this origin. The app loaded once per font through the persisted theme store, so the provider applied it the usual way. All 27 render their own family, none makes a request off the app's origin, and the proxy logged 0 violation reports.
  5. next start serves every /fonts file with the new Cache-Control. Other public/ files keep max-age=0, and the next/font files keep their one-year immutable.

Visual parity: the four layout fonts are byte-identical to the latin files next/font/google downloaded on origin/next, and the other 23 are byte-identical to what Google serves Chrome and Edge on Windows and Firefox on Linux (decision 4). In next dev, picking Outfit, Montserrat, Lato, Space Mono, IBM Plex Mono and Comfortaa in Theme Studio, after the default Inter, renders the app in each face from this origin.

Decisions

  1. Each family's OFL.txt comes from its directory in google/fonts, and the manifest names each upstream repository and commit. All 27 are OFL-1.1, Roboto Mono included, since google/fonts moved it off Apache. The woff2 files are Google's latin subsets, byte-identical to what fonts.gstatic.com serves. google/fonts itself ships full TTFs. Eight families declare a Reserved Font Name: Comfortaa, IBM Plex Mono ("Plex"), Lato, Orbitron, Quicksand, Raleway, Righteous and Source Sans 3 ("Source"). They ship here as Google's subsets, unchanged, which is what next/font/google already did for the four layout fonts and what Fontsource publishes (its latin files for Lato, Poppins and Righteous match these byte for byte). Whether that's acceptable for the eight is the owner's call, and this PR leaves the files as they are.
  2. next/font/local names a family after its const, so each call sets font-family in declarations to keep "Inter", "Plus Jakarta Sans" and the rest matching font-themes.ts. Turbopack applies that to the @font-face only, so the --font-* variables still carry the const names. Nothing renders through them today. A comment at the calls tells [CHECKMATE-17.2] Formalize composable theme axes, tokens, constraints, and presets #78 to drop declarations when it moves the entries to var(--font-*), or the 12 multi-word families stop matching.
  3. Where css2 gives a variable font one @font-face per weight on the same url, the stylesheet has one face over the weight range. With one face per weight the browser fetched the file once per weight it used, four times on a plain page.
  4. css2 serves Windows and Linux a build with TrueType hinting tables, full hinting for Lato, Montserrat, Open Sans, Raleway, Space Mono and IBM Plex Mono and a small prep program for most others, and serves macOS, iOS and Android one without. The 23 vendor the hinted build, 79,708 bytes more, so Windows and Linux keep the files they had. On macOS the two builds render pixel-identically (14 weights across 8 families, 11 to 24px). iOS and Android now get the hinted build too. iOS renders with the same engine as macOS, and Android is unchecked. The four layout fonts keep the unhinted files next/font/google already served to everyone.
  5. Latin only, as the issue says. Text outside it falls back to the system font, and the layout fonts no longer get latin-ext, cyrillic, greek or vietnamese.
  6. unified-theme-provider.tsx drops mergedEffects as any. lint-changed lints the whole file once its comments change, and the value already types as EffectSettings.
  7. googleFontUrl keeps its name, as the issue says, until [CHECKMATE-17.2] Formalize composable theme axes, tokens, constraints, and presets #78 removes it. The four layout fonts get a stylesheet in /fonts too, so every family directory has the same three parts, though only next/font/local loads them.

Migration, rollback and docs (#1's DoD-7): no stored value changes, and reverting this PR goes back to Google-hosted fonts. Dependency failure (#1's DoD-4): Google being out of reach is covered by steps 3 and 4 above, and a fetch that fails partway by step 1.

One owner decision, decision 1's eight Reserved Font Name families. No owner steps.

Latin-subset woff2 files, one @font-face stylesheet and the OFL license for
each of the 27 web fonts in font-themes.ts. scripts/fonts/fetch-fonts.mjs
fetches them from the Google Fonts css2 API and google/fonts at a pinned
commit, and records each file's source URL and SHA-256 in
scripts/fonts/manifest.json; --verify downloads them again and compares.
The four root-layout fonts move to next/font/local over the vendored files,
keeping their family names, and each lazily linked font's googleFontUrl now
names its stylesheet under /fonts. The provider's effects cast goes, since
the merged value already types as EffectSettings.
fonts.test.ts checks the vendored files against the manifest and
font-themes.ts offline. check-font-hosts.mjs scans what a build serves for
font hosts and off-origin stylesheets, and CI's build job now builds with
Google Fonts pointed at loopback, then runs it (#83 test 11).
css2 gives a variable font one face per weight, all on one url, and the
browser fetched the file once per face it used, four times on a plain
page, since public/ files are served with max-age=0. One face over the
requested weight range fetches it once and renders those weights the same.
css2 serves Windows and Linux a hinted build and Apple platforms an
unhinted one. The 23 formerly linked fonts now vendor the hinted build, so
Windows and Linux get the files they loaded from Google, and macOS renders
it pixel-for-pixel like the unhinted one. The four layout fonts keep the
files next/font/google downloaded.
@sourcery-ai

sourcery-ai Bot commented Oct 20, 2026

Copy link
Copy Markdown

Sorry @keyurgolani, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 6 days and 12 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@sourcery-ai

sourcery-ai Bot commented Oct 20, 2026

Copy link
Copy Markdown

Reviewer's Guide

The PR removes runtime and build-time dependence on Google Fonts by committing reproducibly fetched latin WOFF2 assets and generated stylesheets for all 27 theme families, loading the four layout fonts through next/font/local and the other families through same-origin stylesheets, and adding manifest-based tests plus CI build scanning to enforce that no served asset contacts an external font host.

Sequence diagram for selecting a vendored theme font

sequenceDiagram
    actor User
    participant Studio as ThemeStudio
    participant Provider as UnifiedThemeProvider
    participant App as AppOrigin
    participant Font as VendoredFontStylesheet

    User->>Studio: select font
    Studio->>Provider: update activeFontTheme
    Provider->>App: generateThemeCssVariables
    Provider->>App: querySelector
    Provider->>App: append link for googleFontUrl
    App->>Font: GET /fonts/<id>/<id>.css
    Font-->>App: @font-face with same-origin WOFF2
    App-->>User: render selected font
Loading

Flow diagram for offline font verification in CI

flowchart LR
    Sources["Committed fonts and manifest"] --> Unit["fonts.test.ts"]
    Unit --> Build["npm run build"]
    Block["block-google-fonts.sh"] --> Build
    Build --> Scan["check-font-hosts.mjs"]
    Scan --> Result{"All served font URLs same-origin?"}
    Result -->|yes| Pass["CI passes"]
    Result -->|no| Fail["CI fails"]
Loading

File-Level Changes

Change Details Files
Vendor all Theme Studio web fonts locally with reproducible source metadata and integrity checks.
  • Add latin-subset WOFF2 assets, generated @font-face stylesheets, and OFL licenses for all 27 families.
  • Record download URLs, upstream metadata, byte counts, and SHA-256 hashes in a manifest.
  • Provide fetch and offline verification tooling that regenerates or validates the committed artifacts.
public/fonts/**
scripts/fonts/fetch-fonts.mjs
scripts/fonts/manifest.json
scripts/fonts/fonts.test.ts
Replace Google-hosted font loading with same-origin loading while preserving family names, weights, and lazy-selection behavior.
  • Switch the four layout fonts from next/font/google to next/font/local over vendored files.
  • Point the remaining theme font stylesheet links at /fonts//.css.
  • Keep CSS family declarations aligned with font-themes.ts and remove the unnecessary type escape in the provider.
src/app/layout.tsx
src/components/providers/unified-theme-provider.tsx
src/lib/themes/font-themes.ts
src/lib/themes/types.ts
Enforce the no-third-party-font-host requirement in CI and document the operational constraint.
  • Block fonts.googleapis.com and fonts.gstatic.com during the build and fail if they resolve away from loopback.
  • Scan built client assets, prerendered pages, and public files for external font hosts or off-origin font loads.
  • Document local font generation, verification, firewall, and CSP expectations.
.github/workflows/ci.yml
scripts/fonts/block-google-fonts.sh
scripts/fonts/check-font-hosts.mjs
CONTRIBUTING.md
docs/DEPLOYMENT.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

public/ files are served with max-age=0, so each load revalidated the
linked theme fonts' stylesheet and then the woff2. They now get
max-age=86400 and a week of stale-while-revalidate, as Google Fonts served
its stylesheets; not immutable, since a refetch keeps the file names. The
test also cleans up the fake build directories it creates.
fetch-fonts staged nothing, so a run that failed after the first families
left public/fonts out of step with the manifest. It now downloads every
family into .cache/fetch-fonts and only then renames the families and the
manifest into place, holding SIGINT and SIGTERM until that's done.
@keyurgolani
keyurgolani merged commit c390c07 into next Oct 20, 2026
10 checks passed
@keyurgolani
keyurgolani deleted the feat/19.1-fonts branch October 20, 2026 21:14
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