Skip to content

🎥 Make videos programmatically with React

Deploy the Vibe Code template as a static export at remotion.dev/experimental_sdk

JonnyBurgera29729a379b6
diff --git a/packages/docs/build-docs.ts b/packages/docs/build-docs.ts
index 0b70bce0d0..8c19fd455d 100644
--- a/packages/docs/build-docs.ts
+++ b/packages/docs/build-docs.ts
@@ -150,5 +150,6 @@ await run('build standalone Browser Studio', 'bun', [
'build-browser-studio-standalone.ts',
]);
await run('copy convert assets', 'bun', ['copy-convert.ts']);
+await run('copy Vibe Code editor', 'bun', ['copy-experimental-sdk.ts']);
await run('generate asset manifest', 'bun', ['generate-asset-manifest.ts']);
await run('count generated pages', 'bun', ['count-pages.ts']);
diff --git a/packages/docs/copy-experimental-sdk.ts b/packages/docs/copy-experimental-sdk.ts
new file mode 100644
index 0000000000..2dc2bd177c
--- /dev/null
+++ b/packages/docs/copy-experimental-sdk.ts
@@ -0,0 +1,25 @@
+import fs from 'fs';
+import path from 'path';
+import {$} from 'bun';
+
+// remotion.dev/experimental_sdk hosts the Vibe Code template as a static
+// export, with the starter project embedded at build time. The template's
+// workspace dependencies are dependencies of the docs, so they are built.
+const basePath = '/experimental_sdk';
+const templateDir = path.join(__dirname, '../template-vibe-code');
+const outDir = path.join(templateDir, 'out');
+const destination = path.join(__dirname, 'build', basePath.slice(1));
+
+await $`bun run build`.cwd(templateDir).env({
+ ...process.env,
+ STATIC_EXPORT_BASE_PATH: basePath,
+});
+
+if (!fs.existsSync(path.join(outDir, 'index.html'))) {
+ throw new Error(`Expected the static export at ${outDir}`);
+}
+
+fs.rmSync(destination, {recursive: true, force: true});
+fs.mkdirSync(path.dirname(destination), {recursive: true});
+fs.cpSync(outDir, destination, {recursive: true});
+console.log(`[docs build] Copied the Vibe Code editor to ${basePath}`);
diff --git a/packages/docs/vercel.ts b/packages/docs/vercel.ts
index 6bda3b89ca..42f72f3245 100644
--- a/packages/docs/vercel.ts
+++ b/packages/docs/vercel.ts
@@ -44,6 +44,17 @@ export const config: VercelConfig = {
...['/new', '/new/', '/new.html', '/new/index.html'].map((source) =>
routes.header(source, [{key: 'Cache-Control', value: 'no-store'}]),
),
+ // The Vibe Code editor compiles with the browser bundler, which needs
+ // cross-origin isolation for the editor, its preview iframe and the
+ // compiler worker.
+ routes.header('/experimental_sdk(.*)', browserStudioIsolationHeaders),
+ ...[
+ '/experimental_sdk',
+ '/experimental_sdk/',
+ '/experimental_sdk/index.html',
+ ].map((source) =>
+ routes.header(source, [{key: 'Cache-Control', value: 'no-store'}]),
+ ),
routes.header('/convert/assets/(.*)', [
{key: 'Cross-Origin-Embedder-Policy', value: 'require-corp'},
{key: 'Cross-Origin-Opener-Policy', value: 'same-origin'},
diff --git a/packages/template-vibe-code/README.md b/packages/template-vibe-code/README.md
index f04d68a188..5af1f16f60 100644
--- a/packages/template-vibe-code/README.md
+++ b/packages/template-vibe-code/README.md
@@ -108,6 +108,17 @@ npx remotion upgrade
The compiler's Web Worker and WebAssembly binary are copied to `public/compiler` by `scripts/build-preview.mjs` and loaded from there (see `workerUrl` in `src/editor/hooks/use-compiler.ts`), so they do not go through the Next.js bundler and both Turbopack and webpack work.
+### Static export
+
+Set `STATIC_EXPORT_BASE_PATH` to export the editor as static files into `out/`, for example to host it on a static file server or under a path of an existing site:
+
+```bash
+STATIC_EXPORT_BASE_PATH=/ npm run build # served at the root
+STATIC_EXPORT_BASE_PATH=/editor npm run build # served under /editor
+```
+
+The starter project is embedded at build time and edits stay in memory. Only the `*.static.tsx` route files in `src/app` are part of the export, so the page and the `/api/project` route that read the project from disk are left out. Serve the files with the `Cross-Origin-Opener-Policy` and `Cross-Origin-Embedder-Policy` headers described below; `next.config.mjs` cannot set headers for a static export. This is how [remotion.dev/experimental_sdk](https://www.remotion.dev/experimental_sdk) is deployed.
+
## Docs
Get started with Remotion by reading the [fundamentals page](https://www.remotion.dev/docs/the-fundamentals).
diff --git a/packages/template-vibe-code/next.config.mjs b/packages/template-vibe-code/next.config.mjs
index 042f658886..d6d1f0d9a9 100644
--- a/packages/template-vibe-code/next.config.mjs
+++ b/packages/template-vibe-code/next.config.mjs
@@ -4,30 +4,51 @@
// the editor, the preview iframe and the compiler assets.
// See: https://www.remotion.dev/docs/browser-bundler
+// Setting STATIC_EXPORT_BASE_PATH (for example "/" or "/editor") exports the
+// editor as static files, with the starter project embedded at build time and
+// saving disabled. Only the *.static.tsx routes take part in the export, so the
+// page and the /api/project route that read the project from disk stay out.
+// Serve the exported files with the COOP/COEP headers yourself.
+const staticExportBasePath = process.env.STATIC_EXPORT_BASE_PATH;
+const basePath =
+ staticExportBasePath === undefined
+ ? ""
+ : staticExportBasePath.replace(/\/+$/, "");
+
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
agentRules: false,
- // The starter project in src/remotion is read from disk at request time.
- outputFileTracingIncludes: {
- "/": ["./src/remotion/**/*"],
- "/api/project": ["./src/remotion/**/*"],
- },
- async headers() {
- return [
- "/",
- "/preview.html",
- "/preview.js",
- "/compiler/:path*",
- "/_next/:path*",
- ].map((source) => ({
- source,
- headers: [
- { key: "Cross-Origin-Opener-Policy", value: "same-origin" },
- { key: "Cross-Origin-Embedder-Policy", value: "require-corp" },
- ],
- }));
- },
+ basePath,
+ // Read by src/lib/base-path.ts to address public files and the API.
+ env: { NEXT_PUBLIC_BASE_PATH: basePath },
+ ...(staticExportBasePath === undefined
+ ? {
+ // The starter project in src/remotion is read from disk at request time.
+ outputFileTracingIncludes: {
+ "/": ["./src/remotion/**/*"],
+ "/api/project": ["./src/remotion/**/*"],
+ },
+ async headers() {
+ return [
+ "/",
+ "/preview.html",
+ "/preview.js",
+ "/compiler/:path*",
+ "/_next/:path*",
+ ].map((source) => ({
+ source,
+ headers: [
+ { key: "Cross-Origin-Opener-Policy", value: "same-origin" },
+ { key: "Cross-Origin-Embedder-Policy", value: "require-corp" },
+ ],
+ }));
+ },
+ }
+ : {
+ output: "export",
+ pageExtensions: ["static.tsx"],
+ }),
};
export default nextConfig;
diff --git a/packages/template-vibe-code/public/preview.html b/packages/template-vibe-code/public/preview.html
index 8b487274f6..efd9d52242 100644
--- a/packages/template-vibe-code/public/preview.html
+++ b/packages/template-vibe-code/public/preview.html
@@ -33,7 +33,7 @@
font-size: 12px;
}
</style>
- <script defer src="/preview.js"></script>
+ <script defer src="preview.js"></script>
</head>
<body>
<main id="preview-root">
diff --git a/packages/template-vibe-code/src/app/layout.static.tsx b/packages/template-vibe-code/src/app/layout.static.tsx
new file mode 100644
index 0000000000..8669861de6
--- /dev/null
+++ b/packages/template-vibe-code/src/app/layout.static.tsx
@@ -0,0 +1,2 @@
+// The static export only picks up *.static.tsx route files, see next.config.mjs.
+export { default, metadata, viewport } from "./layout";
diff --git a/packages/template-vibe-code/src/app/page.static.tsx b/packages/template-vibe-code/src/app/page.static.tsx
new file mode 100644
index 0000000000..1179ae9204
--- /dev/null
+++ b/packages/template-vibe-code/src/app/page.static.tsx
@@ -0,0 +1,17 @@
+import { Editor } from "@/editor/Editor";
+import { PROJECT_ENTRY_POINT } from "@/lib/project-paths";
+import { readProjectFiles } from "@/server/project-files";
+
+// The page of the static export (see next.config.mjs): the project is read
+// once at build time and edits stay in memory.
+export default async function Page() {
+ const files = await readProjectFiles();
+
+ return (
+ <Editor
+ initialFiles={files}
+ entryPoint={PROJECT_ENTRY_POINT}
+ canSave={false}
+ />
+ );
+}
diff --git a/packages/template-vibe-code/src/editor/hooks/use-compiler.ts b/packages/template-vibe-code/src/editor/hooks/use-compiler.ts
index 6d61e689a3..3afaa28874 100644
--- a/packages/template-vibe-code/src/editor/hooks/use-compiler.ts
+++ b/packages/template-vibe-code/src/editor/hooks/use-compiler.ts
@@ -5,6 +5,7 @@ import {
createBrowserBundler,
} from "@remotion/browser-bundler";
import { useCallback, useEffect, useRef } from "react";
+import { BASE_PATH } from "@/lib/base-path";
import type {
PreviewCompositionSelection,
PreviewHost,
@@ -71,7 +72,7 @@ export const useCompiler = ({
enableFastRefresh: true,
// Served from public/compiler, see scripts/build-preview.mjs.
workerUrl: new URL(
- "/compiler/browser-bundler-worker.js",
+ `${BASE_PATH}/compiler/browser-bundler-worker.js`,
window.location.origin,
),
onProgress: ({ loadedBytes, totalBytes }) => {
diff --git a/packages/template-vibe-code/src/editor/hooks/use-editor-actions.ts b/packages/template-vibe-code/src/editor/hooks/use-editor-actions.ts
index 259426bc0c..a05dd0ed14 100644
--- a/packages/template-vibe-code/src/editor/hooks/use-editor-actions.ts
+++ b/packages/template-vibe-code/src/editor/hooks/use-editor-actions.ts
@@ -78,6 +78,7 @@ import {
type Layer,
} from "../model/layers";
import { filesAreEqual, getFileName, toCodemodProject } from "../model/project";
+import { BASE_PATH } from "@/lib/base-path";
import { resolveProjectPathInput } from "@/lib/project-paths";
import type { PlaybackStore } from "./use-playback";
import { getErrorMessage } from "./use-preview-host";
@@ -571,7 +572,7 @@ export const ${componentName}: React.FC = () => {
(file) => !(file in files),
);
try {
- const response = await fetch("/api/project", {
+ const response = await fetch(`${BASE_PATH}/api/project`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ files, deleted }),
diff --git a/packages/template-vibe-code/src/editor/hooks/use-preview-host.ts b/packages/template-vibe-code/src/editor/hooks/use-preview-host.ts
index 874abcbb9c..8cf4e56c0b 100644
--- a/packages/template-vibe-code/src/editor/hooks/use-preview-host.ts
+++ b/packages/template-vibe-code/src/editor/hooks/use-preview-host.ts
@@ -5,13 +5,14 @@ import type {
CanvasSequencePropStatusResolver,
} from "@remotion/canvas";
import { useEffect, useRef, useState } from "react";
+import { BASE_PATH } from "@/lib/base-path";
import type { PreviewHost, PreviewKeyEvent } from "@/preview/bridge";
export const getErrorMessage = (error: unknown) =>
error instanceof Error ? error.message : String(error);
/**
- * Loads /preview.html into the iframe and connects to the preview host that
+ * Loads preview.html into the iframe and connects to the preview host that
* the iframe exposes on its window.
*/
export const usePreviewHost = ({
@@ -106,7 +107,7 @@ export const usePreviewHost = ({
iframe.addEventListener("load", onLoad);
iframe.addEventListener("error", onIframeError);
- iframe.src = "/preview.html";
+ iframe.src = `${BASE_PATH}/preview.html`;
return () => {
disposed = true;
diff --git a/packages/template-vibe-code/src/lib/base-path.ts b/packages/template-vibe-code/src/lib/base-path.ts
new file mode 100644
index 0000000000..c6f396ed1f
--- /dev/null
+++ b/packages/template-vibe-code/src/lib/base-path.ts
@@ -0,0 +1,4 @@
+// The path the app is served under, without a trailing slash and empty at the
+// root. Next.js prefixes its own routes and assets, but public files and the
+// API must be addressed with it explicitly. Set in next.config.mjs.
+export const BASE_PATH = process.env.NEXT_PUBLIC_BASE_PATH ?? "";