π₯ Make videos programmatically with React
`remotion`: Dismiss in-canvas error overlay after Fast Refresh fixes the bug
Jonny Burgerd020d27fb274
diff --git a/packages/core/src/Composition.tsx b/packages/core/src/Composition.tsxindex 4d8de7d417..ce83032e93 100644--- a/packages/core/src/Composition.tsx+++ b/packages/core/src/Composition.tsx@@ -12,6 +12,7 @@ import {CompositionRenderErrorContext} from './composition-render-error-context.import {CompositionErrorBoundary} from './CompositionErrorBoundary.js';import type {TComposition} from './CompositionManager.js';import {CompositionSetters} from './CompositionManagerContext.js';+import {FastRefreshSignalContext} from './fast-refresh-signal-context.js';import {FolderContext} from './Folder.js';import {serializeThenDeserializeInStudio} from './input-props-serialization.js';import {useIsPlayer} from './is-player.js';@@ -231,6 +232,7 @@ const InnerComposition = <const resolved = useResolvedVideoConfig(id);const {setError, clearError} = useContext(CompositionRenderErrorContext);+ const fastRefreshSignal = useContext(FastRefreshSignalContext);const onError = useCallback((error: Error) => {@@ -260,7 +262,11 @@ const InnerComposition = <return createPortal(<CanUseRemotionHooksProvider>- <CompositionErrorBoundary onError={onError} onClear={onClear}>+ <CompositionErrorBoundary+ onError={onError}+ onClear={onClear}+ resetKey={fastRefreshSignal}+ ><Suspense fallback={<Loading />}><Comp{diff --git a/packages/core/src/CompositionErrorBoundary.tsx b/packages/core/src/CompositionErrorBoundary.tsxindex dd8791adbd..97c977e4c8 100644--- a/packages/core/src/CompositionErrorBoundary.tsx+++ b/packages/core/src/CompositionErrorBoundary.tsx@@ -4,29 +4,66 @@ type Props = {readonly children: React.ReactNode;readonly onError: (error: Error) => void;readonly onClear: () => void;+ // Bumped every time something happens that should cause the boundary to+ // retry rendering its children β currently every time Fast Refresh applies+ // an HMR update in the studio. Without this, once the boundary catches a+ // runtime error it returns `null` forever and the in-canvas error UI stays+ // up even after the user fixes the bug.+ // See https://github.com/remotion-dev/remotion/issues/7447.+ readonly resetKey: number;};type State = {hasError: boolean;+ lastResetKey: number;};export class CompositionErrorBoundary extends React.Component<Props, State> {- state: State = {hasError: false};+ state: State = {hasError: false, lastResetKey: this.props.resetKey};static getDerivedStateFromError(): Partial<State> {return {hasError: true};}+ static getDerivedStateFromProps(+ nextProps: Props,+ prevState: State,+ ): Partial<State> | null {+ if (nextProps.resetKey === prevState.lastResetKey) {+ return null;+ }++ // Drop the error state on every reset. If the children still throw,+ // `getDerivedStateFromError` will set `hasError` back to `true` during+ // the same render pass. If they succeed, `componentDidUpdate` will fire+ // `onClear()` so the studio can dismiss the error UI.+ return {+ hasError: false,+ lastResetKey: nextProps.resetKey,+ };+ }+componentDidCatch(error: Error): void {this.props.onError(error);}- componentDidUpdate(_prevProps: Props): void {+ componentDidMount(): void {+ // If a freshly mounted boundary renders its children successfully, tell+ // the host to clear any stale render error. This matters when Fast+ // Refresh causes the previous boundary instance (which was stuck in the+ // error state) to unmount and a new one to mount in its place β in that+ // case `componentDidUpdate` never fires the transition.if (!this.state.hasError) {this.props.onClear();}}+ componentDidUpdate(_prevProps: Props, prevState: State): void {+ if (prevState.hasError && !this.state.hasError) {+ this.props.onClear();+ }+ }+render() {if (this.state.hasError) {return null;diff --git a/packages/core/src/fast-refresh-signal-context.ts b/packages/core/src/fast-refresh-signal-context.tsnew file mode 100644index 0000000000..278c612498--- /dev/null+++ b/packages/core/src/fast-refresh-signal-context.ts@@ -0,0 +1,13 @@+import {createContext} from 'react';++// Incremented every time the studio detects that webpack HMR has applied+// updates (i.e. once `__webpack_module__.hot` reports the `idle` status).+//+// This is read by `CompositionErrorBoundary` to know when to drop the+// `hasError` flag and attempt to render the composition again. Without it,+// once the boundary catches a runtime error, it returns `null` forever and+// the error UI persists even after the user fixes the underlying bug.+//+// Defaults to `0` (the value used when no provider is in the tree, e.g.+// in `@remotion/player`, where there is no Fast Refresh).+export const FastRefreshSignalContext = createContext<number>(0);diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.tsindex 9d97be4eb4..6f946c798c 100644--- a/packages/core/src/internals.ts+++ b/packages/core/src/internals.ts@@ -62,6 +62,7 @@ import {addSequenceStackTraces,getComponentsToAddStacksTo,} from './enable-sequence-stack-traces.js';+import {FastRefreshSignalContext} from './fast-refresh-signal-context.js';import {findPropsToDelete} from './find-props-to-delete.js';import {flattenActiveSchema,@@ -352,6 +353,7 @@ export const Internals = {RenderAssetManagerProvider,getEffectiveVisualModeValue,CompositionRenderErrorContext,+ FastRefreshSignalContext,useEffectChainState,runEffectChain,useMemoizedEffects,diff --git a/packages/example/e2e/constants.mts b/packages/example/e2e/constants.mtsindex 76a663bffb..3f6fa5a57d 100644--- a/packages/example/e2e/constants.mts+++ b/packages/example/e2e/constants.mts@@ -24,6 +24,12 @@ export const lostNodePathE2eFile = path.join('LostNodePathE2e','LostNodePathRepro.tsx',);+export const errorOverlayE2eFile = path.join(+ exampleDir,+ 'src',+ 'ErrorOverlayE2e',+ 'ErrorOverlayRepro.tsx',+);export const newVideoFile = path.join(exampleDir, 'src', 'NewVideo.tsx');export const remotionBin = path.join(exampleDir,@@ -48,6 +54,10 @@ export const ORIGINAL_LOST_NODE_PATH_E2E_FILE = path.join(os.tmpdir(),'remotion-e2e-original-lost-node-path-repro.tsx',);+export const ORIGINAL_ERROR_OVERLAY_E2E_FILE = path.join(+ os.tmpdir(),+ 'remotion-e2e-original-error-overlay-repro.tsx',+);export const PID_FILE = path.join(os.tmpdir(), 'remotion-e2e-studio.pid');export const EXPANDED_SIDEBAR_STATE = path.join(os.tmpdir(),diff --git a/packages/example/e2e/error-overlay.test.mts b/packages/example/e2e/error-overlay.test.mtsnew file mode 100644index 0000000000..966ce67c38--- /dev/null+++ b/packages/example/e2e/error-overlay.test.mts@@ -0,0 +1,102 @@+import fs from 'fs';+import {expect, test} from '@playwright/test';+import {errorOverlayE2eFile, STUDIO_URL} from './constants.mts';+import {readStudioLogs, stripAnsi} from './helpers.mts';+import {startStudio, stopStudio} from './studio-server.mts';++/**+ * Regression test for https://github.com/remotion-dev/remotion/issues/7447+ *+ * Removing the `radius: 24` argument from `blur({radius: 24})` causes the+ * `<Solid>` component to throw a `TypeError` during render. The studio's+ * `CompositionErrorBoundary` catches it and shows the error UI inside the+ * canvas area. Adding the argument back should dismiss the error once HMR+ * applies the fix.+ *+ * The bug is that the error UI does not dismiss until the page is reloaded:+ * `CompositionErrorBoundary` sets `hasError: true` on catch, returns `null`+ * forever, and never resets that flag, so the boundary's children are never+ * re-rendered after the fix arrives.+ */+test.describe('error overlay dismissal', () => {+ test.beforeEach(async () => {+ await startStudio();+ });++ test.afterEach(async () => {+ await stopStudio();+ });++ test('error UI dismisses after the underlying runtime error is fixed', async ({+ page,+ }) => {+ const originalContent = fs.readFileSync(errorOverlayE2eFile, 'utf-8');+ expect(originalContent).toContain('blur({radius: 24})');+ // Make sure the marker only appears once so the swap is unambiguous.+ expect(originalContent.split('blur({radius: 24})')).toHaveLength(2);++ await page.goto(`${STUDIO_URL}/error-overlay-e2e`);+ await expect(page).toHaveURL(/error-overlay-e2e/, {timeout: 15_000});++ // Sanity check: no error visible initially.+ await expect(+ page.getByText('"radius" must be a finite number'),+ ).toHaveCount(0);++ // 1. Introduce the bug: remove the `radius: 24` argument.+ const buggyContent = originalContent.replace(+ 'blur({radius: 24})',+ 'blur({})',+ );+ expect(buggyContent).not.toBe(originalContent);++ const logCountBeforeBug = readStudioLogs().length;+ fs.writeFileSync(errorOverlayE2eFile, buggyContent);++ // Webpack rebuilds successfully β the bug is a runtime error, not a build error.+ await expect+ .poll(+ () => {+ const newLogs = readStudioLogs()+ .slice(logCountBeforeBug)+ .map(stripAnsi);+ return newLogs.some((log) => log.includes('Built in'));+ },+ {+ message: 'Expected webpack to rebuild after introducing the bug',+ timeout: 30_000,+ },+ )+ .toBe(true);++ // Error should appear with the radius validation message.+ await expect(+ page.getByText('"radius" must be a finite number'),+ ).toBeVisible({timeout: 15_000});++ // 2. Fix the bug: restore the `radius: 24` argument.+ const logCountBeforeFix = readStudioLogs().length;+ fs.writeFileSync(errorOverlayE2eFile, originalContent);++ await expect+ .poll(+ () => {+ const newLogs = readStudioLogs()+ .slice(logCountBeforeFix)+ .map(stripAnsi);+ return newLogs.some((log) => log.includes('Built in'));+ },+ {+ message: 'Expected webpack to rebuild after fixing the bug',+ timeout: 30_000,+ },+ )+ .toBe(true);++ // 3. The error UI should dismiss once HMR applies the fix. This is the+ // assertion that currently fails β it stays on screen forever.+ await expect(+ page.getByText('"radius" must be a finite number'),+ ).toHaveCount(0, {timeout: 15_000});+ });+});diff --git a/packages/example/e2e/global-setup.mts b/packages/example/e2e/global-setup.mtsindex 7d0d914d4c..86ac0ec833 100644--- a/packages/example/e2e/global-setup.mts+++ b/packages/example/e2e/global-setup.mts@@ -1,8 +1,10 @@import fs from 'fs';import {ORIGINAL_CONTENT_FILE,+ ORIGINAL_ERROR_OVERLAY_E2E_FILE,ORIGINAL_LOST_NODE_PATH_E2E_FILE,ORIGINAL_VISUAL_CONTROLS_FILE,+ errorOverlayE2eFile,lostNodePathE2eFile,rootFile,visualControlsFile,@@ -18,4 +20,8 @@ export default async function globalSetup(): Promise<void> {ORIGINAL_LOST_NODE_PATH_E2E_FILE,fs.readFileSync(lostNodePathE2eFile, 'utf-8'),);+ fs.writeFileSync(+ ORIGINAL_ERROR_OVERLAY_E2E_FILE,+ fs.readFileSync(errorOverlayE2eFile, 'utf-8'),+ );}diff --git a/packages/example/e2e/global-teardown.mts b/packages/example/e2e/global-teardown.mtsindex b5ee3a492a..c2c151b702 100644--- a/packages/example/e2e/global-teardown.mts+++ b/packages/example/e2e/global-teardown.mts@@ -1,8 +1,10 @@import fs from 'fs';import {ORIGINAL_CONTENT_FILE,+ ORIGINAL_ERROR_OVERLAY_E2E_FILE,ORIGINAL_LOST_NODE_PATH_E2E_FILE,ORIGINAL_VISUAL_CONTROLS_FILE,+ errorOverlayE2eFile,lostNodePathE2eFile,rootFile,visualControlsFile,@@ -29,4 +31,12 @@ export default async function globalTeardown(): Promise<void> {);fs.unlinkSync(ORIGINAL_LOST_NODE_PATH_E2E_FILE);}++ if (fs.existsSync(ORIGINAL_ERROR_OVERLAY_E2E_FILE)) {+ fs.writeFileSync(+ errorOverlayE2eFile,+ fs.readFileSync(ORIGINAL_ERROR_OVERLAY_E2E_FILE, 'utf-8'),+ );+ fs.unlinkSync(ORIGINAL_ERROR_OVERLAY_E2E_FILE);+ }}diff --git a/packages/example/e2e/studio-server.mts b/packages/example/e2e/studio-server.mtsindex 751f2b5d68..74750e617f 100644--- a/packages/example/e2e/studio-server.mts+++ b/packages/example/e2e/studio-server.mts@@ -5,11 +5,13 @@ import {EXPANDED_SIDEBAR_STATE,LOGS_FILE,ORIGINAL_CONTENT_FILE,+ ORIGINAL_ERROR_OVERLAY_E2E_FILE,ORIGINAL_LOST_NODE_PATH_E2E_FILE,ORIGINAL_VISUAL_CONTROLS_FILE,STUDIO_PORT,STUDIO_URL,e2eEntryPoint,+ errorOverlayE2eFile,exampleDir,lostNodePathE2eFile,remotionBin,@@ -60,6 +62,13 @@ export async function startStudio(): Promise<void> {);}+ if (!fs.existsSync(ORIGINAL_ERROR_OVERLAY_E2E_FILE)) {+ fs.writeFileSync(+ ORIGINAL_ERROR_OVERLAY_E2E_FILE,+ fs.readFileSync(errorOverlayE2eFile, 'utf-8'),+ );+ }+// Restore original files before startingfs.writeFileSync(rootFile, fs.readFileSync(ORIGINAL_CONTENT_FILE, 'utf-8'));fs.writeFileSync(@@ -70,6 +79,10 @@ export async function startStudio(): Promise<void> {lostNodePathE2eFile,fs.readFileSync(ORIGINAL_LOST_NODE_PATH_E2E_FILE, 'utf-8'),);+ fs.writeFileSync(+ errorOverlayE2eFile,+ fs.readFileSync(ORIGINAL_ERROR_OVERLAY_E2E_FILE, 'utf-8'),+ );fs.writeFileSync(LOGS_FILE, '');fs.writeFileSync(@@ -201,4 +214,11 @@ export async function stopStudio(): Promise<void> {fs.readFileSync(ORIGINAL_LOST_NODE_PATH_E2E_FILE, 'utf-8'),);}++ if (fs.existsSync(ORIGINAL_ERROR_OVERLAY_E2E_FILE)) {+ fs.writeFileSync(+ errorOverlayE2eFile,+ fs.readFileSync(ORIGINAL_ERROR_OVERLAY_E2E_FILE, 'utf-8'),+ );+ }}diff --git a/packages/example/src/E2eTestRoot.tsx b/packages/example/src/E2eTestRoot.tsxindex bfcf9654ae..7b3d8f0055 100644--- a/packages/example/src/E2eTestRoot.tsx+++ b/packages/example/src/E2eTestRoot.tsx@@ -1,5 +1,6 @@import React from 'react';import {Composition, Folder} from 'remotion';+import {ErrorOverlayRepro} from './ErrorOverlayE2e/ErrorOverlayRepro';import {LostNodePathRepro} from './LostNodePathE2e/LostNodePathRepro';import {NewVideoComp} from './NewVideo';import {SchemaTest, schemaTestSchema} from './SchemaTest';@@ -55,6 +56,16 @@ export const E2eTestRoot: React.FC = () => {durationInFrames={90}/></Folder>+ <Folder name="error-overlay">+ <Composition+ id="error-overlay-e2e"+ component={ErrorOverlayRepro}+ width={400}+ height={400}+ fps={30}+ durationInFrames={30}+ />+ </Folder><NewVideoComp /></>);diff --git a/packages/example/src/ErrorOverlayE2e/ErrorOverlayRepro.tsx b/packages/example/src/ErrorOverlayE2e/ErrorOverlayRepro.tsxnew file mode 100644index 0000000000..f592fc3148--- /dev/null+++ b/packages/example/src/ErrorOverlayE2e/ErrorOverlayRepro.tsx@@ -0,0 +1,22 @@+// Regression fixture for https://github.com/remotion-dev/remotion/issues/7447+// The error overlay does not dismiss after a runtime error is fixed.+// The e2e test toggles the radius argument below to reproduce.+import {blur} from '@remotion/effects/blur';+import React from 'react';+import {AbsoluteFill, Solid} from 'remotion';++const STYLE: React.CSSProperties = {width: '100%', height: '100%'};++export const ErrorOverlayRepro: React.FC = () => {+ return (+ <AbsoluteFill style={{backgroundColor: 'white'}}>+ <Solid+ width={400}+ height={400}+ color="#ff5fa2"+ style={STYLE}+ _experimentalEffects={[blur({radius: 24})]}+ />+ </AbsoluteFill>+ );+};diff --git a/packages/studio/src/FastRefreshProvider.tsx b/packages/studio/src/FastRefreshProvider.tsxindex a387d9fe8c..875919010b 100644--- a/packages/studio/src/FastRefreshProvider.tsx+++ b/packages/studio/src/FastRefreshProvider.tsx@@ -1,4 +1,5 @@import React, {useCallback, useEffect, useMemo, useState} from 'react';+import {Internals} from 'remotion';import {FastRefreshContext} from './fast-refresh-context';declare const __webpack_module__: {@@ -36,7 +37,9 @@ export const FastRefreshProvider: React.FC<{return (<FastRefreshContext.Provider value={value}>- {children}+ <Internals.FastRefreshSignalContext.Provider value={fastRefreshes}>+ {children}+ </Internals.FastRefreshSignalContext.Provider></FastRefreshContext.Provider>);};