Skip to content

🎥 Make videos programmatically with React

Scope shared Google Fonts caches to the target document

Alec Watts920b20aa9317
diff --git a/packages/google-fonts/src/base.ts b/packages/google-fonts/src/base.ts
index 2ba66929f8..3e9c4fddd5 100644
--- a/packages/google-fonts/src/base.ts
+++ b/packages/google-fonts/src/base.ts
@@ -2,7 +2,21 @@ import {continueRender, delayRender} from 'remotion';
import {NoReactInternals} from 'remotion/no-react';
import {resolveFontSubsetKeys} from './resolve-font-subsets';
-const loadedFonts: Record<string, Promise<void> | undefined> = {};
+const loadedFontsByDocument = new WeakMap<
+ Document,
+ Record<string, Promise<void> | undefined>
+>();
+
+const getLoadedFontsForDocument = (targetDocument: Document) => {
+ const cached = loadedFontsByDocument.get(targetDocument);
+ if (cached) {
+ return cached;
+ }
+
+ const loadedFonts: Record<string, Promise<void> | undefined> = {};
+ loadedFontsByDocument.set(targetDocument, loadedFonts);
+ return loadedFonts;
+};
export type FontInfo = {
fontFamily: string;
@@ -126,6 +140,8 @@ export const loadFonts = (
`The font ${meta.fontFamily} does not have a style ${style}`,
);
}
+ const targetDocument = options?.document ?? document;
+ const loadedFonts = getLoadedFontsForDocument(targetDocument);
const weights = options?.weights ?? Object.keys(meta.fonts[style]);
for (const weight of weights) {
if (!meta.fonts[style][weight]) {
@@ -230,7 +246,7 @@ export const loadFonts = (
const promise = Promise.all([tryToLoad(), previousAdded]).then(
([{fontFace, fontData}]) => {
- (options?.document ?? document).fonts.add(fontFace);
+ targetDocument.fonts.add(fontFace);
registerFont(fontData);
continueRender(handle);
},
@@ -322,6 +338,8 @@ export const loadVariableFonts = (
font.subset,
font.src,
].join('-');
+ const targetDocument = options.document ?? document;
+ const loadedFonts = getLoadedFontsForDocument(targetDocument);
const previousPromise = loadedFonts[fontKey];
if (previousPromise) {
promises.push(previousPromise);
@@ -390,7 +408,7 @@ export const loadVariableFonts = (
const promise = Promise.all([tryToLoad(), previousAdded]).then(
([{fontFace, fontData}]) => {
- (options.document ?? document).fonts.add(fontFace);
+ targetDocument.fonts.add(fontFace);
registerFont(fontData);
continueRender(handle);
},
diff --git a/packages/it-tests/src/monorepo/esm.test.ts b/packages/it-tests/src/monorepo/esm.test.ts
index bc0a723bc8..12eb2b2d5d 100644
--- a/packages/it-tests/src/monorepo/esm.test.ts
+++ b/packages/it-tests/src/monorepo/esm.test.ts
@@ -92,5 +92,20 @@ test('Google font ESM entries share loaded font faces with from-info', () => {
]);
assert.equal(constructed, 2);
assert.equal(added.size, 2);
+
+ const iframeFaces = new Set();
+ const iframeDocument = {fonts: {add: (face) => iframeFaces.add(face)}};
+ const iframeOptions = {...options, document: iframeDocument};
+ await Promise.all([
+ loadFont('normal', iframeOptions).waitUntilDone(),
+ loadFontFromInfo(info, 'normal', iframeOptions).waitUntilDone(),
+ loadVariableFont('normal', iframeOptions).waitUntilDone(),
+ loadVariableFontFromInfo(info, 'normal', iframeOptions).waitUntilDone(),
+ ]);
+ assert.equal(constructed, 4);
+ assert.equal(iframeFaces.size, 2);
+ await loadFontFromInfo(info, 'normal', options).waitUntilDone();
+ assert.equal(constructed, 4);
+ assert.equal(added.size, 2);
"`);
});