Skip to content

🎥 Make videos programmatically with React

Fix Studio asset URLs across server restarts

Igor Samokhovetsd6de7ddee5d5
diff --git a/packages/browser-studio/src/BrowserStudio.tsx b/packages/browser-studio/src/BrowserStudio.tsx
index cfec5f1e4e..6b116791f0 100644
--- a/packages/browser-studio/src/BrowserStudio.tsx
+++ b/packages/browser-studio/src/BrowserStudio.tsx
@@ -649,6 +649,7 @@ export const BrowserStudio: React.FC<BrowserStudioProps> = ({
logLevel: 'info',
mode: 'dev',
numberOfAudioTags: 0,
+ outputHash: null,
packageManager: 'unknown',
projectName: 'template-blank',
publicFiles,
diff --git a/packages/bundler/src/bundle.ts b/packages/bundler/src/bundle.ts
index 17dea565f3..b057e8983a 100644
--- a/packages/bundler/src/bundle.ts
+++ b/packages/bundler/src/bundle.ts
@@ -421,6 +421,7 @@ export const internalBundle = async (
const html = indexHtml({
importMap: null,
staticHash,
+ outputHash: null,
publicPath,
editorName: null,
inputProps: null,
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 85de6a2000..85a81515d4 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -71,6 +71,7 @@ declare global {
) => Promise<VideoConfigWithSerializedProps>;
remotion_setBundleMode: (bundleMode: BundleState) => void;
remotion_staticBase: string;
+ remotion_outputsBase: string | null;
remotion_staticFiles: StaticFile[];
remotion_publicPath: string;
remotion_publicFolderExists: string | null;
diff --git a/packages/studio-server/src/routes.ts b/packages/studio-server/src/routes.ts
index 8efa666b85..2bf049c85d 100644
--- a/packages/studio-server/src/routes.ts
+++ b/packages/studio-server/src/routes.ts
@@ -104,6 +104,7 @@ const handleRemotionConfig = (
const handleFallback = async ({
remotionRoot,
hash,
+ outputHash,
response,
request,
getCurrentInputProps,
@@ -123,6 +124,7 @@ const handleFallback = async ({
}: {
remotionRoot: string;
hash: string;
+ outputHash: string;
response: ServerResponse;
request: IncomingMessage;
publicDir: string;
@@ -190,6 +192,7 @@ const handleFallback = async ({
BundlerInternals.indexHtml({
importMap: null,
staticHash: hash,
+ outputHash,
publicPath: '/',
editorName: displayName,
envVariables: getEnvVariables(),
@@ -681,6 +684,7 @@ export const handleRoutes = ({
return handleFallback({
remotionRoot,
hash: staticHash,
+ outputHash,
response,
request,
getCurrentInputProps,
diff --git a/packages/studio-server/src/start-studio.ts b/packages/studio-server/src/start-studio.ts
index 78f27444c5..f1e77e9576 100644
--- a/packages/studio-server/src/start-studio.ts
+++ b/packages/studio-server/src/start-studio.ts
@@ -124,13 +124,16 @@ export const startStudio = async ({
relativePublicDir,
remotionRoot,
});
- const hash = crypto.randomBytes(6).toString('hex');
-
const outputHashPrefix = '/outputs-';
- const outputHash = `${outputHashPrefix}${hash}`;
+ const outputHash = `${outputHashPrefix}${crypto.randomBytes(6).toString('hex')}`;
+ // Asset URLs must survive restarts, but remain scoped to this project and folder.
const staticHashPrefix = '/static-';
- const staticHash = `${staticHashPrefix}${hash}`;
+ const staticHash = `${staticHashPrefix}${crypto
+ .createHash('sha256')
+ .update(JSON.stringify([remotionRoot, publicDir]))
+ .digest('hex')
+ .slice(0, 12)}`;
initPublicFolderWatch({
publicDir,
diff --git a/packages/studio-shared/src/studio-html.ts b/packages/studio-shared/src/studio-html.ts
index 1e11634b49..539277ef9c 100644
--- a/packages/studio-shared/src/studio-html.ts
+++ b/packages/studio-shared/src/studio-html.ts
@@ -7,6 +7,7 @@ import type {StudioRuntimeConfig} from './studio-runtime-config';
export type StudioHtmlOptions = {
staticHash: string;
+ outputHash: string | null;
publicPath: string;
editorName: string | null;
inputProps: object | null;
@@ -44,6 +45,7 @@ export const studioHtml = ({
inputProps,
envVariables,
staticHash,
+ outputHash,
remotionRoot,
studioServerCommand,
renderQueue,
@@ -109,6 +111,7 @@ export const studioHtml = ({
<script>window.remotion_previewSampleRate = ${sampleRate};</script>
${mode === 'dev' ? `<script>window.remotion_logLevel = "${logLevel}";</script>` : ''}
<script>window.remotion_staticBase = ${staticBaseValue};</script>
+ <script>window.remotion_outputsBase = ${JSON.stringify(outputHash)};</script>
${
editorName
? `<script>window.remotion_editorName = "${editorName}";</script>`
diff --git a/packages/studio-shared/src/test/studio-html.test.ts b/packages/studio-shared/src/test/studio-html.test.ts
index a71f25666a..46695e79c4 100644
--- a/packages/studio-shared/src/test/studio-html.test.ts
+++ b/packages/studio-shared/src/test/studio-html.test.ts
@@ -18,6 +18,7 @@ const makeHtml = ({
importMap,
publicPath,
staticHash,
+ outputHash: null,
editorName: null,
inputProps: null,
remotionRoot: '/project',
diff --git a/packages/studio/src/helpers/get-asset-metadata.ts b/packages/studio/src/helpers/get-asset-metadata.ts
index 5ef1f274eb..66a9b72616 100644
--- a/packages/studio/src/helpers/get-asset-metadata.ts
+++ b/packages/studio/src/helpers/get-asset-metadata.ts
@@ -5,10 +5,7 @@ import {getPreviewFileType} from './get-preview-file-type';
import type {Dimensions} from './is-current-selected-still';
import {getMediaMetadata, type MediaMetadata} from './use-media-metadata';
-export const remotion_outputsBase = window.remotion_staticBase.replace(
- 'static',
- 'outputs',
-);
+export const remotion_outputsBase = window.remotion_outputsBase ?? '';
const getSrcFromCanvasContent = (
canvasContent: CanvasContent & {type: 'asset' | 'output'},