From 2f23fbd3649c4adc5e29fb82c0ceadfd0b3587f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Fri, 20 Nov 2026 16:19:41 +0000 Subject: [PATCH] fix(producer): a remote font embeds in the page instead of warning it is missing (#8297) --- .../src/services/htmlCompiler.test.ts | 45 +++++++++++++++++++ .../producer/src/services/htmlCompiler.ts | 32 +++++++++---- 2 files changed, 69 insertions(+), 8 deletions(-) diff --git a/packages/producer/src/services/htmlCompiler.test.ts b/packages/producer/src/services/htmlCompiler.test.ts index a6cf37a952..c929f6c753 100644 --- a/packages/producer/src/services/htmlCompiler.test.ts +++ b/packages/producer/src/services/htmlCompiler.test.ts @@ -2652,6 +2652,51 @@ h1 { font-size: 2rem; }`; } }); + // fails without the fix: the embed looked for _remote_media/ in the project, logged ENOENT and kept the file URL. + it("inlines a downloaded remote font as a data URI instead of warning it is missing", async () => { + const font = readFileSync( + join( + import.meta.dirname, + "../../../../docs/public/catalog/items/frost-sequence-camera-orbit/assets/fonts/Geist-Regular.ttf", + ), + ); + stubDownloadFetch.fetch = async () => new Response(font, { status: 200 }); + const projectDir = mkdtempSync(join(tmpdir(), "hf-remote-font-embed-")); + const downloadDir = mkdtempSync(join(tmpdir(), "hf-remote-font-dl-")); + writeFileSync( + join(projectDir, "index.html"), + ` + +
Text
+`, + ); + const originalWarn = defaultLogger.warn; + const warnings: string[] = []; + defaultLogger.warn = (message) => { + warnings.push(message); + }; + try { + const { html } = await compileForRender( + projectDir, + join(projectDir, "index.html"), + downloadDir, + ); + const inlined = html.match(/data:font\/ttf;base64,([A-Za-z0-9+/=]+)/)?.[1] ?? ""; + expect(Buffer.from(inlined, "base64").equals(font)).toBe(true); + expect(html).not.toContain('url("_remote_media/'); + expect(warnings.filter((message) => message.includes("Could not embed local font"))).toEqual( + [], + ); + } finally { + defaultLogger.warn = originalWarn; + _setDownloadFetchForTests(); + rmSync(projectDir, { recursive: true, force: true }); + rmSync(downloadDir, { recursive: true, force: true }); + } + }); + it("keeps tag when external stylesheet fetch fails (graceful degradation)", async () => { const STYLESHEET_URL = "https://down.example.com/fonts.css"; stubDownloadFetch.fetch = async () => new Response(null, { status: 503 }); diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index 55198aa75f..8500e82618 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -2182,21 +2182,27 @@ async function readLocalFont(absPath: string): Promise { return { kind: "inline", buffer: Buffer.concat(chunks, totalBytes) }; } -/** The data URI a project font path embeds as, or undefined when it stays a file. */ +/** The data URI a project or downloaded font path embeds as, or undefined when it stays a file. */ async function localFontDataUri( localPath: string, projectDir: string, assetRoot: string | undefined, dataUriByAbsolutePath: Map, log: ProducerLogger, + downloadedPath?: string, ): Promise { - const absPath = localPath.startsWith("/") ? localPath : resolve(projectDir, localPath); - if (!isPathInside(absPath, projectDir) || escapesAssetRoot(absPath, assetRoot)) return undefined; - // A project font that is a link leading outside obeys the same policy as the file server. - if (refusesOutsideLink(projectDir, absPath, (line) => log.warn(`[Compiler] ${line}`))) { - return undefined; + const absPath = + downloadedPath ?? (localPath.startsWith("/") ? localPath : resolve(projectDir, localPath)); + if (downloadedPath === undefined) { + if (!isPathInside(absPath, projectDir) || escapesAssetRoot(absPath, assetRoot)) { + return undefined; + } + // A project font that is a link leading outside obeys the same policy as the file server. + if (refusesOutsideLink(projectDir, absPath, (line) => log.warn(`[Compiler] ${line}`))) { + return undefined; + } + if (isPrivateCompositionFile(projectDir, absPath)) return undefined; } - if (isPrivateCompositionFile(projectDir, absPath)) return undefined; if (dataUriByAbsolutePath.has(absPath)) return dataUriByAbsolutePath.get(absPath); const ext = absPath.match(/\.(woff2?|ttf|otf|ttc)$/i)?.[1]?.toLowerCase() ?? "ttf"; try { @@ -2242,6 +2248,7 @@ async function embedLocalFontFaces( assetRoot: string | undefined, budget: CompositionExpansionBudget, log: ProducerLogger = defaultLogger, + downloadedFonts: ReadonlyMap = new Map(), ): Promise { const dataUriByPath = new Map(); const dataUriByAbsolutePath = new Map(); @@ -2252,7 +2259,14 @@ async function embedLocalFontFaces( if (!localPath || dataUriByPath.has(localPath)) continue; dataUriByPath.set( localPath, - await localFontDataUri(localPath, projectDir, assetRoot, dataUriByAbsolutePath, log), + await localFontDataUri( + localPath, + projectDir, + assetRoot, + dataUriByAbsolutePath, + log, + downloadedFonts.get(localPath), + ), ); } } @@ -2668,12 +2682,14 @@ export async function compileForRender( defaultLogger.debug(`[Compiler] Prepared ${preparedGifs.length} animated GIF input(s) as WebM`); } + // Remote fonts were downloaded outside the project, so they embed from where they landed. const embeddedHtml = await embedLocalFontFaces( htmlWithPreparedGifs, projectDir, assetRoot, budget, options.log, + remoteFontAssets, ); // Collect assets that resolve outside projectDir (e.g. ../shared-assets/hero.png).