🎥 Make videos programmatically with React
Remove saveSnapshot/restoreSnapshot APIs, inline into template-vercel
Jonny Burger450a9a8cd94b
diff --git a/packages/docs/docs/vercel/add-bundle-to-sandbox.mdx b/packages/docs/docs/vercel/add-bundle-to-sandbox.mdxindex a6c4161af9..6d5a40bad7 100644--- a/packages/docs/docs/vercel/add-bundle-to-sandbox.mdx+++ b/packages/docs/docs/vercel/add-bundle-to-sandbox.mdx@@ -6,9 +6,7 @@ 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.+Copies your Remotion bundle into a sandbox. Call this after [`createSandbox()`](/docs/vercel/create-sandbox) to add your bundle files.## Example@@ -49,5 +47,4 @@ A bundle can be created using the [`npx remotion bundle`](/docs/cli/bundle) comm## 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 0375299004..45c1b2571f 100644--- a/packages/docs/docs/vercel/create-sandbox.mdx+++ b/packages/docs/docs/vercel/create-sandbox.mdx@@ -91,6 +91,4 @@ 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/restore-snapshot.mdx b/packages/docs/docs/vercel/restore-snapshot.mdxdeleted file mode 100644index 8ab3782bbc..0000000000--- a/packages/docs/docs/vercel/restore-snapshot.mdx+++ /dev/null@@ -1,63 +0,0 @@-----image: /generated/articles-docs-vercel-restore-snapshot.png-title: restoreSnapshot()-crumb: '@remotion/vercel'------# restoreSnapshot()<AvailableFrom v="4.0.426" />--Restores a sandbox from a cached snapshot. Reads the snapshot ID from Vercel Blob storage (keyed by `VERCEL_DEPLOYMENT_ID`) and creates a sandbox from it.--This is the production counterpart to [`createSandbox()`](/docs/vercel/create-sandbox). Use `createSandbox()` during development and `restoreSnapshot()` in production.--## Example--```ts twoslash title="route.ts"-// @module: es2022-// @target: es2022-import {addBundleToSandbox, createSandbox, restoreSnapshot} from '@remotion/vercel';-// ---cut----const sandbox = process.env.VERCEL- ? await restoreSnapshot()- : await createSandbox();--if (!process.env.VERCEL) {- await addBundleToSandbox({sandbox, bundleDir: '/path/to/bundle'});-}--// ... use the sandbox--await sandbox.stop();-```--## Arguments--None.--## Return value--A [`VercelSandbox`](/docs/vercel/types#vercelsandbox) object restored from the cached snapshot. Throws if no snapshot is found.--## Using `await using`--You can use `await using` to automatically clean up the sandbox when it goes out of scope:--```ts twoslash title="route.ts"-// @module: es2022-// @target: es2022-import {addBundleToSandbox, createSandbox, restoreSnapshot} from '@remotion/vercel';-// ---cut----await using sandbox = process.env.VERCEL- ? await restoreSnapshot()- : await createSandbox();--if (!process.env.VERCEL) {- await addBundleToSandbox({sandbox, bundleDir: '/path/to/bundle'});-}-```--## See also--- [`createSandbox()`](/docs/vercel/create-sandbox)-- [`saveSnapshot()`](/docs/vercel/save-snapshot)-- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/vercel/src/restore-snapshot.ts)diff --git a/packages/docs/docs/vercel/save-snapshot.mdx b/packages/docs/docs/vercel/save-snapshot.mdxdeleted file mode 100644index df619d1aa9..0000000000--- a/packages/docs/docs/vercel/save-snapshot.mdx+++ /dev/null@@ -1,50 +0,0 @@-----image: /generated/articles-docs-vercel-save-snapshot.png-title: saveSnapshot()-crumb: '@remotion/vercel'------# saveSnapshot()<AvailableFrom v="4.0.426" />--Takes a snapshot of a sandbox and saves the snapshot ID to Vercel Blob storage, keyed by the current `VERCEL_DEPLOYMENT_ID`. This allows [`restoreSnapshot()`](/docs/vercel/restore-snapshot) to restore the sandbox in production without re-creating it.--Typically called during the build step (e.g. in `create-snapshot.ts`) to cache the sandbox for production use.--## Example--```ts twoslash title="create-snapshot.ts"-// @module: es2022-// @target: es2022-import {addBundleToSandbox, createSandbox, saveSnapshot} from '@remotion/vercel';-const sandbox = await createSandbox();-await addBundleToSandbox({sandbox, bundleDir: '/path/to/bundle'});-// ---cut----const {snapshotId} = await saveSnapshot({sandbox});-console.log(`Snapshot saved: ${snapshotId}`);-```--## Arguments--An object with the following properties:--### `sandbox`--A [`Sandbox`](https://vercel.com/docs/vercel-sandbox/sdk-reference#sandbox-class) instance, typically obtained from [`createSandbox()`](/docs/vercel/create-sandbox).--### `expiration?`--The expiration time for the snapshot in seconds. Default: `0` (never expires).--## Return value--An object containing:--### `snapshotId`--A `string` identifier for the snapshot.--## 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/save-snapshot.ts)diff --git a/packages/docs/docs/vercel/table-of-contents.tsx b/packages/docs/docs/vercel/table-of-contents.tsxindex 79ffb66b37..e7a139bd92 100644--- a/packages/docs/docs/vercel/table-of-contents.tsx+++ b/packages/docs/docs/vercel/table-of-contents.tsx@@ -14,14 +14,6 @@ export const TableOfContents: React.FC = () => {<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>- </TOCItem>- <TOCItem link="/docs/vercel/restore-snapshot">- <strong>restoreSnapshot()</strong>- <div>Restore a sandbox from a cached snapshot</div>- </TOCItem><TOCItem link="/docs/vercel/render-video-on-vercel"><strong>renderVideoOnVercel()</strong><div>Render a video in a sandbox</div>diff --git a/packages/docs/docs/vercel/types.mdx b/packages/docs/docs/vercel/types.mdxindex 8379afcddd..1c245e609d 100644--- a/packages/docs/docs/vercel/types.mdx+++ b/packages/docs/docs/vercel/types.mdx@@ -16,7 +16,7 @@ import type {VercelSandbox} from '@remotion/vercel';// ^?```-A [`Sandbox`](https://vercel.com/docs/vercel-sandbox/sdk-reference#sandbox-class) with `AsyncDisposable` support. Returned by [`createSandbox()`](/docs/vercel/create-sandbox) and [`restoreSnapshot()`](/docs/vercel/restore-snapshot).+A [`Sandbox`](https://vercel.com/docs/vercel-sandbox/sdk-reference#sandbox-class) with `AsyncDisposable` support. Returned by [`createSandbox()`](/docs/vercel/create-sandbox).## `CreateSandboxOnProgress`diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.tsindex 7ad5acba1a..79ce168f49 100644--- a/packages/docs/sidebars.ts+++ b/packages/docs/sidebars.ts@@ -727,8 +727,6 @@ const sidebars: SidebarsConfig = {items: ['vercel/create-sandbox','vercel/add-bundle-to-sandbox',- 'vercel/save-snapshot',- 'vercel/restore-snapshot','vercel/render-video-on-vercel','vercel/render-still-on-vercel','vercel/upload-to-vercel-blob',diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.tsindex e760b99e00..0983cbf3c6 100644--- a/packages/docs/src/data/articles.ts+++ b/packages/docs/src/data/articles.ts@@ -6583,24 +6583,6 @@ export const articles = [noAi: false,slug: 'vercel/render-video-on-vercel',},- {- id: 'vercel/restore-snapshot',- title: 'restoreSnapshot()',- relativePath: 'docs/vercel/restore-snapshot.mdx',- compId: 'articles-docs-vercel-restore-snapshot',- crumb: '@remotion/vercel',- noAi: false,- slug: 'vercel/restore-snapshot',- },- {- id: 'vercel/save-snapshot',- title: 'saveSnapshot()',- relativePath: 'docs/vercel/save-snapshot.mdx',- compId: 'articles-docs-vercel-save-snapshot',- crumb: '@remotion/vercel',- noAi: false,- slug: 'vercel/save-snapshot',- },{id: 'vercel-types',title: 'TypeScript Types Reference',diff --git a/packages/template-vercel/create-snapshot.ts b/packages/template-vercel/create-snapshot.tsindex b349dd0ac2..431248e5da 100644--- a/packages/template-vercel/create-snapshot.ts+++ b/packages/template-vercel/create-snapshot.ts@@ -1,4 +1,8 @@-import { addBundleToSandbox, createSandbox, saveSnapshot } from "@remotion/vercel";+import { put } from "@vercel/blob";+import { addBundleToSandbox, createSandbox } from "@remotion/vercel";++const getSnapshotBlobKey = () =>+ `snapshot-cache/${process.env.VERCEL_DEPLOYMENT_ID ?? "local"}.json`;const sandbox = await createSandbox({onProgress: ({ progress, message }) => {@@ -11,6 +15,13 @@ console.log("[create-snapshot] Adding Remotion bundle...");await addBundleToSandbox({ sandbox, bundleDir: ".remotion" });console.log("[create-snapshot] Taking snapshot...");-const { snapshotId } = await saveSnapshot({ sandbox });+const snapshot = await sandbox.snapshot({ expiration: 0 });+const { snapshotId } = snapshot;++await put(getSnapshotBlobKey(), JSON.stringify({ snapshotId }), {+ access: "public",+ contentType: "application/json",+ addRandomSuffix: false,+});console.log(`[create-snapshot] Snapshot saved: ${snapshotId} (never expires)`);diff --git a/packages/template-vercel/src/app/api/render/route.ts b/packages/template-vercel/src/app/api/render/route.tsindex f074038497..59790f8301 100644--- a/packages/template-vercel/src/app/api/render/route.ts+++ b/packages/template-vercel/src/app/api/render/route.ts@@ -2,9 +2,10 @@ import {addBundleToSandbox,createSandbox,renderVideoOnVercel,- restoreSnapshot,uploadToVercelBlob,} from "@remotion/vercel";+import { head } from "@vercel/blob";+import { Sandbox } from "@vercel/sandbox";import { waitUntil } from "@vercel/functions";import { COMP_NAME } from "../../../../types/constants";import { RenderRequest } from "../../../../types/schema";@@ -14,6 +15,41 @@ import {type RenderProgress,} from "./helpers";+const SANDBOX_CREATING_TIMEOUT = 5 * 60 * 1000;++const getSnapshotBlobKey = () =>+ `snapshot-cache/${process.env.VERCEL_DEPLOYMENT_ID ?? "local"}.json`;++async function restoreSnapshot() {+ let snapshotId: string | null = null;++ try {+ const metadata = await head(getSnapshotBlobKey());+ const response = await fetch(metadata.url);+ const cache: { snapshotId: string } = await response.json();+ snapshotId = cache.snapshotId;+ } catch {+ // ignore+ }++ if (!snapshotId) {+ throw new Error(+ "No sandbox snapshot found. Run `bun run create-snapshot` as part of the build process.",+ );+ }++ const sandbox = await Sandbox.create({+ source: { type: "snapshot", snapshotId },+ timeout: SANDBOX_CREATING_TIMEOUT,+ });++ return Object.assign(sandbox, {+ [Symbol.asyncDispose]: async () => {+ await sandbox.stop().catch(() => {});+ },+ });+}+export async function POST(req: Request) {const encoder = new TextEncoder();const stream = new TransformStream();@@ -54,7 +90,6 @@ export async function POST(req: Request) {sandbox,compositionId: COMP_NAME,inputProps: body.inputProps,- bundleDir: ".remotion",onProgress: (update) => {switch (update.type) {case "opening-browser":diff --git a/packages/vercel/src/index.ts b/packages/vercel/src/index.tsindex b5717cb26e..b2dae89b0f 100644--- a/packages/vercel/src/index.ts+++ b/packages/vercel/src/index.ts@@ -2,8 +2,6 @@ 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';-export {restoreSnapshot} from './restore-snapshot';-export {saveSnapshot} from './save-snapshot';export type {AudioCodec,Bitrate,diff --git a/packages/vercel/src/restore-snapshot.ts b/packages/vercel/src/restore-snapshot.tsdeleted file mode 100644index 7c5b778408..0000000000--- a/packages/vercel/src/restore-snapshot.ts+++ /dev/null@@ -1,35 +0,0 @@-import {head} from '@vercel/blob';-import {SANDBOX_CREATING_TIMEOUT} from './create-sandbox';-import {createDisposableSandbox} from './internals/disposable';-import type {VercelSandbox} from './types';--type SnapshotCache = {- snapshotId: string;-};--const getSnapshotBlobKey = () =>- `snapshot-cache/${process.env.VERCEL_DEPLOYMENT_ID ?? 'local'}.json`;--export async function restoreSnapshot(): Promise<VercelSandbox> {- let snapshotId: string | null = null;-- try {- const metadata = await head(getSnapshotBlobKey());- const response = await fetch(metadata.url);- const cache: SnapshotCache = await response.json();- snapshotId = cache.snapshotId;- } catch {- // ignore- }-- if (!snapshotId) {- throw new Error(- 'No sandbox snapshot found. Run `bun run create-snapshot` as part of the build process.',- );- }-- return createDisposableSandbox({- source: {type: 'snapshot', snapshotId},- timeout: SANDBOX_CREATING_TIMEOUT,- });-}diff --git a/packages/vercel/src/save-snapshot.ts b/packages/vercel/src/save-snapshot.tsdeleted file mode 100644index 3bdcd86f21..0000000000--- a/packages/vercel/src/save-snapshot.ts+++ /dev/null@@ -1,32 +0,0 @@-import {put} from '@vercel/blob';-import type {Sandbox} from '@vercel/sandbox';--type SnapshotCache = {- snapshotId: string;-};--const getSnapshotBlobKey = () =>- `snapshot-cache/${process.env.VERCEL_DEPLOYMENT_ID ?? 'local'}.json`;--export async function saveSnapshot({- sandbox,- expiration = 0,-}: {- sandbox: Sandbox;- expiration?: number;-}): Promise<{snapshotId: string}> {- const snapshot = await sandbox.snapshot({expiration});- const {snapshotId} = snapshot;-- const cache: SnapshotCache = {- snapshotId,- };-- await put(getSnapshotBlobKey(), JSON.stringify(cache), {- access: 'public',- contentType: 'application/json',- addRandomSuffix: false,- });-- return {snapshotId};-}