Skip to content

🎥 Make videos programmatically with React

ignore fast refresh when saving back to Root.tsx

Jonny Burgercde0eeb7bf7c
diff --git a/packages/core/src/Composition.tsx b/packages/core/src/Composition.tsx
index 5550a981ff..fd9e9bb79f 100644
--- a/packages/core/src/Composition.tsx
+++ b/packages/core/src/Composition.tsx
@@ -122,8 +122,8 @@ export type CompositionProps<
Schema extends AnyZodObject,
Props extends Record<string, unknown>,
> = {
- id: string;
- schema?: Schema;
+ readonly id: string;
+ readonly schema?: Schema;
} & CompositionCalculateMetadataOrExplicit<Schema, Props> &
CompProps<Props> &
PropsIfHasProps<Schema, Props>;
diff --git a/packages/core/src/ResolveCompositionConfig.tsx b/packages/core/src/ResolveCompositionConfig.tsx
index 9d854af694..6baefd7556 100644
--- a/packages/core/src/ResolveCompositionConfig.tsx
+++ b/packages/core/src/ResolveCompositionConfig.tsx
@@ -7,7 +7,6 @@ import {
useEffect,
useImperativeHandle,
useMemo,
- useRef,
useState,
} from 'react';
import type {AnyZodObject} from 'zod';
@@ -17,6 +16,7 @@ import {CompositionManager} from './CompositionManagerContext.js';
import {EditorPropsContext} from './EditorProps.js';
import {getInputProps} from './config/input-props.js';
import {getRemotionEnvironment} from './get-remotion-environment.js';
+import {NonceContext} from './nonce.js';
import type {InferProps} from './props-if-has-props.js';
import {resolveVideoConfig} from './resolve-video-config.js';
import {validateDimension} from './validation/validate-dimensions.js';
@@ -67,6 +67,7 @@ export const ResolveCompositionConfig: React.FC<
useState<string | null>(null);
const {compositions, canvasContent, currentCompositionMetadata} =
useContext(CompositionManager);
+ const {fastRefreshes} = useContext(NonceContext);
const selectedComposition = useMemo(() => {
return compositions.find(
(c) =>
@@ -271,47 +272,33 @@ export const ResolveCompositionConfig: React.FC<
const isTheSame = selectedComposition?.id === renderModalComposition?.id;
- const lastDefaultProps = useRef({});
const currentDefaultProps = useMemo(() => {
- const newDefaultProps = {
+ return {
...(selectedComposition?.defaultProps ?? {}),
...(selectedEditorProps ?? {}),
};
-
- if (
- JSON.stringify(lastDefaultProps.current) ===
- JSON.stringify(newDefaultProps)
- ) {
- return lastDefaultProps.current;
- }
-
- lastDefaultProps.current = newDefaultProps;
- return newDefaultProps;
}, [selectedComposition?.defaultProps, selectedEditorProps]);
- const lastOriginalProps = useRef({});
const originalProps = useMemo(() => {
- const newOriginalProps = {
+ return {
...currentDefaultProps,
...(inputProps ?? {}),
};
-
- if (
- JSON.stringify(lastOriginalProps.current) ===
- JSON.stringify(newOriginalProps)
- ) {
- return lastOriginalProps.current;
- }
-
- lastOriginalProps.current = newOriginalProps;
- return newOriginalProps;
}, [currentDefaultProps, inputProps]);
const canResolve =
selectedComposition && needsResolution(selectedComposition);
+ const shouldIgnoreUpdate =
+ fastRefreshes === window.remotion_ignoreFastRefreshUpdate;
+
useEffect(() => {
- console.log('triggered');
+ if (shouldIgnoreUpdate) {
+ // We already have the current state, we just saved it back
+ // to the file
+ return;
+ }
+
if (canResolve) {
const controller = doResolution({
calculateMetadata: selectedComposition.calculateMetadata,
@@ -340,6 +327,7 @@ export const ResolveCompositionConfig: React.FC<
selectedComposition?.height,
selectedComposition?.id,
selectedComposition?.width,
+ shouldIgnoreUpdate,
]);
useEffect(() => {
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 5d856355bb..a083bc3af1 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -40,6 +40,7 @@ declare global {
remotion_publicPath: string;
remotion_publicFolderExists: string | null;
remotion_editorName: string | null;
+ remotion_ignoreFastRefreshUpdate: number | null;
remotion_numberOfAudioTags: number;
remotion_projectName: string;
remotion_cwd: string;
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index 3d4ffb13a5..7144039823 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -1,12 +1,12 @@
import {alias} from 'lib/alias';
-import React, {useCallback} from 'react';
+import React, {useCallback, useMemo} from 'react';
import {
CalculateMetadataFunction,
Composition,
Folder,
+ Still,
getInputProps,
staticFile,
- Still,
} from 'remotion';
import {z} from 'zod';
import {TwentyTwoKHzAudio} from './22KhzAudio';
@@ -32,8 +32,8 @@ import {Layers} from './Layers';
import {ManyAudio} from './ManyAudio';
import {MissingImg} from './MissingImg';
import {
- calculateMetadataFn,
OffthreadRemoteVideo,
+ calculateMetadataFn,
} from './OffthreadRemoteVideo/OffthreadRemoteVideo';
import {OrbScene} from './Orb';
import {PremountedExample} from './Premount';
@@ -45,8 +45,8 @@ import RiveVehicle from './Rive/RiveExample';
import {ScalePath} from './ScalePath';
import {
ArrayTest,
- schemaArrayTestSchema,
SchemaTest,
+ schemaArrayTestSchema,
schemaTestSchema,
} from './SchemaTest';
import {Scripts} from './Scripts';
@@ -69,7 +69,6 @@ import {
saveStudioSchema,
} from './StudioApis/SaveDefaultProps';
import {WriteStaticFile} from './StudioApis/WriteStaticFile';
-import './style.css';
import {Tailwind} from './Tailwind';
import {TenFrameTester} from './TenFrameTester';
import {TextStroke} from './TextStroke';
@@ -86,6 +85,7 @@ import {VideoSpeed} from './VideoSpeed';
import {VideoTesting} from './VideoTesting';
import {WarpDemoOuter} from './WarpText';
import {WarpDemo2} from './WarpText/demo2';
+import './style.css';
import {WatchStaticDemo} from './watch-static';
if (alias !== 'alias') {
@@ -100,73 +100,74 @@ export const Index: React.FC = () => {
const calculateMetadata: CalculateMetadataFunction<
z.infer<typeof dynamicDurationSchema>
- > = useCallback(async ({props}) => {
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
- const shouldLog = (..._data: unknown[]) => undefined;
- // To test logging
- // const shouldLog = console.log;
+ > = useMemo(() => {
+ return async ({props}) => {
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
+ const shouldLog = (..._data: unknown[]) => undefined;
+ // To test logging
+ // const shouldLog = console.log;
+ const foo = function* () {
+ yield 'a';
+ yield 'b';
+ yield 'c';
+ };
+ shouldLog('');
+ shouldLog('');
+ shouldLog('');
+ shouldLog('');
- const foo = function* () {
- yield 'a';
- yield 'b';
- yield 'c';
- };
- shouldLog('');
- shouldLog('');
- shouldLog('');
- shouldLog('');
+ shouldLog('objects', {a: 'string'});
+ shouldLog('boolean:', false);
+ shouldLog('number:', 1);
+ shouldLog('symbol', Symbol('hi'));
+ shouldLog('Date:', new Date());
+ shouldLog('bigint:', BigInt(123));
+ shouldLog('function:', () => 'hi');
+ shouldLog('array:', [1, 2, 3]);
+ shouldLog('regex:', /abc/);
+ shouldLog('');
+ shouldLog('');
+ shouldLog('');
+ shouldLog('');
+ shouldLog('Hello World ArrayBuffer', new ArrayBuffer(1));
+ shouldLog('Hello World DataView', new DataView(new ArrayBuffer(1)));
+ shouldLog('Hello World Error', new Error('hithere'));
+ shouldLog('Hello World Generator', foo());
+ shouldLog('Hello World Iterator', [1, 2, 3].values());
+ const map = new Map();
+ map.set('a', 1);
+ shouldLog('Hello World Map', map);
+ shouldLog('Hello World Node', document.createElement('div'));
+ shouldLog('Hello World null', null);
+ shouldLog(
+ 'Hello World Promise',
+ new Promise<void>((resolve) => {
+ resolve();
+ }),
+ );
+ shouldLog('Hello World Proxy', new Proxy(document, {}));
+ shouldLog('Hello World RegExp', /abc/);
+ shouldLog('Hello World Set', {a: [1, 2, 3]});
+ shouldLog('Hello World TypedArray', new Uint8Array([1, 2, 3]));
+ const wm3 = new WeakMap();
+ const o1 = {};
+ wm3.set(o1, 'azerty');
+ const ws = new WeakSet();
+ const foo2 = {};
- shouldLog('objects', {a: 'string'});
- shouldLog('boolean:', false);
- shouldLog('number:', 1);
- shouldLog('symbol', Symbol('hi'));
- shouldLog('Date:', new Date());
- shouldLog('bigint:', BigInt(123));
- shouldLog('function:', () => 'hi');
- shouldLog('array:', [1, 2, 3]);
- shouldLog('regex:', /abc/);
- shouldLog('');
- shouldLog('');
- shouldLog('');
- shouldLog('');
- shouldLog('Hello World ArrayBuffer', new ArrayBuffer(1));
- shouldLog('Hello World DataView', new DataView(new ArrayBuffer(1)));
- shouldLog('Hello World Error', new Error('hithere'));
- shouldLog('Hello World Generator', foo());
- shouldLog('Hello World Iterator', [1, 2, 3].values());
- const map = new Map();
- map.set('a', 1);
- shouldLog('Hello World Map', map);
- shouldLog('Hello World Node', document.createElement('div'));
- shouldLog('Hello World null', null);
- shouldLog(
- 'Hello World Promise',
- new Promise<void>((resolve) => {
- resolve();
- }),
- );
- shouldLog('Hello World Proxy', new Proxy(document, {}));
- shouldLog('Hello World RegExp', /abc/);
- shouldLog('Hello World Set', {a: [1, 2, 3]});
- shouldLog('Hello World TypedArray', new Uint8Array([1, 2, 3]));
- const wm3 = new WeakMap();
- const o1 = {};
- wm3.set(o1, 'azerty');
- const ws = new WeakSet();
- const foo2 = {};
+ ws.add(foo2);
- ws.add(foo2);
+ shouldLog('Hello World WeakMap', wm3);
+ shouldLog('Hello World WeakSet', ws);
- shouldLog('Hello World WeakMap', wm3);
- shouldLog('Hello World WeakSet', ws);
+ await new Promise((r) => {
+ setTimeout(r, 1000);
+ });
- await new Promise((r) => {
- setTimeout(r, 1000);
- });
-
- return {
- durationInFrames: props.duration,
- fps: 30,
+ return {
+ durationInFrames: props.duration,
+ fps: 30,
+ };
};
}, []);
@@ -205,7 +206,7 @@ export const Index: React.FC = () => {
durationInFrames={100}
calculateMetadata={calculateMetadata}
schema={dynamicDurationSchema}
- defaultProps={{duration: 20}}
+ defaultProps={{duration: 130}}
/>
<Composition
id="failing-dynamic-length"
diff --git a/packages/studio/src/components/RenderModal/DataEditor.tsx b/packages/studio/src/components/RenderModal/DataEditor.tsx
index 2effeab523..770a377f04 100644
--- a/packages/studio/src/components/RenderModal/DataEditor.tsx
+++ b/packages/studio/src/components/RenderModal/DataEditor.tsx
@@ -8,7 +8,7 @@ import React, {
import type {AnyComposition, SerializedJSONWithCustomFields} from 'remotion';
import {Internals, getInputProps} from 'remotion';
import {NoReactInternals} from 'remotion/no-react';
-import type {z} from 'zod';
+import {type z} from 'zod';
import {StudioServerConnectionCtx} from '../../helpers/client-id';
import {BACKGROUND, BORDER_COLOR, LIGHT_TEXT} from '../../helpers/colors';
import {ValidationMessage} from '../NewComposition/ValidationMessage';
@@ -308,10 +308,6 @@ export const DataEditor: React.FC<{
});
}, [unresolvedComposition.id, defaultProps, schema, z]);
- useEffect(() => {
- setSaving(false);
- }, [fastRefreshes, setSaving]);
-
const onSave = useCallback(
(
updater: (oldState: Record<string, unknown>) => Record<string, unknown>,
@@ -321,6 +317,7 @@ export const DataEditor: React.FC<{
return;
}
+ window.remotion_ignoreFastRefreshUpdate = fastRefreshes + 1;
setSaving(true);
callUpdateDefaultPropsApi(
unresolvedComposition.id,
@@ -340,11 +337,15 @@ export const DataEditor: React.FC<{
.catch((err) => {
showNotification(`Cannot update default props: ${err.message}`, 2000);
setSaving(false);
+ })
+ .finally(() => {
+ setSaving(false);
});
},
[
schema,
z,
+ fastRefreshes,
setSaving,
unresolvedComposition.id,
unresolvedComposition.defaultProps,