π₯ Make videos programmatically with React
`@remotion/vercel`: Extract `addBundleToSandbox()` as a public API
Jonny Burgerc1a3f81e8e9f
diff --git a/packages/docs/docs/vercel/add-bundle-to-sandbox.mdx b/packages/docs/docs/vercel/add-bundle-to-sandbox.mdxnew file mode 100644index 0000000000..e1a09cf88d--- /dev/null+++ b/packages/docs/docs/vercel/add-bundle-to-sandbox.mdx@@ -0,0 +1,52 @@+---+image: /generated/articles-docs-vercel-add-bundle-to-sandbox.png+title: addBundleToSandbox()+crumb: '@remotion/vercel'+---++# addBundleToSandbox()<AvailableFrom v="4.0.426" />++Copies your Remotion bundle into a sandbox. Call this after [`createSandbox()`](/docs/vercel/create-sandbox) during development to add your bundle files.++In production, prefer [`restoreSnapshot()`](/docs/vercel/restore-snapshot) which already includes the bundle.++## Example++```ts twoslash title="create-snapshot.ts"+// @module: es2022+// @target: es2022+import {addBundleToSandbox, createSandbox} from '@remotion/vercel';+// ---cut---+const sandbox = await createSandbox();++await addBundleToSandbox({+ sandbox,+ bundleDir: '.remotion',+});++// ... use the sandbox++await sandbox.stop();+```++## Arguments++An object with the following properties:++### `sandbox`++A [`VercelSandbox`](/docs/vercel/types#vercelsandbox) instance, typically obtained from [`createSandbox()`](/docs/vercel/create-sandbox).++### `bundleDir`++The path to your Remotion bundle directory, relative to the current working directory. This is the output of [`npx remotion bundle`](/docs/cli/bundle).++## Return value++`Promise<void>` β resolves when the bundle has been copied into the sandbox.++## See also++- [`createSandbox()`](/docs/vercel/create-sandbox)+- [`restoreSnapshot()`](/docs/vercel/restore-snapshot)+- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/vercel/src/add-bundle-to-sandbox.ts)diff --git a/packages/docs/docs/vercel/create-sandbox.mdx b/packages/docs/docs/vercel/create-sandbox.mdxindex 5d4cd28642..ae9e1bc6ba 100644--- a/packages/docs/docs/vercel/create-sandbox.mdx+++ b/packages/docs/docs/vercel/create-sandbox.mdx@@ -6,7 +6,9 @@ crumb: '@remotion/vercel'# createSandbox()<AvailableFrom v="4.0.426" />-Creates a new [Vercel Sandbox](https://vercel.com/docs/vercel-sandbox) with all Remotion dependencies installed, including system libraries, the compositor, a browser, and your Remotion bundle.+Creates a new [Vercel Sandbox](https://vercel.com/docs/vercel-sandbox) with all Remotion dependencies installed, including system libraries, the compositor, and a browser.++After creating the sandbox, call [`addBundleToSandbox()`](/docs/vercel/add-bundle-to-sandbox) to copy your Remotion bundle into it.Creates a sandbox from scratch. In production, prefer [`restoreSnapshot()`](/docs/vercel/restore-snapshot) to restore from a cached snapshot instead.@@ -15,16 +17,20 @@ Creates a sandbox from scratch. In production, prefer [`restoreSnapshot()`](/doc```ts twoslash title="create-snapshot.ts"// @module: es2022// @target: es2022-import {createSandbox} from '@remotion/vercel';+import {addBundleToSandbox, createSandbox} from '@remotion/vercel';import type {CreateSandboxOnProgress} from '@remotion/vercel';// ---cut---const sandbox = await createSandbox({- bundleDir: '.remotion',onProgress: ({progress, message}) => {console.log(`${message} (${Math.round(progress * 100)}%)`);},});+await addBundleToSandbox({+ sandbox,+ bundleDir: '.remotion',+});+// ... use the sandboxawait sandbox.stop();@@ -34,10 +40,6 @@ await sandbox.stop();An object with the following properties:-### `bundleDir`--The path to your Remotion bundle directory, relative to the current working directory. This is the output of [`npx remotion bundle`](/docs/cli/bundle).-### `onProgress?`A callback that receives progress updates during sandbox creation.@@ -66,13 +68,12 @@ You can use `await using` to automatically clean up the sandbox when it goes out// @target: es2022import {createSandbox} from '@remotion/vercel';// ---cut----await using sandbox = await createSandbox({- bundleDir: '.remotion',-});+await using sandbox = await createSandbox();```## See also+- [`addBundleToSandbox()`](/docs/vercel/add-bundle-to-sandbox)- [`restoreSnapshot()`](/docs/vercel/restore-snapshot)- [`saveSnapshot()`](/docs/vercel/save-snapshot)- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/vercel/src/create-sandbox.ts)diff --git a/packages/docs/docs/vercel/render-still-on-vercel.mdx b/packages/docs/docs/vercel/render-still-on-vercel.mdxindex ef84bebfdd..1783d6d17d 100644--- a/packages/docs/docs/vercel/render-still-on-vercel.mdx+++ b/packages/docs/docs/vercel/render-still-on-vercel.mdx@@ -15,8 +15,9 @@ The rendered file stays inside the sandbox. Use [`uploadToVercelBlob()`](/docs/v```ts twoslash title="route.ts"// @module: es2022// @target: es2022-import {renderStillOnVercel, createSandbox} from '@remotion/vercel';-const sandbox = await createSandbox({bundleDir: '.remotion'});+import {renderStillOnVercel, addBundleToSandbox, createSandbox} from '@remotion/vercel';+const sandbox = await createSandbox();+await addBundleToSandbox({sandbox, bundleDir: '.remotion'});// ---cut---const {file} = await renderStillOnVercel({sandbox,diff --git a/packages/docs/docs/vercel/render-video-on-vercel.mdx b/packages/docs/docs/vercel/render-video-on-vercel.mdxindex 8d8c0a61f3..f001abfe56 100644--- a/packages/docs/docs/vercel/render-video-on-vercel.mdx+++ b/packages/docs/docs/vercel/render-video-on-vercel.mdx@@ -15,8 +15,9 @@ The rendered file stays inside the sandbox. Use [`uploadToVercelBlob()`](/docs/v```ts twoslash title="route.ts"// @module: es2022// @target: es2022-import {renderVideoOnVercel, createSandbox} from '@remotion/vercel';-const sandbox = await createSandbox({bundleDir: '.remotion'});+import {renderVideoOnVercel, addBundleToSandbox, createSandbox} from '@remotion/vercel';+const sandbox = await createSandbox();+await addBundleToSandbox({sandbox, bundleDir: '.remotion'});// ---cut---const {file} = await renderVideoOnVercel({sandbox,diff --git a/packages/docs/docs/vercel/restore-snapshot.mdx b/packages/docs/docs/vercel/restore-snapshot.mdxindex 5eb2512da9..d8c02ae7ef 100644--- a/packages/docs/docs/vercel/restore-snapshot.mdx+++ b/packages/docs/docs/vercel/restore-snapshot.mdx@@ -15,11 +15,15 @@ This is the production counterpart to [`createSandbox()`](/docs/vercel/create-sa```ts twoslash title="route.ts"// @module: es2022// @target: es2022-import {createSandbox, restoreSnapshot} from '@remotion/vercel';+import {addBundleToSandbox, createSandbox, restoreSnapshot} from '@remotion/vercel';// ---cut---const sandbox = process.env.VERCEL? await restoreSnapshot()- : await createSandbox({bundleDir: '.remotion'});+ : await createSandbox();++if (!process.env.VERCEL) {+ await addBundleToSandbox({sandbox, bundleDir: '.remotion'});+}// ... use the sandbox@@ -41,11 +45,15 @@ You can use `await using` to automatically clean up the sandbox when it goes out```ts twoslash title="route.ts"// @module: es2022// @target: es2022-import {createSandbox, restoreSnapshot} from '@remotion/vercel';+import {addBundleToSandbox, createSandbox, restoreSnapshot} from '@remotion/vercel';// ---cut---await using sandbox = process.env.VERCEL? await restoreSnapshot()- : await createSandbox({bundleDir: '.remotion'});+ : await createSandbox();++if (!process.env.VERCEL) {+ await addBundleToSandbox({sandbox, bundleDir: '.remotion'});+}```## See alsodiff --git a/packages/docs/docs/vercel/save-snapshot.mdx b/packages/docs/docs/vercel/save-snapshot.mdxindex b256765238..3dd2e82566 100644--- a/packages/docs/docs/vercel/save-snapshot.mdx+++ b/packages/docs/docs/vercel/save-snapshot.mdx@@ -15,8 +15,9 @@ Typically called during the build step (e.g. in `create-snapshot.ts`) to cache t```ts twoslash title="create-snapshot.ts"// @module: es2022// @target: es2022-import {createSandbox, saveSnapshot} from '@remotion/vercel';-const sandbox = await createSandbox({bundleDir: '.remotion'});+import {addBundleToSandbox, createSandbox, saveSnapshot} from '@remotion/vercel';+const sandbox = await createSandbox();+await addBundleToSandbox({sandbox, bundleDir: '.remotion'});// ---cut---const {snapshotId} = await saveSnapshot({sandbox});console.log(`Snapshot saved: ${snapshotId}`);diff --git a/packages/docs/docs/vercel/table-of-contents.tsx b/packages/docs/docs/vercel/table-of-contents.tsxindex 2ede6858f4..79ffb66b37 100644--- a/packages/docs/docs/vercel/table-of-contents.tsx+++ b/packages/docs/docs/vercel/table-of-contents.tsx@@ -10,6 +10,10 @@ export const TableOfContents: React.FC = () => {<strong>createSandbox()</strong><div>Create a sandbox with Remotion installed</div></TOCItem>+ <TOCItem link="/docs/vercel/add-bundle-to-sandbox">+ <strong>addBundleToSandbox()</strong>+ <div>Copy a Remotion bundle into a sandbox</div>+ </TOCItem><TOCItem link="/docs/vercel/save-snapshot"><strong>saveSnapshot()</strong><div>Snapshot a sandbox and save to Vercel Blob</div>diff --git a/packages/docs/docs/vercel/upload-to-vercel-blob.mdx b/packages/docs/docs/vercel/upload-to-vercel-blob.mdxindex 6b5e28ac9d..9774c0a95b 100644--- a/packages/docs/docs/vercel/upload-to-vercel-blob.mdx+++ b/packages/docs/docs/vercel/upload-to-vercel-blob.mdx@@ -13,8 +13,9 @@ Uploads a file from the sandbox to [Vercel Blob](https://vercel.com/docs/storage```ts twoslash title="route.ts"// @module: es2022// @target: es2022-import {uploadToVercelBlob, createSandbox} from '@remotion/vercel';-const sandbox = await createSandbox({bundleDir: '.remotion'});+import {uploadToVercelBlob, addBundleToSandbox, createSandbox} from '@remotion/vercel';+const sandbox = await createSandbox();+await addBundleToSandbox({sandbox, bundleDir: '.remotion'});// ---cut---const {url, size} = await uploadToVercelBlob({sandbox,diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.tsindex 3d32302a6c..4d1ab55537 100644--- a/packages/docs/sidebars.ts+++ b/packages/docs/sidebars.ts@@ -469,6 +469,7 @@ const sidebars: SidebarsConfig = {},items: ['vercel/create-sandbox',+ 'vercel/add-bundle-to-sandbox','vercel/save-snapshot','vercel/restore-snapshot','vercel/render-video-on-vercel',diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.tsindex 8b7b70ff3e..e760b99e00 100644--- a/packages/docs/src/data/articles.ts+++ b/packages/docs/src/data/articles.ts@@ -6538,6 +6538,15 @@ export const articles = [noAi: false,slug: 'vercel',},+ {+ id: 'vercel/add-bundle-to-sandbox',+ title: 'addBundleToSandbox()',+ relativePath: 'docs/vercel/add-bundle-to-sandbox.mdx',+ compId: 'articles-docs-vercel-add-bundle-to-sandbox',+ crumb: '@remotion/vercel',+ noAi: false,+ slug: 'vercel/add-bundle-to-sandbox',+ },{id: 'vercel/create-sandbox',title: 'createSandbox()',diff --git a/packages/template-vercel/create-snapshot.ts b/packages/template-vercel/create-snapshot.tsindex 77952f9bf8..b349dd0ac2 100644--- a/packages/template-vercel/create-snapshot.ts+++ b/packages/template-vercel/create-snapshot.ts@@ -1,13 +1,15 @@-import { createSandbox, saveSnapshot } from "@remotion/vercel";+import { addBundleToSandbox, createSandbox, saveSnapshot } from "@remotion/vercel";const sandbox = await createSandbox({- bundleDir: ".remotion",onProgress: ({ progress, message }) => {const pct = Math.round(progress * 100);console.log(`[create-snapshot] ${message} (${pct}%)`);},});+console.log("[create-snapshot] Adding Remotion bundle...");+await addBundleToSandbox({ sandbox, bundleDir: ".remotion" });+console.log("[create-snapshot] Taking snapshot...");const { snapshotId } = await saveSnapshot({ sandbox });diff --git a/packages/template-vercel/src/app/api/render/route.ts b/packages/template-vercel/src/app/api/render/route.tsindex 7a2188bc36..f074038497 100644--- a/packages/template-vercel/src/app/api/render/route.ts+++ b/packages/template-vercel/src/app/api/render/route.ts@@ -1,4 +1,5 @@import {+ addBundleToSandbox,createSandbox,renderVideoOnVercel,restoreSnapshot,@@ -35,7 +36,6 @@ export async function POST(req: Request) {await using sandbox = process.env.VERCEL? await restoreSnapshot(): await createSandbox({- bundleDir: ".remotion",onProgress: ({ progress, message }) => {send({type: "phase",@@ -46,6 +46,10 @@ export async function POST(req: Request) {},});+ if (!process.env.VERCEL) {+ await addBundleToSandbox({ sandbox, bundleDir: ".remotion" });+ }+const { file } = await renderVideoOnVercel({sandbox,compositionId: COMP_NAME,diff --git a/packages/vercel/src/add-bundle-to-sandbox.ts b/packages/vercel/src/add-bundle-to-sandbox.tsnew file mode 100644index 0000000000..c45888ecea--- /dev/null+++ b/packages/vercel/src/add-bundle-to-sandbox.ts@@ -0,0 +1,12 @@+import {addBundleToSandbox as addBundleInternal} from './internals/add-bundle';+import type {VercelSandbox} from './types';++export async function addBundleToSandbox({+ sandbox,+ bundleDir,+}: {+ sandbox: VercelSandbox;+ bundleDir: string;+}): Promise<void> {+ await addBundleInternal({sandbox, bundleDir});+}diff --git a/packages/vercel/src/create-sandbox.ts b/packages/vercel/src/create-sandbox.tsindex e8ea27e1dd..ac8d8f2331 100644--- a/packages/vercel/src/create-sandbox.ts+++ b/packages/vercel/src/create-sandbox.ts@@ -1,4 +1,3 @@-import {addBundleToSandbox} from './internals/add-bundle';import {createDisposableSandbox} from './internals/disposable';import {installBrowser} from './internals/install-browser';import {installJsDependencies} from './internals/install-js-dependencies';@@ -9,12 +8,10 @@ import type {CreateSandboxOnProgress, VercelSandbox} from './types';export const SANDBOX_CREATING_TIMEOUT = 5 * 60 * 1000;export async function createSandbox({- bundleDir,onProgress,}: {- bundleDir: string;onProgress?: CreateSandboxOnProgress;-}): Promise<VercelSandbox> {+} = {}): Promise<VercelSandbox> {const report = (progress: number, message: string) => {onProgress?.({progress, message});};@@ -25,17 +22,15 @@ export async function createSandbox({timeout: SANDBOX_CREATING_TIMEOUT,});- // Preparation has 3 stages with weights:- // - System dependencies: 60%- // - Copying bundle: 20%- // - Downloading browser: 20%- const WEIGHT_SYS_DEPS = 0.6;- const WEIGHT_BUNDLE = 0.2;- const WEIGHT_BROWSER = 0.2;+ // Preparation has 2 stages with weights:+ // - System dependencies: 75%+ // - Downloading browser: 25%+ const WEIGHT_SYS_DEPS = 0.75;+ const WEIGHT_BROWSER = 0.25;report(0, 'Installing system dependencies...');- // Stage 1: Install system dependencies (60%)+ // Stage 1: Install system dependencies (75%)await installSystemDependencies({sandbox,onProgress: (stageProgress: number) => {@@ -47,12 +42,7 @@ export async function createSandbox({},});- report(WEIGHT_SYS_DEPS, 'Adding Remotion bundle to sandbox...');-- // Stage 2: Copy Remotion bundle (20%)- await addBundleToSandbox({sandbox, bundleDir});-- report(WEIGHT_SYS_DEPS + WEIGHT_BUNDLE, 'Installing JS dependencies...');+ report(WEIGHT_SYS_DEPS, 'Installing JS dependencies...');// Install renderer and blob SDKawait installJsDependencies({sandbox});@@ -60,13 +50,13 @@ export async function createSandbox({// Patch compositor binary for glibc 2.34 compatibility (Amazon Linux 2023)await patchCompositor({sandbox});- // Stage 3: Download browser (20%)- report(WEIGHT_SYS_DEPS + WEIGHT_BUNDLE, 'Downloading browser...');+ // Stage 2: Download browser (25%)+ report(WEIGHT_SYS_DEPS, 'Downloading browser...');await installBrowser({sandbox,onProgress: (browserProgress: number) => {report(- WEIGHT_SYS_DEPS + WEIGHT_BUNDLE + browserProgress * WEIGHT_BROWSER,+ WEIGHT_SYS_DEPS + browserProgress * WEIGHT_BROWSER,'Downloading browser...',);return Promise.resolve();diff --git a/packages/vercel/src/index.ts b/packages/vercel/src/index.tsindex 9ef1a5a6f7..b5717cb26e 100644--- a/packages/vercel/src/index.ts+++ b/packages/vercel/src/index.ts@@ -1,3 +1,4 @@+export {addBundleToSandbox} from './add-bundle-to-sandbox';export {createSandbox} from './create-sandbox';export {renderStillOnVercel} from './render-still-on-vercel';export {renderVideoOnVercel} from './render-video-on-vercel';