Skip to content

fonts gate before composition scripts only covers faces that static markup already uses #5112

Closed

Description

@jbernard077

Since 0.8.122 (#4980) composition scripts start only after web fonts are ready, which fixed measure-after-build layouts for fonts that static markup uses. The gate does not cover a face that only script-built text uses: it forces one layout, then awaits document.fonts.ready, and a face starts loading only when some text already in the DOM requests it. Text created by the composition script requests its face afterwards, so a getBoundingClientRect() taken at script time still sees the fallback font for that face. Which faces are loaded when a script runs therefore depends on what other scenes in the assembled page happen to show statically.

Repro (two files, attached): a local @font-face ("LocalFace", font-display: block, embedded by the compiler as a data URI) that no static text uses. The script sets a title in that face, measures it, centres it from the measurement, registers the timeline, then measures again inside document.fonts.load() and writes both numbers into the frame.

  • v0.8.135, render --workers 1: script-time width 1378.1 status unloaded / after fonts.load width 1056.5 status loaded. The title is centred from the first number, so it sits 161 px off centre in the final frame.
  • Control: uncomment the one static <span> in the face and both widths are 1056.5 with status loaded at script time.

Same mechanism with the compiler-fetched Google face: in a nine-scene project, a script-built block measured correctly only because an unrelated scene's static label happened to use the same weight; a second script-built block whose rows use a different weight measured 4 px short and was centred 2 px off. A handwritten title whose underline is drawn to the measured width rendered the underline 253 px too long and the title 114 px off centre in every render.

Two fixes look possible from the outside:

  1. Before releasing the scripts, call load() on every declared FontFace (Array.from(document.fonts).map((f) => f.load())) under the existing 5 s cap, so the stated guarantee holds for script-built text too.
  2. A lint finding when a composition script calls getBoundingClientRect / offsetWidth / offsetHeight outside a document.fonts.load() / document.fonts.ready callback, with the fix hint to measure inside it and register the timeline at the end (the documented async-build path).

Repro project (index.html, assets/fonts/local-face-700.woff2, package.json, hyperframes.json) and the rendered frame attached. Render log lines come from the page console as surfaced by the CLI.

Attachments: repro-bundle.zip (the four repro files)

rendered frame: script-time width 1378.1 px vs 1056.5 px after fonts.load

Activity

  1. jbernard077 commented on Oct 6, 2026

    @jbernard077
    ContributorAuthor

    A related case from the same project, posted here rather than as a new issue because it is the same shape: an audit that silently skips content under a condition the author cannot see.

    contrast-audit.browser.js probes text inside a data-layout-allow-occlusion element with document.elementFromPoint at a few points of its box and skips the element as intentionally occluded when something else answers. A sub-composition host is a full-frame box, so any host that comes later in the DOM and is visible at the sample time answers the probe for every allow-occlusion text block beneath it, even though the host paints nothing there. In the project this removed all eight contrast findings of a block the moment an unrelated small overlay scene was moved into a sub-composition with an overlapping time window. Neither overflow nor pointer-events: none on the sub-composition root changes it, because the host element itself takes the hit.

    Two possible fixes from the outside: probe with elementsFromPoint and ignore hits that are composition hosts or roots with no painted background at that point, so only painted content counts as cover; or treat allow-occlusion as a declaration (skip only the layout occlusion finding) and still measure contrast against the pixels that are actually behind the text. A lint note that data-layout-allow-occlusion belongs on the covering element, not on the text that may be covered, would help too.

  2. added
    bug
    triage/needs-design
    on Oct 10, 2026
  3. miguel-heygen commented on Oct 24, 2026

    @miguel-heygen
    Collaborator

    Thanks for the repro bundle. I reproduced it on 0.8.193 with a snapshot at 1.5 s: script time measures 1378.1 px with the face unloaded, fonts.load() gives 1056.5 px, and the title is centred from the first number. With your static <span> uncommented, both readings are 1056.5 px.

    The cause is as you describe. Before releasing composition scripts, the runtime forces one layout and waits for document.fonts.ready (waitForFonts in packages/core/src/runtime/afterFonts.ts). A face no laid-out text uses yet hasn't started loading, so ready doesn't wait for it.

    Until the gate changes, the documented workaround is to measure inside document.fonts.load() and register the timeline at the end of that callback (added to the docs in #5203). The contrast-audit case from your follow-up comment was fixed separately in #5202.

  4. jbernard077 commented on Oct 24, 2026

    @jbernard077
    ContributorAuthor

    Thanks for confirming. The project this came from already measures inside document.fonts.load() everywhere it needs a width, so the workaround is in use here.

    For the gate itself, one design that keeps the current behaviour for static text and closes this case: after the forced layout, call load() on every entry in document.fonts whose status is still unloaded, then await ready under the existing timeout. Faces a page declares but never uses would load once at startup, bounded by the same timeout and warning path. If that is the direction you want, I can open a PR with a core test for the script-only-face case from the repro.

  5. miguel-heygen commented on Oct 25, 2026

    @miguel-heygen
    Collaborator

    Yes, please open that PR; the direction you describe fits. A few pointers so it lands in one round:

    • The change goes in waitForFonts in packages/core/src/runtime/afterFonts.ts. It is the one place every caller of the gate goes through. After the forced layout (void document.body?.offsetHeight), call load() for each entry in document.fonts whose status is still unloaded, then keep awaiting fonts.ready under the existing FONT_WAIT_TIMEOUT_MS race, so a face that never loads still ends in the existing timeout warning and the runtime_font_wait_timeout diagnostic.
    • Handle a rejected load() (a missing font file) by letting it fall through to that same timeout path instead of throwing, so one broken face can't stop composition scripts from running.
    • Tests live in packages/core/src/runtime/entry.afterFonts.test.ts. Please add the script-only-face case from your repro there (a face declared but not used by any laid-out text, measured at script time), and a case where the face fails to load.
    • Declared-but-never-used faces will now load once at startup. Please say so in the PR description, so the cost is visible to maintainers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtriage/needs-design

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions