fix(producer): a remote font embeds in the page instead of warning it is missing (#8297)

This commit is contained in:
Miguel Ángel
2026-11-20 16:19:41 +00:00
committed by GitHub
parent 88c22a77f4
commit 2f23fbd364
2 changed files with 69 additions and 8 deletions
@@ -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"),
`<!DOCTYPE html>
<html><head><style>
@font-face { font-family: "Remote"; src: url("https://fonts.example.com/remote.ttf"); }
</style></head><body>
<div data-composition-id="root" data-width="640" data-height="360" data-duration="1">Text</div>
</body></html>`,
);
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 <link> 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 });
+24 -8
View File
@@ -2182,21 +2182,27 @@ async function readLocalFont(absPath: string): Promise<LocalFontRead> {
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<string, string | undefined>,
log: ProducerLogger,
downloadedPath?: string,
): Promise<string | undefined> {
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<string, string> = new Map(),
): Promise<string> {
const dataUriByPath = new Map<string, string | undefined>();
const dataUriByAbsolutePath = new Map<string, string | undefined>();
@@ -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).