Skip to content

🎥 Make videos programmatically with React

`@remotion/vercel`: Simplify API - merge createSnapshot into saveSnapshot, replace getOrCreateSandbox/getSnapshot with restoreSnapshot

Jonny Burger8e35ec4ba5f0
diff --git a/packages/docs/docs/vercel/create-sandbox.mdx b/packages/docs/docs/vercel/create-sandbox.mdx
index 7e19a86c32..4470666c4e 100644
--- a/packages/docs/docs/vercel/create-sandbox.mdx
+++ b/packages/docs/docs/vercel/create-sandbox.mdx
@@ -8,7 +8,7 @@ crumb: '@remotion/vercel'
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.
-This is a lower-level API. For most use cases, prefer [`getOrCreateSandbox()`](/docs/vercel/get-or-create-sandbox) which handles dev/prod routing automatically.
+Creates a sandbox from scratch. In production, prefer [`restoreSnapshot()`](/docs/vercel/restore-snapshot) to restore from a cached snapshot instead.
## Example
@@ -55,6 +55,6 @@ A `VercelSandbox` object (a [`Sandbox`](https://vercel.com/docs/vercel-sandbox/s
## See also
-- [`getOrCreateSandbox()`](/docs/vercel/get-or-create-sandbox)
-- [`createSnapshot()`](/docs/vercel/create-snapshot)
+- [`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/create-snapshot.mdx b/packages/docs/docs/vercel/create-snapshot.mdx
deleted file mode 100644
index 8a5f6ac2ad..0000000000
--- a/packages/docs/docs/vercel/create-snapshot.mdx
+++ /dev/null
@@ -1,45 +0,0 @@
----
-image: /generated/articles-docs-vercel-create-snapshot.png
-title: createSnapshot()
-crumb: '@remotion/vercel'
----
-
-# createSnapshot()<AvailableFrom v="4.0.425" />
-
-Takes a snapshot of a sandbox. Snapshots allow you to skip the sandbox setup time in production by restoring from a pre-built state.
-
-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 {createSnapshot, createSandbox} from '@remotion/vercel';
-const sandbox = await createSandbox({bundleDir: '.remotion'});
-// ---cut---
-const {snapshotId} = await createSnapshot({sandbox});
-console.log(`Snapshot created: ${snapshotId}`);
-```
-
-## Arguments
-
-An object with the following properties:
-
-### `sandbox`
-
-A `VercelSandbox` instance, typically obtained from [`createSandbox()`](/docs/vercel/create-sandbox).
-
-## Return value
-
-An object containing:
-
-### `snapshotId`
-
-A `string` identifier for the snapshot. Pass this to [`saveSnapshot()`](/docs/vercel/save-snapshot) to cache it for production use.
-
-## See also
-
-- [`saveSnapshot()`](/docs/vercel/save-snapshot)
-- [`createSandbox()`](/docs/vercel/create-sandbox)
-- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/vercel/src/create-snapshot.ts)
diff --git a/packages/docs/docs/vercel/get-or-create-sandbox.mdx b/packages/docs/docs/vercel/get-or-create-sandbox.mdx
deleted file mode 100644
index c6db54c71d..0000000000
--- a/packages/docs/docs/vercel/get-or-create-sandbox.mdx
+++ /dev/null
@@ -1,49 +0,0 @@
----
-image: /generated/articles-docs-vercel-get-or-create-sandbox.png
-title: getOrCreateSandbox()
-crumb: '@remotion/vercel'
----
-
-# getOrCreateSandbox()<AvailableFrom v="4.0.425" />
-
-The primary entry point for getting a sandbox. Automatically handles the dev/prod routing:
-
-- **Development** (no `VERCEL` env var): Creates a fresh sandbox from scratch using [`createSandbox()`](/docs/vercel/create-sandbox).
-- **Production** (on Vercel): Restores a sandbox from a cached snapshot. Throws an error if no snapshot is found.
-
-## Example
-
-```ts twoslash title="route.ts"
-// @module: es2022
-// @target: es2022
-import {getOrCreateSandbox} from '@remotion/vercel';
-// ---cut---
-await using sandbox = await getOrCreateSandbox({
- bundleDir: '.remotion',
- onProgress: ({progress, message}) => {
- console.log(`${message} (${Math.round(progress * 100)}%)`);
- },
-});
-```
-
-## Arguments
-
-An object with the following properties:
-
-### `bundleDir`
-
-The path to your Remotion bundle directory, relative to the current working directory.
-
-### `onProgress?`
-
-A callback of type [`CreateSandboxOnProgress`](/docs/vercel/create-sandbox#onprogress) that receives progress updates. Only called during development when a fresh sandbox is created.
-
-## Return value
-
-A `VercelSandbox` object.
-
-## See also
-
-- [`createSandbox()`](/docs/vercel/create-sandbox)
-- [`getSnapshot()`](/docs/vercel/get-snapshot)
-- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/vercel/src/get-or-create-sandbox.ts)
diff --git a/packages/docs/docs/vercel/get-snapshot.mdx b/packages/docs/docs/vercel/get-snapshot.mdx
deleted file mode 100644
index d6ef33a1f0..0000000000
--- a/packages/docs/docs/vercel/get-snapshot.mdx
+++ /dev/null
@@ -1,38 +0,0 @@
----
-image: /generated/articles-docs-vercel-get-snapshot.png
-title: getSnapshot()
-crumb: '@remotion/vercel'
----
-
-# getSnapshot()<AvailableFrom v="4.0.425" />
-
-Retrieves a cached snapshot ID from Vercel Blob storage. Returns `null` if no snapshot is found or if not running on Vercel.
-
-Used internally by [`getOrCreateSandbox()`](/docs/vercel/get-or-create-sandbox) to check for a cached sandbox.
-
-## Example
-
-```ts twoslash title="route.ts"
-// @module: es2022
-// @target: es2022
-import {getSnapshot} from '@remotion/vercel';
-// ---cut---
-const snapshotId = await getSnapshot();
-if (snapshotId) {
- console.log(`Found cached snapshot: ${snapshotId}`);
-}
-```
-
-## Arguments
-
-None.
-
-## Return value
-
-`string | null` - The cached snapshot ID, or `null` if not found.
-
-## See also
-
-- [`saveSnapshot()`](/docs/vercel/save-snapshot)
-- [`getOrCreateSandbox()`](/docs/vercel/get-or-create-sandbox)
-- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/vercel/src/get-snapshot.ts)
diff --git a/packages/docs/docs/vercel/render-still-on-vercel.mdx b/packages/docs/docs/vercel/render-still-on-vercel.mdx
index a22254a8b4..7391f8db77 100644
--- a/packages/docs/docs/vercel/render-still-on-vercel.mdx
+++ b/packages/docs/docs/vercel/render-still-on-vercel.mdx
@@ -15,8 +15,8 @@ The rendered file stays inside the sandbox. Use [`uploadToBlobStorage()`](/docs/
```ts twoslash title="route.ts"
// @module: es2022
// @target: es2022
-import {renderStillOnVercel, getOrCreateSandbox} from '@remotion/vercel';
-const sandbox = await getOrCreateSandbox({bundleDir: '.remotion'});
+import {renderStillOnVercel, createSandbox} from '@remotion/vercel';
+const sandbox = await createSandbox({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.mdx
index 06b39e9b41..abe1df65fe 100644
--- a/packages/docs/docs/vercel/render-video-on-vercel.mdx
+++ b/packages/docs/docs/vercel/render-video-on-vercel.mdx
@@ -15,8 +15,8 @@ The rendered file stays inside the sandbox. Use [`uploadToBlobStorage()`](/docs/
```ts twoslash title="route.ts"
// @module: es2022
// @target: es2022
-import {renderVideoOnVercel, getOrCreateSandbox} from '@remotion/vercel';
-const sandbox = await getOrCreateSandbox({bundleDir: '.remotion'});
+import {renderVideoOnVercel, createSandbox} from '@remotion/vercel';
+const sandbox = await createSandbox({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.mdx
new file mode 100644
index 0000000000..f3214bc2b4
--- /dev/null
+++ b/packages/docs/docs/vercel/restore-snapshot.mdx
@@ -0,0 +1,37 @@
+---
+image: /generated/articles-docs-vercel-restore-snapshot.png
+title: restoreSnapshot()
+crumb: '@remotion/vercel'
+---
+
+# restoreSnapshot()<AvailableFrom v="4.0.425" />
+
+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 {createSandbox, restoreSnapshot} from '@remotion/vercel';
+// ---cut---
+await using sandbox = process.env.VERCEL
+ ? await restoreSnapshot()
+ : await createSandbox({bundleDir: '.remotion'});
+```
+
+## Arguments
+
+None.
+
+## Return value
+
+A [`VercelSandbox`](/docs/vercel/types#vercelsandbox) object restored from the cached snapshot. Throws if no snapshot is found.
+
+## 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.mdx
index 8e0a8ae0e7..5c311eb1cc 100644
--- a/packages/docs/docs/vercel/save-snapshot.mdx
+++ b/packages/docs/docs/vercel/save-snapshot.mdx
@@ -6,32 +6,44 @@ crumb: '@remotion/vercel'
# saveSnapshot()<AvailableFrom v="4.0.425" />
-Saves a snapshot ID to Vercel Blob storage, keyed by the current `VERCEL_DEPLOYMENT_ID`. This allows [`getOrCreateSandbox()`](/docs/vercel/get-or-create-sandbox) to restore the sandbox in production without re-creating it.
+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 {saveSnapshot} from '@remotion/vercel';
+import {createSandbox, saveSnapshot} from '@remotion/vercel';
+const sandbox = await createSandbox({bundleDir: '.remotion'});
// ---cut---
-await saveSnapshot({snapshotId: 'snap_abc123'});
+const {snapshotId} = await saveSnapshot({sandbox});
+console.log(`Snapshot saved: ${snapshotId}`);
```
## Arguments
An object with the following properties:
-### `snapshotId`
+### `sandbox`
+
+A `VercelSandbox` instance, typically obtained from [`createSandbox()`](/docs/vercel/create-sandbox).
+
+### `expiration?`
-The snapshot ID returned by [`createSnapshot()`](/docs/vercel/create-snapshot).
+The expiration time for the snapshot in seconds. Default: `0` (never expires).
## Return value
-`void`. Throws if the upload to Vercel Blob fails.
+An object containing:
+
+### `snapshotId`
+
+A `string` identifier for the snapshot.
## See also
-- [`createSnapshot()`](/docs/vercel/create-snapshot)
-- [`getSnapshot()`](/docs/vercel/get-snapshot)
+- [`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.tsx
index a4b581cafb..0838f24555 100644
--- a/packages/docs/docs/vercel/table-of-contents.tsx
+++ b/packages/docs/docs/vercel/table-of-contents.tsx
@@ -10,21 +10,13 @@ export const TableOfContents: React.FC = () => {
<strong>createSandbox()</strong>
<div>Create a sandbox with Remotion installed</div>
</TOCItem>
- <TOCItem link="/docs/vercel/get-or-create-sandbox">
- <strong>getOrCreateSandbox()</strong>
- <div>Get a cached sandbox or create a new one</div>
- </TOCItem>
- <TOCItem link="/docs/vercel/create-snapshot">
- <strong>createSnapshot()</strong>
- <div>Take a snapshot of a sandbox</div>
- </TOCItem>
<TOCItem link="/docs/vercel/save-snapshot">
<strong>saveSnapshot()</strong>
- <div>Save a snapshot ID to Vercel Blob</div>
+ <div>Snapshot a sandbox and save to Vercel Blob</div>
</TOCItem>
- <TOCItem link="/docs/vercel/get-snapshot">
- <strong>getSnapshot()</strong>
- <div>Get a cached snapshot ID from Vercel Blob</div>
+ <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>
diff --git a/packages/docs/docs/vercel/types.mdx b/packages/docs/docs/vercel/types.mdx
index d5b19e587e..f79a504091 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 [`getOrCreateSandbox()`](/docs/vercel/get-or-create-sandbox).
+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).
## `CreateSandboxOnProgress`
diff --git a/packages/docs/docs/vercel/upload-to-blob-storage.mdx b/packages/docs/docs/vercel/upload-to-blob-storage.mdx
index 69189b5a37..2bf14e8ec8 100644
--- a/packages/docs/docs/vercel/upload-to-blob-storage.mdx
+++ b/packages/docs/docs/vercel/upload-to-blob-storage.mdx
@@ -13,8 +13,8 @@ Uploads a file from the sandbox to [Vercel Blob](https://vercel.com/docs/storage
```ts twoslash title="route.ts"
// @module: es2022
// @target: es2022
-import {uploadToBlobStorage, getOrCreateSandbox} from '@remotion/vercel';
-const sandbox = await getOrCreateSandbox({bundleDir: '.remotion'});
+import {uploadToBlobStorage, createSandbox} from '@remotion/vercel';
+const sandbox = await createSandbox({bundleDir: '.remotion'});
// ---cut---
const {url, size} = await uploadToBlobStorage({
sandbox,
diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.ts
index 9615711bbd..1b3cc9137d 100644
--- a/packages/docs/sidebars.ts
+++ b/packages/docs/sidebars.ts
@@ -469,10 +469,8 @@ const sidebars: SidebarsConfig = {
},
items: [
'vercel/create-sandbox',
- 'vercel/get-or-create-sandbox',
- 'vercel/create-snapshot',
'vercel/save-snapshot',
- 'vercel/get-snapshot',
+ 'vercel/restore-snapshot',
'vercel/render-video-on-vercel',
'vercel/render-still-on-vercel',
'vercel/upload-to-blob-storage',
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index 59e09f2012..b28336ae68 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -6547,33 +6547,6 @@ export const articles = [
noAi: false,
slug: 'vercel/create-sandbox',
},
- {
- id: 'vercel/create-snapshot',
- title: 'createSnapshot()',
- relativePath: 'docs/vercel/create-snapshot.mdx',
- compId: 'articles-docs-vercel-create-snapshot',
- crumb: '@remotion/vercel',
- noAi: false,
- slug: 'vercel/create-snapshot',
- },
- {
- id: 'vercel/get-or-create-sandbox',
- title: 'getOrCreateSandbox()',
- relativePath: 'docs/vercel/get-or-create-sandbox.mdx',
- compId: 'articles-docs-vercel-get-or-create-sandbox',
- crumb: '@remotion/vercel',
- noAi: false,
- slug: 'vercel/get-or-create-sandbox',
- },
- {
- id: 'vercel/get-snapshot',
- title: 'getSnapshot()',
- relativePath: 'docs/vercel/get-snapshot.mdx',
- compId: 'articles-docs-vercel-get-snapshot',
- crumb: '@remotion/vercel',
- noAi: false,
- slug: 'vercel/get-snapshot',
- },
{
id: 'vercel-api',
title: '@remotion/vercel',
@@ -6601,6 +6574,15 @@ 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()',
diff --git a/packages/docs/static/generated/articles-docs-vercel-restore-snapshot.png b/packages/docs/static/generated/articles-docs-vercel-restore-snapshot.png
new file mode 100644
index 0000000000..6e52cd6c5c
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-vercel-restore-snapshot.png differ
diff --git a/packages/template-vercel/create-snapshot.ts b/packages/template-vercel/create-snapshot.ts
index 908189a2af..77952f9bf8 100644
--- a/packages/template-vercel/create-snapshot.ts
+++ b/packages/template-vercel/create-snapshot.ts
@@ -1,4 +1,4 @@
-import { createSandbox, createSnapshot, saveSnapshot } from "@remotion/vercel";
+import { createSandbox, saveSnapshot } from "@remotion/vercel";
const sandbox = await createSandbox({
bundleDir: ".remotion",
@@ -9,7 +9,6 @@ const sandbox = await createSandbox({
});
console.log("[create-snapshot] Taking snapshot...");
-const { snapshotId } = await createSnapshot({ sandbox });
-await saveSnapshot({ snapshotId });
+const { snapshotId } = await saveSnapshot({ sandbox });
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.ts
index bc484c5e4b..e7db876d4a 100644
--- a/packages/template-vercel/src/app/api/render/route.ts
+++ b/packages/template-vercel/src/app/api/render/route.ts
@@ -1,6 +1,7 @@
import {
- getOrCreateSandbox,
+ createSandbox,
renderVideoOnVercel,
+ restoreSnapshot,
uploadToBlobStorage,
} from "@remotion/vercel";
import { waitUntil } from "@vercel/functions";
@@ -31,17 +32,19 @@ export async function POST(req: Request) {
await send({ type: "phase", phase: "Creating sandbox...", progress: 0 });
- await using sandbox = await getOrCreateSandbox({
- bundleDir: ".remotion",
- onProgress: ({ progress, message }) => {
- send({
- type: "phase",
- phase: message,
- progress,
- subtitle: "This is only needed during development.",
+ await using sandbox = process.env.VERCEL
+ ? await restoreSnapshot()
+ : await createSandbox({
+ bundleDir: ".remotion",
+ onProgress: ({ progress, message }) => {
+ send({
+ type: "phase",
+ phase: message,
+ progress,
+ subtitle: "This is only needed during development.",
+ });
+ },
});
- },
- });
const { file } = await renderVideoOnVercel({
sandbox,
diff --git a/packages/vercel/src/create-snapshot.ts b/packages/vercel/src/create-snapshot.ts
deleted file mode 100644
index 019110c506..0000000000
--- a/packages/vercel/src/create-snapshot.ts
+++ /dev/null
@@ -1,10 +0,0 @@
-import type {VercelSandbox} from './types';
-
-export async function createSnapshot({
- sandbox,
-}: {
- sandbox: VercelSandbox;
-}): Promise<{snapshotId: string}> {
- const snapshot = await sandbox.snapshot({expiration: 0});
- return {snapshotId: snapshot.snapshotId};
-}
diff --git a/packages/vercel/src/get-or-create-sandbox.ts b/packages/vercel/src/get-or-create-sandbox.ts
deleted file mode 100644
index cb30b8b1af..0000000000
--- a/packages/vercel/src/get-or-create-sandbox.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-import {createSandbox, SANDBOX_CREATING_TIMEOUT} from './create-sandbox';
-import {getSnapshot} from './get-snapshot';
-import {createDisposableSandbox} from './internals/disposable';
-import type {CreateSandboxOnProgress, VercelSandbox} from './types';
-
-export async function getOrCreateSandbox({
- bundleDir,
- onProgress,
-}: {
- bundleDir: string;
- onProgress?: CreateSandboxOnProgress;
-}): Promise<VercelSandbox> {
- // During local development, a new sandbox is created from scratch always (no snapshotting)
- if (!process.env.VERCEL) {
- onProgress?.({progress: 0, message: 'Creating sandbox...'});
- return createSandbox({bundleDir, onProgress});
- }
-
- // In production, the snapshot is created at build time via `create-snapshot`
- const cachedSnapshotId = await getSnapshot();
-
- if (!cachedSnapshotId) {
- throw new Error(
- 'No sandbox snapshot found. Run `bun run create-snapshot` as part of the build process.',
- );
- }
-
- return createDisposableSandbox({
- source: {type: 'snapshot', snapshotId: cachedSnapshotId},
- timeout: SANDBOX_CREATING_TIMEOUT,
- });
-}
diff --git a/packages/vercel/src/get-snapshot.ts b/packages/vercel/src/get-snapshot.ts
deleted file mode 100644
index c130369f11..0000000000
--- a/packages/vercel/src/get-snapshot.ts
+++ /dev/null
@@ -1,24 +0,0 @@
-import {head} from '@vercel/blob';
-
-type SnapshotCache = {
- snapshotId: string;
-};
-
-const getSnapshotBlobKey = () =>
- `snapshot-cache/${process.env.VERCEL_DEPLOYMENT_ID ?? 'local'}.json`;
-
-export async function getSnapshot(): Promise<string | null> {
- if (!process.env.VERCEL) {
- return null;
- }
-
- try {
- const metadata = await head(getSnapshotBlobKey());
- const response = await fetch(metadata.url);
- const cache: SnapshotCache = await response.json();
-
- return cache.snapshotId;
- } catch {
- return null;
- }
-}
diff --git a/packages/vercel/src/index.ts b/packages/vercel/src/index.ts
index e8f1129766..653d74f04b 100644
--- a/packages/vercel/src/index.ts
+++ b/packages/vercel/src/index.ts
@@ -1,9 +1,7 @@
export {createSandbox} from './create-sandbox';
-export {createSnapshot} from './create-snapshot';
-export {getOrCreateSandbox} from './get-or-create-sandbox';
-export {getSnapshot} from './get-snapshot';
export {renderStillOnVercel} from './render-still';
export {renderVideoOnVercel} from './render-video';
+export {restoreSnapshot} from './restore-snapshot';
export {saveSnapshot} from './save-snapshot';
export type {
CreateSandboxOnProgress,
diff --git a/packages/vercel/src/restore-snapshot.ts b/packages/vercel/src/restore-snapshot.ts
new file mode 100644
index 0000000000..7c5b778408
--- /dev/null
+++ b/packages/vercel/src/restore-snapshot.ts
@@ -0,0 +1,35 @@
+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.ts
index 8e932a5b23..2624b728c7 100644
--- a/packages/vercel/src/save-snapshot.ts
+++ b/packages/vercel/src/save-snapshot.ts
@@ -1,4 +1,5 @@
import {put} from '@vercel/blob';
+import type {VercelSandbox} from './types';
type SnapshotCache = {
snapshotId: string;
@@ -8,10 +9,15 @@ const getSnapshotBlobKey = () =>
`snapshot-cache/${process.env.VERCEL_DEPLOYMENT_ID ?? 'local'}.json`;
export async function saveSnapshot({
- snapshotId,
+ sandbox,
+ expiration = 0,
}: {
- snapshotId: string;
-}): Promise<void> {
+ sandbox: VercelSandbox;
+ expiration?: number;
+}): Promise<{snapshotId: string}> {
+ const snapshot = await sandbox.snapshot({expiration});
+ const {snapshotId} = snapshot;
+
const cache: SnapshotCache = {
snapshotId,
};
@@ -21,4 +27,6 @@ export async function saveSnapshot({
contentType: 'application/json',
addRandomSuffix: false,
});
+
+ return {snapshotId};
}