🎥 Make videos programmatically with React
Preserve asset cache busting on Studio restarts
Igor Samokhovetsd35b78be49c9
diff --git a/packages/core/src/input-props-serialization.ts b/packages/core/src/input-props-serialization.tsindex a726650b3a..6f7d5de239 100644--- a/packages/core/src/input-props-serialization.ts+++ b/packages/core/src/input-props-serialization.ts@@ -1,6 +1,7 @@// Must keep this file in sync with the one in packages/lambda/src/shared/serialize-props.ts!import {getRemotionEnvironment} from './get-remotion-environment.js';+import {getStaticFileUrl} from './static-file.js';export type SerializedJSONWithCustomFields = {serializedString: string;@@ -54,7 +55,7 @@ export const serializeJSONWithSpecialTypes = ({item.startsWith(staticBase)) {customFileUsed = true;- return `${FILE_TOKEN}${item.replace(staticBase + '/', '')}`;+ return `${FILE_TOKEN}${item.replace(staticBase + '/', '').split('?')[0]}`;}return value;@@ -71,7 +72,7 @@ export const serializeJSONWithSpecialTypes = ({};export const resolveFileTokenToUrl = (value: string) => {- const encodedName = value.replace(FILE_TOKEN, '');+ const encodedName = value.replace(FILE_TOKEN, '').split('?')[0];let name = encodedName;try {name = encodedName.split('/').map(decodeURIComponent).join('/');@@ -86,7 +87,7 @@ export const resolveFileTokenToUrl = (value: string) => {return matchingStaticFile.src;}- return `${window.remotion_staticBase}/${encodedName}`;+ return getStaticFileUrl(encodedName);};export const deserializeJSONWithSpecialTypes = <T = Record<string, unknown>>(diff --git a/packages/core/src/static-file.ts b/packages/core/src/static-file.tsindex 35e0117581..66c7e694ea 100644--- a/packages/core/src/static-file.ts+++ b/packages/core/src/static-file.ts@@ -60,7 +60,7 @@ const trimLeadingSlash = (path: string): string => {return path;};-const inner = (path: string): string => {+export const getStaticFileUrl = (path: string): string => {if (typeof window !== 'undefined' && window.remotion_staticBase) {if (path.startsWith(window.remotion_staticBase)) {throw new Error(@@ -68,7 +68,10 @@ const inner = (path: string): string => {);}- return `${window.remotion_staticBase}/${trimLeadingSlash(path)}`;+ const src = `${window.remotion_staticBase}/${trimLeadingSlash(path)}`;+ return window.remotion_outputsBase+ ? `${src}?v=${encodeURIComponent(window.remotion_outputsBase)}`+ : src;}return `/${trimLeadingSlash(path)}`;@@ -140,8 +143,8 @@ export const staticFile = (path: string) => {if (typeof window !== 'undefined') {// This lookup is primarily used by Browser Studio, where virtual assets- // have blob: URLs. Regular Studio sources are already encoded using the- // same per-path-segment encoding as the fallback below.+ // have blob: URLs. Regular Studio sources include the per-start cache+ // version and use the same per-path-segment encoding as the fallback below.const matchingStaticFile = window.remotion_staticFiles?.find((file) => file.name === trimLeadingSlash(path),);@@ -151,7 +154,7 @@ export const staticFile = (path: string) => {}const preprocessed = encodeBySplitting(path);- const preparsed = inner(preprocessed);+ const preparsed = getStaticFileUrl(preprocessed);if (!preparsed.startsWith('/')) {return `/${preparsed}`;diff --git a/packages/core/src/watch-static-file.ts b/packages/core/src/watch-static-file.tsindex 6002d7bd3c..48f401a434 100644--- a/packages/core/src/watch-static-file.ts+++ b/packages/core/src/watch-static-file.ts@@ -33,9 +33,13 @@ export const watchStaticFile = (return {cancel: () => undefined};}- const withoutStaticBase = fileName.startsWith(window.remotion_staticBase)- ? fileName.replace(window.remotion_staticBase, '')- : fileName;+ const withoutStaticBase =+ window.remotion_staticBase &&+ fileName.startsWith(window.remotion_staticBase + '/')+ ? decodeURIComponent(+ fileName.slice(window.remotion_staticBase.length).split('?')[0],+ )+ : fileName;const withoutLeadingSlash = withoutStaticBase.startsWith('/')? withoutStaticBase.slice(1): withoutStaticBase;diff --git a/packages/studio-server/src/preview-server/public-folder.ts b/packages/studio-server/src/preview-server/public-folder.tsindex dac8c3a62b..ec7cf4b879 100644--- a/packages/studio-server/src/preview-server/public-folder.ts+++ b/packages/studio-server/src/preview-server/public-folder.ts@@ -5,17 +5,21 @@ import type {StaticFile} from 'remotion';import {envSupportsFsRecursive} from './env-supports-fs-recursive';let files: StaticFile[] = [];+let staticFileCacheBust: string | null = null;export const initPublicFolderWatch = ({+ cacheBust,publicDir,onUpdate,staticHash,}: {publicDir: string;remotionRoot: string;+ cacheBust: string | null;onUpdate: () => void;staticHash: string;}) => {+ staticFileCacheBust = cacheBust;fetchFolder({publicDir, staticHash});watchPublicFolder({publicDir, onUpdate, staticHash});};@@ -35,6 +39,9 @@ export const fetchFolder = ({}).map((f) => {return {...f,+ src: staticFileCacheBust+ ? `${f.src}?v=${encodeURIComponent(staticFileCacheBust)}`+ : f.src,name: f.name.split(path.sep).join('/'),};});diff --git a/packages/studio-server/src/start-studio.ts b/packages/studio-server/src/start-studio.tsindex f1e77e9576..ea26f1ccbb 100644--- a/packages/studio-server/src/start-studio.ts+++ b/packages/studio-server/src/start-studio.ts@@ -135,7 +135,12 @@ export const startStudio = async ({.digest('hex').slice(0, 12)}`;+ // Previously, restarting Studio changed every asset URL, allowing users to+ // recover from replaced files or stale browser/media caches. Reuse the random+ // output namespace as a query version to preserve that behavior for newly+ // loaded Studios, while stable paths keep old asset URLs valid across restarts.initPublicFolderWatch({+ cacheBust: outputHash,publicDir,remotionRoot,onUpdate: () => {diff --git a/packages/studio/src/api/delete-static-file.ts b/packages/studio/src/api/delete-static-file.tsindex 7c15934511..2dc5d40526 100644--- a/packages/studio/src/api/delete-static-file.ts+++ b/packages/studio/src/api/delete-static-file.ts@@ -10,9 +10,12 @@ export const deleteStaticFile = async (throw new Error('deleteStaticFile() is only available in the Studio');}- if (relativePath.startsWith(window.remotion_staticBase)) {- relativePath = relativePath.substring(- window.remotion_staticBase.length + 1,+ if (+ window.remotion_staticBase &&+ relativePath.startsWith(window.remotion_staticBase + '/')+ ) {+ relativePath = decodeURIComponent(+ relativePath.slice(window.remotion_staticBase.length + 1).split('?')[0],);}diff --git a/packages/studio/src/api/watch-static-file.ts b/packages/studio/src/api/watch-static-file.tsindex f90258b345..4948852bf2 100644--- a/packages/studio/src/api/watch-static-file.ts+++ b/packages/studio/src/api/watch-static-file.ts@@ -33,9 +33,13 @@ export const watchStaticFile = (return {cancel: () => undefined};}- const withoutStaticBase = fileName.startsWith(window.remotion_staticBase)- ? fileName.replace(window.remotion_staticBase, '')- : fileName;+ const withoutStaticBase =+ window.remotion_staticBase &&+ fileName.startsWith(window.remotion_staticBase + '/')+ ? decodeURIComponent(+ fileName.slice(window.remotion_staticBase.length).split('?')[0],+ )+ : fileName;const withoutLeadingSlash = withoutStaticBase.startsWith('/')? withoutStaticBase.slice(1): withoutStaticBase;diff --git a/packages/studio/src/components/Timeline/timeline-asset-link.ts b/packages/studio/src/components/Timeline/timeline-asset-link.tsindex 15b23db848..43bfc6aa47 100644--- a/packages/studio/src/components/Timeline/timeline-asset-link.ts+++ b/packages/studio/src/components/Timeline/timeline-asset-link.ts@@ -51,7 +51,9 @@ export const getTimelineAssetSrcFromSchema = (export const getTimelineAssetLinkInfo = (src: string): LinkInfo => {if (src.startsWith(NoReactInternals.FILE_TOKEN)) {- const encodedAssetPath = src.slice(NoReactInternals.FILE_TOKEN.length);+ const encodedAssetPath = src+ .slice(NoReactInternals.FILE_TOKEN.length)+ .split('?')[0];let assetPath = encodedAssetPath;try {assetPath = encodedAssetPath.split('/').map(decodeURIComponent).join('/');@@ -70,7 +72,7 @@ export const getTimelineAssetLinkInfo = (src: string): LinkInfo => {typeof window === 'undefined' ? null : window.remotion_staticBase;if (staticBase && src.startsWith(staticBase + '/')) {- const assetPath = src.slice(staticBase.length + 1);+ const assetPath = src.slice(staticBase.length + 1).split('?')[0];return {kind: 'local',assetPath: decodeURIComponent(assetPath),