Skip to content

Commit 872014b

Browse files
committed
fix: preload dynamic-layout fonts before first render
1 parent f707bb2 commit 872014b

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

pages/demos/dynamic-layout.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const CREDIT_FONT = '12px "Helvetica Neue", Helvetica, Arial, sans-serif'
4444
const CREDIT_LINE_HEIGHT = 16
4545
const HEADLINE_TEXT = 'SITUATIONAL AWARENESS: THE DECADE AHEAD'
4646
const HEADLINE_FONT_FAMILY = '"Iowan Old Style", "Palatino Linotype", "Book Antiqua", Palatino, serif'
47+
const HEADLINE_PRELOAD_FONT = `700 96px ${HEADLINE_FONT_FAMILY}`
4748
const HINT_PILL_SAFE_TOP = 72
4849
const NARROW_BREAKPOINT = 760
4950
const NARROW_COLUMN_MAX_WIDTH = 430
@@ -199,6 +200,20 @@ function createLogo(className: string, alt: string, src: string): HTMLImageEleme
199200
return element
200201
}
201202

203+
async function waitForInitialLayoutAssets(): Promise<void> {
204+
// `document.fonts.ready` only waits for faces currently used in the DOM.
205+
// Our measured title/body text is mounted after this await, so preload the
206+
// exact faces we measure with canvas before the first render.
207+
if ('fonts' in document) {
208+
await Promise.all([
209+
document.fonts.ready,
210+
document.fonts.load(BODY_FONT, BODY_COPY.slice(0, 96)),
211+
document.fonts.load(CREDIT_FONT, CREDIT_TEXT),
212+
document.fonts.load(HEADLINE_PRELOAD_FONT, HEADLINE_TEXT),
213+
])
214+
}
215+
}
216+
202217
function mountStaticNodes(): void {
203218
stage.append(
204219
domCache.headline,
@@ -209,7 +224,7 @@ function mountStaticNodes(): void {
209224
}
210225

211226
const [, openaiLayout, claudeLayout, openaiHit, claudeHit] = await Promise.all([
212-
document.fonts.ready,
227+
waitForInitialLayoutAssets(),
213228
getWrapHull(OPENAI_LOGO_SRC, { smoothRadius: 6, mode: 'mean' }),
214229
getWrapHull(CLAUDE_LOGO_SRC, { smoothRadius: 6, mode: 'mean' }),
215230
getWrapHull(OPENAI_LOGO_SRC, { smoothRadius: 3, mode: 'mean' }),

0 commit comments

Comments
 (0)