🎥 Make videos programmatically with React
finish data fetching docs
Jonny Burgere568a1f7e156
diff --git a/packages/core/src/Composition.tsx b/packages/core/src/Composition.tsxindex 011f92114a..f6a4cd4b7b 100644--- a/packages/core/src/Composition.tsx+++ b/packages/core/src/Composition.tsx@@ -35,7 +35,7 @@ export type CompProps<Props> =component: LooseComponentType<Props>;};-export type CalculateMetadataFunction<T = unknown> = (options: {+export type CalculateMetadataFunction<T> = (options: {defaultProps: T;props: T;abortSignal: AbortSignal;diff --git a/packages/docs/docs/data-fetching.md b/packages/docs/docs/data-fetching.mdindex f9053f553c..5960ab4179 100644--- a/packages/docs/docs/data-fetching.md+++ b/packages/docs/docs/data-fetching.md@@ -5,60 +5,285 @@ title: Data fetchingcrumb: "How to"----## Fetching data before the render+In Remotion, you may fetch data from an API to use it in your video. On this page, we document recipes and best practices.++## Fetching data before the render<AvailableFrom v="4.0.0"/>You may use the [`calculateMetadata`](/docs/composition#calculatemetadata) prop of the [`<Composition />`](/docs/composition) component to alter the props that get passed to your React component.-```tsx twoslash-// ---cut----import { useCallback } from "react";+### When to use++The data being fetched using `calculateMetadata()` must be JSON-serializable. That means it is useful for API responses, but not for assets in binary format.++### Usage++Pass a callback function which takes the untransformed `props`, and return an object with the new props.++```tsx twoslash title="src/Root.tsx"import { Composition } from "remotion";-type MyVideoProps = {+type ApiResponse = {title: string;description: string;};+type MyCompProps = {+ id: string;+ data: ApiResponse | null;+};-const MyVideo: React.FC<MyVideoProps> = () => null;+const MyComp: React.FC<MyCompProps> = () => null;export const Root: React.FC = () => {- const calculateMyVideoMetadata = useCallback(async () => {- const data = await fetch("http://example.com/api");- const json = await data.json();+ return (+ <Composition+ id="MyComp"+ component={MyComp}+ durationInFrames={300}+ fps={30}+ width={1920}+ height={1080}+ defaultProps={{+ id: "1",+ data: null,+ }}+ calculateMetadata={async ({ props }) => {+ const data = await fetch(`https://example.com/api/${props.id}`);+ const json = await data.json();++ return {+ props: {+ ...props,+ data: json,+ },+ };+ }}+ />+ );+};+```++The `props` being passed to `calculateMetadata()` are the [input props merged together with the default props](/docs/props-resolution). If you only want to fetch based on the default props, use the `defaultProps` also available in the same object.++The type of the untransformed input and the transformed output must be the same.+Consider using a nullable type for your data and throw an error inside your component to deal with the `null` type:++```tsx twoslash title="MyComp.tsx"+type ApiResponse = {+ title: string;+ description: string;+};+// ---cut---+type MyCompProps = {+ id: string;+ data: ApiResponse | null;+};++const MyComp: React.FC<MyCompProps> = ({ data }) => {+ if (data === null) {+ throw new Error("Data was not fetched");+ }++ return <div>{data.title}</div>;+};+```++### TypeScript typing++You may use the `CalculateMetadataFunction` type from `remotion` to type your callback function. Pass as the generic value (`<>`) the type of your props.++```tsx twoslash title="src/Root.tsx"+type ApiResponse = {+ title: string;+ description: string;+};+type MyCompProps = {+ id: string;+ data: ApiResponse | null;+};++// ---cut---+import { CalculateMetadataFunction } from "remotion";++export const calculateMyCompMetadata: CalculateMetadataFunction<+ MyCompProps+> = async ({ props }) => {+ return {+ props: {+ ...props,+ data: {+ title: "Hello world",+ description: "This is a description",+ },+ },+ };+};++export const MyComp: React.FC<MyCompProps> = () => null;+```- return {- props: json,- };- }, []);+### Colocation+Here is an example of how you could define a schema, a component and a fetcher function in the same file:++```tsx twoslash title="MyComp.tsx"+import { CalculateMetadataFunction } from "remotion";+import { z } from "zod";++const apiResponse = z.object({ title: z.string(), description: z.string() });++export const myCompSchema = z.object({+ id: z.string(),+ data: z.nullable(apiResponse),+});++type Props = z.infer<typeof myCompSchema>;++export const calcMyCompMetadata: CalculateMetadataFunction<Props> = async ({+ props,+}) => {+ const data = await fetch(`https://example.com/api/${props.id}`);+ const json = await data.json();++ return {+ props: {+ ...props,+ data: json,+ },+ };+};++export const MyComp: React.FC<Props> = ({ data }) => {+ if (data === null) {+ throw new Error("Data was not fetched");+ }++ return <div>{data.title}</div>;+};+```++```tsx twoslash title="src/Root.tsx"+// @filename: src/MyComp.tsx+// organize-imports-ignore+import React from "react";+import { CalculateMetadataFunction } from "remotion";+import { z } from "zod";++const apiResponse = z.object({ title: z.string(), description: z.string() });++export const myCompSchema = z.object({+ id: z.string(),+ data: z.nullable(apiResponse),+});++type Props = z.infer<typeof myCompSchema>;++export const calcMyCompMetadata: CalculateMetadataFunction<Props> = async ({+ props,+}) => {+ const data = await fetch(`https://example.com/api/${props.id}`);+ const json = await data.json();++ return {+ props: {+ ...props,+ data: json,+ },+ };+};++export const MyComp: React.FC<Props> = ({ data }) => {+ if (data === null) {+ throw new Error("Data was not fetched");+ }++ return <div>{data.title}</div>;+};++// @filename: src/Root.tsx+// ---cut---+import React from "react";+import { Composition } from "remotion";+import { MyComp, calcMyCompMetadata, myCompSchema } from "./MyComp";++export const Root = () => {return (<Composition- id="MyVideo"- component={MyVideo}+ id="MyComp"+ component={MyComp}durationInFrames={300}fps={30}width={1920}height={1080}defaultProps={{- title: "My video",- description: "This is my video",+ id: "1",+ data: null,}}- calculateMetadata={calculateMyVideoMetadata}+ schema={myCompSchema}+ calculateMetadata={calcMyCompMetadata}/>);};```+By implementing this pattern, The `id` in the [props editor](/docs/visual-editing) can now be tweaked and Remotion will refetch the data whenever it changes.++### Aborting stale requests++The props in the props editor may rapidly change for example by typing fast.+It is a good practice to cancel requests which are stale using the `abortSignal` that gets passed to the [`calculateMetadata()`](/docs/composition#calculatemetadata) function:++```tsx twoslash title="src/Root.tsx" {3-6}+// ---cut---+import { CalculateMetadataFunction } from "remotion";++type ApiResponse = {+ title: string;+ description: string;+};+type MyCompProps = {+ id: string;+ data: ApiResponse | null;+};++// ---cut---+export const calculateMyCompMetadata: CalculateMetadataFunction<+ MyCompProps+> = async ({ props, abortSignal }) => {+ const data = await fetch(`https://example.com/api/${props.id}`, {+ signal: abortSignal,+ });+ const json = await data.json();++ return {+ props: {+ ...props,+ data: json,+ },+ };+};++export const MyComp: React.FC<MyCompProps> = () => null;+```++This `abortSignal` is created by Remotion using the [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) API.+## Fetching data during the render-Using [`delayRender()`](/docs/delay-render) and [`continueRender()`](/docs/continue-render) you can tell Remotion to wait for asynchronous operations to finish before rendering a frame.-Call `delayRender()` as soon as possible, for example when initializing the state inside your component.+Using [`delayRender()`](/docs/delay-render) and [`continueRender()`](/docs/continue-render) you can tell Remotion to wait for asynchronous operations to finish before rendering a frame.++### When to use++Use this approach to load assets which are not JSON serializable or if you are using a Remotion version lower than 4.0.++### Usage++Call [`delayRender()`](/docs/delay-render) as soon as possible, for example when initializing the state inside your component.```tsx twoslashimport { useCallback, useEffect, useState } from "react";import { cancelRender, continueRender, delayRender } from "remotion";-export const MyVideo = () => {+export const MyComp = () => {const [data, setData] = useState(null);const [handle] = useState(() => delayRender());@@ -91,20 +316,22 @@ export const MyVideo = () => {Once the data is fetched, you can call [`continueRender()`](/docs/continue-render) to tell Remotion to continue rendering the video.In case the data fetching fails, you can call [`cancelRender()`](/docs/cancel-render) to cancel the render without waiting for the timeout.-## Time limit+### Time limitYou need to clear all handles created by [`delayRender()`](/docs/delay-render) within 30 seconds after the page is opened. You may [increase the timeout](/docs/timeout#increase-timeout).-## Prevent overfetching+### Prevent overfetchingDuring rendering, multiple headless browser tabs are opened to speed up rendering.-This means that if you are fetching data inside your component, the data fetching will be executed multiple times.+In Remotion Lambda, the [rendering concurrency](/docs/lambda/concurrency) may be up to 200x.+This means that if you are fetching data inside your component, the data fetching will be performed many times.-<Step>1</Step> Each tab will execute the data fetching individually, so if you are rendering with a high concurrency, you may run into a rate limit. <br/>-<Step>2</Step> The data returned by an API must be the same when called multiple times, otherwise <a href="/docs/flickering">flickering</a> may occur. <br/>-<Step>3</Step> Consider fetching data before the render, and pass data as <a href="/docs/parametrized-rendering">input props</a> <br/>-<Step>4</Step> Make sure to not have <code>frame</code> as a dependency of the <code>useEffect()</code>, otherwise data will be fetched every frame leading to slowdown and potentially running into rate limits. <br/>+<Step>1</Step> Prefer fetching the data before render if possible. <br/>+<Step>2</Step> Ensure you are entitled to a high request rate without running into a rate limit.<br/>+<Step>3</Step> The data returned by the API must be the same on all threads, otherwise <a href="/docs/flickering">flickering</a> may occur. <br/>+<Step>4</Step> Make sure to not have <code>frame</code> as a dependency of the <code>useEffect()</code>, directly or indirectly, otherwise data will be fetched every frame leading to slowdown and potentially running into rate limits. <br/>## See also-- [delayRender()](/docs/delay-render)+- [`delayRender()`](/docs/delay-render)+- [How props get resolved](/docs/props-resolution)diff --git a/packages/docs/docs/timeout.md b/packages/docs/docs/timeout.mdindex 51af5ece58..652bbae4f7 100644--- a/packages/docs/docs/timeout.md+++ b/packages/docs/docs/timeout.md@@ -57,22 +57,20 @@ Especially 1.x releases could timeout when importing large assets[Open an issue](https://github.com/remotion-dev/remotion/issues/new) and try to describe your issue in a way that is reproducible for us. We will try to help you out.-## Increase timeout+## Increase timeout<AvailableFrom v="2.6.3"/>-Sometimes, you cannot avoid a render taking longer than 30 seconds. For example:+Sometimes, you cannot avoid a frame taking longer than 30 seconds to render. For example:- Expensive WebGL scenes- Expensive preprocessing of data-You can increase the default timeout from v2.6.3 on:+You can increase the default timeout:- Using the [`--timeout`](/docs/cli/render#--timeout) CLI flag- Using the `timeoutInMilliseconds` option in [`renderStill()`](/docs/renderer/render-still#timeoutinmilliseconds), [`renderFrames()`](/docs/renderer/render-frames#timeoutinmilliseconds), [`getCompositions()`](/docs/renderer/get-compositions#timeoutinmilliseconds), [`renderMedia()`](/docs/renderer/render-media#timeoutinmilliseconds), [`renderMediaOnLambda()`](/docs/lambda/rendermediaonlambda#timeoutinmilliseconds) and [`renderStillOnLambda()`](/docs/lambda/renderstillonlambda#timeoutinmilliseconds)- Using the [`Config.setDelayRenderTimeoutInMilliseconds()`](/docs/config#setdelayrendertimeoutinmilliseconds) option in the config file-## Adding a label to help debugging--_Available from v2.6.13_+## Adding a label to help debugging<AvailableFrom v="2.6.13"/>If you encounter a timeout and don't know where it came from, you can add a label as a parameter: