Skip to content

🎥 Make videos programmatically with React

docs: improve font-loading best practices and defaults

Cursor Agentb41573a7649f
diff --git a/packages/docs/docs/fonts.mdx b/packages/docs/docs/fonts.mdx
index a704c4a3ab..e1eea3bd3c 100644
--- a/packages/docs/docs/fonts.mdx
+++ b/packages/docs/docs/fonts.mdx
@@ -17,13 +17,20 @@ _available from v3.2.40_
```tsx title="MyComp.tsx"
import { loadFont } from "@remotion/google-fonts/TitanOne";
-const { fontFamily } = loadFont();
+const { fontFamily } = loadFont("normal", {
+ weights: ["400"],
+ subsets: ["latin"],
+});
const GoogleFontsComp: React.FC = () => {
return <div style={{ fontFamily }}>Hello, Google Fonts</div>;
};
```
+For projects with multiple fonts, load fonts in one shared module and wait until they are ready before rendering.
+
+If you use text measurement APIs, see the [higher-order component example](/docs/layout-utils/best-practices#example-with-high-order-component).
+
## Google Fonts using CSS
Import the CSS that Google Fonts gives you.
diff --git a/packages/docs/docs/google-fonts/index.mdx b/packages/docs/docs/google-fonts/index.mdx
index 2d2e5cf7bb..75c285293a 100644
--- a/packages/docs/docs/google-fonts/index.mdx
+++ b/packages/docs/docs/google-fonts/index.mdx
@@ -50,36 +50,52 @@ pnpm i @remotion/google-fonts
To load a font, import the package `@remotion/google-fonts/<FontName>` and call [`loadFont()`](/docs/google-fonts/load-font).
-```tsx twoslash title="Load all font styles"
+For production, start with these defaults:
+
+- Pass a style and usually `weights` + `subsets` to avoid loading unnecessary variants.
+- Load your fonts in one module and reuse the exports.
+- If you use text measurement APIs, gate rendering until fonts are ready using a higher-order component.
+
+```tsx twoslash title="Load one style, weight and subset"
import {loadFont} from '@remotion/google-fonts/TitanOne';
-const {fontFamily} = loadFont(); // "Titan One"
+
+const {fontFamily} = loadFont('normal', {
+ weights: ['400'],
+ subsets: ['latin'],
+}); // "Titan One"
```
If you want to import multiple fonts and want to avoid a variable name collision, you can import the fonts using an `import * as` statement.
```tsx twoslash title="Scope loadFont() variable"
import * as Montserrat from '@remotion/google-fonts/Montserrat';
-Montserrat.loadFont();
+Montserrat.loadFont('normal', {
+ weights: ['400'],
+ subsets: ['latin'],
+});
```
-Call [`loadFont()`](/docs/google-fonts/load-font) to start the loading process. By default, every style, weight and subset is loaded.
+Call [`loadFont()`](/docs/google-fonts/load-font) to start the loading process. Calling it without arguments loads every style, weight and subset, which can cause unnecessary requests.
-You can pass a style (such as `normal`, `italic`) to only load that specific style. If you want multiple styles, call `loadFont()` multiple times.
+You can pass a style (such as `normal`, `italic`) to only load that style. If you want multiple styles, call `loadFont()` multiple times.
```tsx twoslash title="Load just one style"
import {loadFont} from '@remotion/google-fonts/TitanOne';
-loadFont('normal');
+loadFont('normal', {
+ weights: ['400'],
+ subsets: ['latin'],
+});
```
Use the TypeScript autocomplete to see the available styles. To further narrow down what's being loaded, you can specify `weights` and `subsets`. These options are also typesafe.
-```tsx twoslash title="Load a specific style with limit weights and subsets"
+```tsx twoslash title="Load a specific style with limited weights and subsets"
import * as Montserrat from '@remotion/google-fonts/Montserrat';
Montserrat.loadFont('normal', {
- weights: ['400', '600', '800'],
- subsets: ['latin', 'latin-ext'],
+ weights: ['400', '700'],
+ subsets: ['latin'],
});
```
@@ -89,7 +105,10 @@ Montserrat.loadFont('normal', {
import {loadFont} from '@remotion/google-fonts/TitanOne';
import {AbsoluteFill} from 'remotion';
-const {fontFamily} = loadFont();
+const {fontFamily} = loadFont('normal', {
+ weights: ['400'],
+ subsets: ['latin'],
+});
export const GoogleFontsDemoComposition = () => {
return (
@@ -104,6 +123,31 @@ export const GoogleFontsDemoComposition = () => {
};
```
+If your project uses multiple fonts, define them in one file and export a `waitForFonts()` helper:
+
+```tsx twoslash title="fonts.ts"
+import {loadFont as loadInter} from '@remotion/google-fonts/Inter';
+import {loadFont as loadRobotoMono} from '@remotion/google-fonts/RobotoMono';
+
+const inter = loadInter('normal', {
+ weights: ['400', '700'],
+ subsets: ['latin'],
+});
+const mono = loadRobotoMono('normal', {
+ weights: ['400'],
+ subsets: ['latin'],
+});
+
+export const waitForFonts = async () => {
+ await Promise.all([inter.waitUntilDone(), mono.waitUntilDone()]);
+};
+```
+
+If you use text measurement APIs, wrap your content in a component that waits for your fonts before rendering:
+
+- [Higher-order component example](/docs/layout-utils/best-practices#example-with-high-order-component)
+- [Troubleshooting font loading errors](/docs/troubleshooting/font-loading-errors)
+
To get information about a font, you can import the `getInfo()` function:
```tsx twoslash title="Get info about the font"
diff --git a/packages/docs/docs/google-fonts/load-font.mdx b/packages/docs/docs/google-fonts/load-font.mdx
index f889e011e7..a1d9d47334 100644
--- a/packages/docs/docs/google-fonts/load-font.mdx
+++ b/packages/docs/docs/google-fonts/load-font.mdx
@@ -14,13 +14,16 @@ Load a [Google Font](https://fonts.google.com) for use in Remotion.
## Usage
-Without arguments, all styles, weights and character subsets of a font are being loaded.
+For production, load only the variants you need.
-```tsx twoslash title="Load all variants of the Lobster font"
+```tsx twoslash title="Load one style, weight and subset"
import {loadFont} from '@remotion/google-fonts/Lobster';
import {AbsoluteFill} from 'remotion';
-const {fontFamily} = loadFont();
+const {fontFamily} = loadFont('normal', {
+ weights: ['400'],
+ subsets: ['latin'],
+});
export const GoogleFontsExample: React.FC = () => {
return (
@@ -37,11 +40,15 @@ export const GoogleFontsExample: React.FC = () => {
Pass a specific style (e.g. `normal`, `italic`) and optionally weights and subsets to narrow down what gets loaded.
+:::warning
+Calling `loadFont()` without arguments loads all styles, weights and character subsets of a font. This can trigger a lot of network requests and may lead to warnings or timeouts.
+:::
+
:::info
-A large font file paired with a limited bandwith can potentially lead to a render timeout. By specifing the exact style, weights and subsets you need, the filesize can be reduced and possibly prevent the render from timing out. If the if the problem persists, [increasing the timeout](/docs/timeout#increase-timeout) will help further.
+A large font file paired with limited bandwidth can lead to a render timeout. By specifying the exact style, weights and subsets you need, the file size can be reduced and may prevent the render from timing out. If the problem persists, [increasing the timeout](/docs/timeout#increase-timeout) can help further.
:::
-If you want to load multiple styles, use multiple `loadFont()` statements.
+If you want to load multiple styles, use multiple `loadFont()` statements and narrow each call.
```tsx twoslash title="Load a specific style, weight and subset"
import {AbsoluteFill} from 'remotion';
@@ -74,9 +81,14 @@ export const GoogleFontsExample: React.FC = () => {
};
```
+If your project uses multiple fonts, load them in one module and export a `waitForFonts()` helper. If you measure text, gate rendering until the fonts are loaded using a higher-order component:
+
+- [Higher-order component example](/docs/layout-utils/best-practices#example-with-high-order-component)
+- [Troubleshooting font loading errors](/docs/troubleshooting/font-loading-errors)
+
## Arguments
-All arguments and options are optional. Use TypeScript autocompletion or [`getInfo`](/docs/google-fonts/get-info) to discover available options. With any of them, all styles, weights and subsets are loaded.
+All arguments and options are optional. Use TypeScript autocompletion or [`getInfo`](/docs/google-fonts/get-info) to discover available options. If no arguments are passed, all styles, weights and subsets are loaded.
### style
@@ -107,7 +119,7 @@ An object with the following properties:
### fontFamily
-The font family name, be used as the `fontFamily` CSS property.
+The font family name, used as the `fontFamily` CSS property.
### fonts
diff --git a/packages/docs/docs/layout-utils/best-practices.mdx b/packages/docs/docs/layout-utils/best-practices.mdx
index c5369f04c3..862fbcf8b1 100644
--- a/packages/docs/docs/layout-utils/best-practices.mdx
+++ b/packages/docs/docs/layout-utils/best-practices.mdx
@@ -13,6 +13,8 @@ These tips apply to all of [`measureText()`](/docs/layout-utils/measure-text), [
Only call [`measureText()`](/docs/layout-utils/measure-text) after the font is loaded. This applies to Google Fonts (example below) or any other font loading mechanism.
+If you only need this in one component, using `waitUntilDone()` in a `useEffect()` is fine.
+
### Example with `useEffect`
```tsx twoslash title="MyComp.tsx"
@@ -20,7 +22,10 @@ import {useState, useEffect} from 'react';
import {Dimensions, measureText} from '@remotion/layout-utils';
import {loadFont, fontFamily} from '@remotion/google-fonts/Inter';
-const {waitUntilDone} = loadFont('normal');
+const {waitUntilDone} = loadFont('normal', {
+ weights: ['400'],
+ subsets: ['latin'],
+});
const MyComp: React.FC = () => {
const [dimensions, setDimensions] = useState<Dimensions | null>(null);
@@ -47,8 +52,9 @@ const MyComp: React.FC = () => {
### Example with high-order component
-This following logic is borrowed from the [Remotion Recorder](/docs/recorder).
-It keeps the code clean by exposing a high-order component which only mounts its children when the font is loaded.
+For larger projects, this pattern keeps the code clean by exposing a higher-order component that only mounts children once fonts are loaded.
+
+The following logic is borrowed from the [Remotion Recorder](/docs/recorder).
A file is defined which loads some fonts:
@@ -59,8 +65,14 @@ import {fontFamily as monospaceFont, loadFont as loadMonospace} from '@remotion/
import {cancelRender, continueRender, delayRender} from 'remotion';
-const regular = loadRegular();
-const monospace = loadMonospace();
+const regular = loadRegular('normal', {
+ weights: ['400', '700'],
+ subsets: ['latin'],
+});
+const monospace = loadMonospace('normal', {
+ weights: ['400'],
+ subsets: ['latin'],
+});
export const waitForFonts = async () => {
await regular.waitUntilDone();
@@ -74,7 +86,7 @@ waitForFonts()
.catch((err) => cancelRender(err));
```
-Then a higher order component is defined which only renders it's children when the font is loaded:
+Then a higher-order component is defined which only renders its children when the font is loaded:
```tsx twoslash title="WaitForFonts.tsx"
// @filename: fonts.ts
@@ -84,8 +96,14 @@ import {fontFamily as monospaceFont, loadFont as loadMonospace} from '@remotion/
import {cancelRender, continueRender, delayRender} from 'remotion';
-export const regular = loadRegular();
-const monospace = loadMonospace();
+export const regular = loadRegular('normal', {
+ weights: ['400', '700'],
+ subsets: ['latin'],
+});
+const monospace = loadMonospace('normal', {
+ weights: ['400'],
+ subsets: ['latin'],
+});
export const waitForFonts = async () => {
await regular.waitUntilDone();
@@ -139,7 +157,7 @@ export const WaitForFonts: React.FC<{
};
```
-Then the component can be wrap any other component that calls text measurement APIs:
+Then the component can wrap any other component that calls text measurement APIs:
```tsx twoslash title="MyComp.tsx"
// @filename: fonts.ts
diff --git a/packages/docs/docs/troubleshooting/font-loading-errors.mdx b/packages/docs/docs/troubleshooting/font-loading-errors.mdx
index f9f187c61e..befcac8a75 100644
--- a/packages/docs/docs/troubleshooting/font-loading-errors.mdx
+++ b/packages/docs/docs/troubleshooting/font-loading-errors.mdx
@@ -34,3 +34,7 @@ loadFont('normal', {
weights: ['400', '700'], // Only load regular (400) and bold (700)
});
```
+
+If your project uses multiple fonts, centralize font loading in one module and wait until all fonts are loaded before rendering:
+
+- [Higher-order component example](/docs/layout-utils/best-practices#example-with-high-order-component)