π₯ Make videos programmatically with React
improve documentation
Jonny Burger30b5e89bbc4b
diff --git a/packages/core/src/ready-manager.ts b/packages/core/src/ready-manager.tsindex 9005c1dba0..82d13de841 100644--- a/packages/core/src/ready-manager.ts+++ b/packages/core/src/ready-manager.ts@@ -11,15 +11,15 @@ const timeouts: {[key: string]: number | NodeJS.Timeout} = {};/*** Call this function to tell Remotion to wait before capturing this frame until data has loaded. Use continueRender() to unblock the render.- * @param description _optional_ A description to identify the call in case it does time out.+ * @param label _optional_ A label to identify the call in case it does time out.* @returns {number} An identifier to be passed to continueRender().* @link https://www.remotion.dev/docs/delay-render*/-export const delayRender = (description?: string): number => {- if (typeof description !== 'string' && typeof description !== 'undefined') {+export const delayRender = (label?: string): number => {+ if (typeof label !== 'string' && typeof label !== 'undefined') {throw new Error(- 'The description parameter of delayRender() must be a string or undefined, got: ' +- JSON.stringify(description)+ 'The label parameter of delayRender() must be a string or undefined, got: ' ++ JSON.stringify(label));}@@ -35,7 +35,7 @@ export const delayRender = (description?: string): number => {timeouts[handle] = setTimeout(() => {const message = [`A delayRender()`,- description ? `"${description}"` : null,+ label ? `"${label}"` : null,`was called but not cleared after ${timeoutToUse}ms. See https://remotion.dev/docs/timeout for help. The delayRender was called: ${called}`,].filter(truthy)diff --git a/packages/docs/docs/delay-render.md b/packages/docs/docs/delay-render.mdindex bddc50c32f..3a08b11651 100644--- a/packages/docs/docs/delay-render.md+++ b/packages/docs/docs/delay-render.md@@ -6,9 +6,9 @@ sidebar_label: delayRender()By calling `delayRender()`, you are signaling that a frame should not be immediately rendered and instead should wait on an asynchronous task to complete.-This method is useful if you for example want to call an API to fetch data before you render.+This method is useful if you want to call an API to fetch data before you render.-`delayRender()` returns an identifier. Once you have fetched data or finished the asynchronous task, you should call `continueRender(identifier)` to let Remotion know that you are now ready to render.+`delayRender()` returns a handle. Once you have fetched data or finished the asynchronous task, you should call `continueRender(handle)` to let Remotion know that you are now ready to render.## Example@@ -42,15 +42,83 @@ export const MyVideo = () => {};```-## Useful to know+## Timeout-- You need to call `continueRender()` within 30 seconds of page load. This is the default timeout of puppeteer and it will throw if you miss to call `continueRender()`.+You need to call `continueRender()` within 30 seconds of page load. This is the default timeout of puppeteer and it will throw if you miss to call `continueRender()`. You can [customize the timeout](/docs/timeout#increase-timeout).-- For every frame that is rendered, the whole page is reloaded, so the whole lifecycle of `delayRender` and `continueRender` happens on every frame. If you are doing an API request, you can speed up the render and avoid rate limits by caching the request, for example by storing the data in `localStorage`.+If `continueRender()` is not called within the timeout frame, the render will fail with an exception similarly to this:-- You can call `delayRender` multiple times. The render will be blocked for as long as at least one blocking handle exists and that has not been cleared by `continueRender()`.+```+A delayRender() was called but not cleared after 28000ms. See https://remotion.dev/docs/timeout for help. The delayRender was called+```++See the [Timeout](/docs/timeout) page to troubleshoot timeouts.++## Adding a label++If you encounter a timeout and don't know where it came from, you can add a label as a parameter:++```tsx twoslash+import { delayRender } from "remotion";++// ---cut---++delayRender("Fetching data from API...");+```++If the call times out, the label will be referenced in the error message:++```+Uncaught Error: A delayRender() "Fetching data from API..." was called but not cleared after 28000ms. See https://remotion.dev/docs/timeout for help. The delayRender was called+```++## Concurrency++Multiple pages are used for rendering, so delayRender() can be called multiple times for a render. If you are doing an API request, you can speed up the render and avoid rate limits by caching the request, for example by storing the data in `localStorage`.++## Multiple calls++You can call `delayRender()` multiple times. The render will be blocked for as long as at least one blocking handle exists and that has not been cleared by `continueRender()`.++```tsx twoslash+import { useEffect, useState } from "react";+import { continueRender, delayRender } from "remotion";-- You should put `delayRender()` calls inside your components rather than placing them as a top-level statement, to avoid blocking a render if a different composition is rendered. Also, in the example below the call is wrapped in a `useState()` to avoid creating multiple blocking calls when the component rerenders.+const MyComp: React.FC = () => {+ const [handle1] = useState(() => delayRender());+ const [handle2] = useState(() => delayRender());++ useEffect(() => {+ // You need to clear all handles before the render continues+ continueRender(handle1);+ continueRender(handle2);+ }, []);++ return null;+};+```++## Encapsulation++You should put `delayRender()` calls inside your components rather than placing them as a top-level statement, to avoid blocking a render if a different composition is rendered. Also, in the example below the call is wrapped in a `useState()` to avoid creating multiple blocking calls when the component rerenders.++```tsx twoslash title="β Don't do this" {4-7}+import { useEffect } from "react";+import { continueRender, delayRender } from "remotion";++// Don't call a delayRender() call outside a component -+// it will block the render if a different composition is rendered+// as well as block the fetching of the list of compositions.+const handle = delayRender();++const MyComp: React.FC = () => {+ useEffect(() => {+ continueRender(handle);+ }, []);++ return null;+};+```## See also