Repository navigation
feat: vendor theme fonts so no page loads fonts from Google - #104
Merged
Merged
Conversation
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.
|
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 |
Reviewer's GuideThe 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 fontsequenceDiagram
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
Flow diagram for offline font verification in CIflowchart 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"]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements CHECKMATE-19.1 · fonts
Refs #83
What changed
public/fonts/<id>/holds the latin-subset woff2 files, one@font-facestylesheet (<id>.css) and the OFL license for each of the 27 web fonts insrc/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.mjsfetches 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.jsonrecords each file's source URL and SHA-256, and--verifydownloads everything again and compares. A run downloads every family into.cache/fetch-fontsbefore it touches anything, so a failed or interrupted run leavespublic/fontsand the manifest as they were.googleFontUrlnow names the local stylesheet,/fonts/<id>/<id>.css, and the provider still links it only when the font is selected. The fourlayout.tsxfonts load throughnext/font/localover the vendored files, at the weight rangesnext/font/googleused.next.config.tsserves/fontswithpublic, max-age=86400, stale-while-revalidate=604800, the cache Google Fonts gave its stylesheets, instead of themax-age=0thatpublic/gets. Notimmutable, since a refetch keeps the file names.fonts.googleapis.comandfonts.gstatic.comat loopback beforenpm run build(scripts/fonts/block-google-fonts.sh, which fails if either still resolves elsewhere), then runsscripts/fonts/check-font-hosts.mjson what the build serves. The job's printed repro notes the block, since it needs Linux and sudo.CONTRIBUTING.mdcovers the fetch script, the cache rule and the build rule.docs/DEPLOYMENT.mdsays no font host is contacted, so an egress firewall or afont-src 'self'policy needs no exception for fonts.Why
next builddownloaded the four layout fonts from Google and failed without it, and the other 23 Theme Studio fonts linked stylesheets fromfonts.googleapis.comin the browser. The theme axes leaf, #78 (CHECKMATE-17.2), then moves all 27 tonext/font/localover these files.Verification
Against
origin/nextat 41a1943, the merge of the storage adapter (#103):scripts/ci/lint-changed.mjs): clean, noeslint-disable.npm run lint: 37 errors, 48 warnings, against 38 and 48 onorigin/next. The one fewer is the theme provider'sas any(decision 6).npx tsc --noEmit: 0 errors, same asorigin/next.npm teston the fixture: 798 tests in 43 files pass, none skipped, 21 of them new.npm run buildwith PocketBase athttp://127.0.0.1:9: passes.#83's test 11, theme fonts with Google out of reach:
scripts/fonts/fonts.test.tsruns offline in the unit job. Every vendored file matches its manifest SHA-256, everyfont-themes.tsentry names the family its@font-facedeclares, the fourlocalFontcalls point at vendored files,/fontsgets the cache rule, and nothing insrc/,public/ornext.config.tsnames a third-party font host or importsnext/font/google. Withorigin/next'sfont-themes.tsandlayout.tsxswapped in, 3 of its tests fail. A fake fetch that fails partway leavespublic/fontsand the manifest unchanged, and that test fails if each family is written as soon as it downloads.check-font-hosts.mjson 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.next buildin a Finchnode:24container with--network none, afternpm ci. Google, gstatic and the npm registry fail withEAI_AGAIN, and the build and the scan pass at this PR's head, 0aa39e8. The same build oforigin/nextfails withFailed to fetch Inter from Google Fonts, and the same for the other three layout fonts.next startbehind 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.next startserves every/fontsfile with the newCache-Control. Otherpublic/files keepmax-age=0, and thenext/fontfiles keep their one-yearimmutable.Visual parity: the four layout fonts are byte-identical to the latin files
next/font/googledownloaded onorigin/next, and the other 23 are byte-identical to what Google serves Chrome and Edge on Windows and Firefox on Linux (decision 4). Innext 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
OFL.txtcomes 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 whatfonts.gstatic.comserves. 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 whatnext/font/googlealready 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.next/font/localnames a family after itsconst, so each call setsfont-familyindeclarationsto keep "Inter", "Plus Jakarta Sans" and the rest matchingfont-themes.ts. Turbopack applies that to the@font-faceonly, 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 dropdeclarationswhen it moves the entries tovar(--font-*), or the 12 multi-word families stop matching.@font-faceper 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.prepprogram 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 filesnext/font/googlealready served to everyone.unified-theme-provider.tsxdropsmergedEffects as any. lint-changed lints the whole file once its comments change, and the value already types asEffectSettings.googleFontUrlkeeps 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/fontstoo, so every family directory has the same three parts, though onlynext/font/localloads 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.