Skip to content

fix(producer): inline a local font only while the page stays within its size limit - #8152

Merged
miguel-heygen merged 3 commits into
mainfrom
secbounds/font-embed-once
Nov 18, 2026
Merged

miguel-heygen merged 3 commits into
mainfrom
secbounds/font-embed-once

Conversation

@miguel-heygen

@miguel-heygen miguel-heygen commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

What

A render inlines a project font as a data URI only while the compiled page stays within the render's size limit. Once the limit is reached, the remaining references keep loading the font from its file, as fonts over 5 MiB already do.

Why

Each local font is capped at 5 MiB, but its data URI was written into every url(path) that named it, anywhere in the page. One 5 MiB font named by a few dozen rules turned into a compiled page of hundreds of megabytes, which every later compile step copies and parses again. With 8 rules the new test's page is 62.9 MB on main.

Related work

Builds on #8144 and the CompositionExpansionBudget it shares across the compile. No second budget.

How

  • embedLocalFontFaces now rewrites only the url() sources inside @font-face rules in <style> blocks (the same rules it reads fonts from), using the existing replaceMatches scans. A url() in an ordinary rule is left alone.
  • Every inlined copy is charged to the render's budget through a new CompositionExpansionBudget.chargeGeneratedIfItFits, which charges only what fits and reports whether it did. A copy that does not fit keeps its file path and the compile logs one warning; the render does not fail.
  • Reading and encoding a font moved into localFontDataUri; the containment, link and private-file checks are unchanged.
  • urlOccurrenceRe is removed: replacing the scanned url() match by its exact path cannot hit a longer path that ends the same way, which is what that regex guarded. Its test file now checks the same hazards through compileForRender: img/logo.ttf beside assets/img/logo.ttf, a path with regex characters, and the path mentioned in a CSS comment each keep their own content.
  • A font path containing ) is still not embedded (the shared url() scanner stops there, as on main); it stays file-backed.

Test plan

fails without the fix: htmlCompiler.sizeLimit.test.ts "inlines a font that many rules name only while the page stays within the limit" builds a 62.9 MB page on main from one 5 MiB font named by 8 rules.

  • New test in htmlCompiler.sizeLimit.test.ts: one 5 MiB font named by 8 @font-face rules and one ordinary rule. It fails on main (62.9 MB page against a limit near 32 MiB) and passes here: some copies inline, the rest and the ordinary rule keep url("big.ttf").

  • Breaking chargeGeneratedIfItFits to always accept turns only that test red (55.9 MB page); restored. Handing every source the first font's data URI turns the anchoring test red; restored.

  • htmlCompiler.sizeLimit.test.ts 21 passed, htmlCompiler.fontEmbed.test.ts 1 passed, htmlCompiler.test.ts 196 passed (includes the existing local-font tests: private files, scene dedupe, equivalent paths, ttf/otf, large collections). Producer tsc --noEmit, oxfmt, oxlint and fallow audit clean.

  • Unit tests added/updated

  • Manual testing performed (no end-to-end render)

  • Documentation updated (if applicable): no docs change

  • Comments follow CONTRIBUTING.md "Comments"

…ts size limit

Only url() sources inside @font-face rules are replaced, and every inlined
copy is charged to the render's expansion budget. Once the budget is spent,
the remaining references keep loading the font from its file.
@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 271 (base branch 271), smooth 79 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Quarantined, measured but not gated (0)

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 65e337d. Two merge blockers remain:

[P2] Add the lane's required fails without the fix: line naming htmlCompiler.sizeLimit.test.ts / "inlines a font that many rules name only while the page stays within the limit". The current body explains the regression, but does not contain that required line.

[P2] Clear the introduced required CI failures. Detect changes rejects deletion of htmlCompiler.fontEmbed.test.ts; Build, Typecheck and Test then fail because change detection did not succeed. Comments separately fails because compositionSizeLimit.ts raises its comment share from 7.9% to 8.3%. I read both failing-job logs. Preserve/rework the anchoring coverage through the repo's deliberate-deletion workflow and resolve the comment gate, then rerun CI.

No additional code blocker found in this pass. The rewritten matches are scoped to style/font-face blocks, filesystem/private-file checks remain, equivalent absolute paths share encoded data, and each inserted copy is charged before insertion, keeping refused copies file-backed. Full workspace build passed; 21 size-limit tests and 196 compiler tests passed. Removing the tryChargeGenerated refusal makes the new regression produce 55,926,646 characters and fail; restoring it passes. No end-to-end font render performed.

— tai (automated review)

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed 4a4c185. The earlier blockers are resolved: the body now has the required fails-without-the-fix line, htmlCompiler.fontEmbed.test.ts is retained and exercises the real compileForRender pipeline, and the budget method is renamed without raising comment share. Local deletion/comment gates pass; their CI checks are green too.

The product delta from 65e337d is the method rename and matching call, with its fit-or-skip behavior unchanged. The new anchoring test checks distinct short/suffix/regex-character font paths and preserves the path mentioned in a CSS comment. Full workspace build passed; 22 size-limit/anchoring tests and 196 compiler tests passed (218 total). Deliberately using the first font's data for every path makes the new anchoring test fail; restoring the reviewed source passes. The prior budget-refusal mutation proof still applies to the unchanged budget logic.

No remaining blocker found. CI is still running, with no failed checks at submission. No end-to-end font render performed.

— tai (automated review)

@miguel-heygen
miguel-heygen added this pull request to the merge queue Nov 18, 2026
Merged via the queue into main with commit 3b79b18 Nov 18, 2026
132 of 133 checks passed
@miguel-heygen
miguel-heygen deleted the secbounds/font-embed-once branch November 18, 2026 09:20
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.

2 participants