Skip to content

🎥 Make videos programmatically with React

`remotion`: simplify HMR reset to live in the error boundary

Jonny Burgerdd54ea6dd2ff
diff --git a/packages/core/src/Composition.tsx b/packages/core/src/Composition.tsx
index ce83032e93..4d8de7d417 100644
--- a/packages/core/src/Composition.tsx
+++ b/packages/core/src/Composition.tsx
@@ -12,7 +12,6 @@ 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';
@@ -232,7 +231,6 @@ const InnerComposition = <
const resolved = useResolvedVideoConfig(id);
const {setError, clearError} = useContext(CompositionRenderErrorContext);
- const fastRefreshSignal = useContext(FastRefreshSignalContext);
const onError = useCallback(
(error: Error) => {
@@ -262,11 +260,7 @@ const InnerComposition = <
return createPortal(
<CanUseRemotionHooksProvider>
- <CompositionErrorBoundary
- onError={onError}
- onClear={onClear}
- resetKey={fastRefreshSignal}
- >
+ <CompositionErrorBoundary onError={onError} onClear={onClear}>
<Suspense fallback={<Loading />}>
<Comp
{
diff --git a/packages/core/src/CompositionErrorBoundary.tsx b/packages/core/src/CompositionErrorBoundary.tsx
index 97c977e4c8..1288a5825d 100644
--- a/packages/core/src/CompositionErrorBoundary.tsx
+++ b/packages/core/src/CompositionErrorBoundary.tsx
@@ -4,55 +4,54 @@ 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, lastResetKey: this.props.resetKey};
+type Hot = {
+ addStatusHandler(callback: (status: string) => void): void;
+ removeStatusHandler(callback: (status: string) => void): void;
+};
- static getDerivedStateFromError(): Partial<State> {
- return {hasError: true};
- }
+// `__webpack_module__` is injected by webpack into every bundled module. It
+// is undefined outside of a webpack build (e.g. `@remotion/player` or
+// server-side rendering), so all access goes through `getHot()`.
+declare const __webpack_module__: {hot?: Hot} | undefined;
- static getDerivedStateFromProps(
- nextProps: Props,
- prevState: State,
- ): Partial<State> | null {
- if (nextProps.resetKey === prevState.lastResetKey) {
+const getHot = (): Hot | null => {
+ try {
+ if (typeof __webpack_module__ === 'undefined') {
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,
- };
+ return __webpack_module__.hot ?? null;
+ } catch {
+ return null;
+ }
+};
+
+export class CompositionErrorBoundary extends React.Component<Props, State> {
+ state: State = {hasError: false};
+
+ private hmrStatusHandler: ((status: string) => void) | null = null;
+
+ static getDerivedStateFromError(): Partial<State> {
+ return {hasError: true};
}
componentDidCatch(error: Error): void {
this.props.onError(error);
+ this.subscribeToHmrReset();
}
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.
+ // A fresh boundary mounting in the success state means any stale
+ // `renderError` left over by a previous boundary instance should be
+ // cleared. Fast Refresh sometimes unmounts the old (errored) boundary
+ // and mounts a new one during `apply`, so `componentDidUpdate`'s
+ // error→success transition never fires.
if (!this.state.hasError) {
this.props.onClear();
}
@@ -64,6 +63,55 @@ export class CompositionErrorBoundary extends React.Component<Props, State> {
}
}
+ componentWillUnmount(): void {
+ this.unsubscribeFromHmrReset();
+ }
+
+ private subscribeToHmrReset() {
+ if (this.hmrStatusHandler) {
+ return;
+ }
+
+ const hot = getHot();
+ if (!hot) {
+ return;
+ }
+
+ // Once the boundary catches a runtime error it returns `null` on every
+ // render and never retries the children — so any subsequent HMR fix
+ // would be invisible. While in the error state, wait for the next
+ // time webpack HMR settles (`idle`), drop the error flag, and let the
+ // boundary re-render. If the children still throw, `componentDidCatch`
+ // resubscribes. If they succeed, `componentDidUpdate` calls
+ // `onClear()`. See https://github.com/remotion-dev/remotion/issues/7447.
+ const handler = (status: string) => {
+ if (status !== 'idle') {
+ return;
+ }
+
+ this.unsubscribeFromHmrReset();
+ this.setState({hasError: false});
+ };
+
+ this.hmrStatusHandler = handler;
+ hot.addStatusHandler(handler);
+ }
+
+ private unsubscribeFromHmrReset() {
+ const handler = this.hmrStatusHandler;
+ if (!handler) {
+ return;
+ }
+
+ this.hmrStatusHandler = null;
+ const hot = getHot();
+ if (!hot) {
+ return;
+ }
+
+ hot.removeStatusHandler(handler);
+ }
+
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.ts
deleted file mode 100644
index 278c612498..0000000000
--- a/packages/core/src/fast-refresh-signal-context.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-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.ts
index 6f946c798c..9d97be4eb4 100644
--- a/packages/core/src/internals.ts
+++ b/packages/core/src/internals.ts
@@ -62,7 +62,6 @@ 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,
@@ -353,7 +352,6 @@ export const Internals = {
RenderAssetManagerProvider,
getEffectiveVisualModeValue,
CompositionRenderErrorContext,
- FastRefreshSignalContext,
useEffectChainState,
runEffectChain,
useMemoizedEffects,
diff --git a/packages/studio/src/FastRefreshProvider.tsx b/packages/studio/src/FastRefreshProvider.tsx
index 875919010b..a387d9fe8c 100644
--- a/packages/studio/src/FastRefreshProvider.tsx
+++ b/packages/studio/src/FastRefreshProvider.tsx
@@ -1,5 +1,4 @@
import React, {useCallback, useEffect, useMemo, useState} from 'react';
-import {Internals} from 'remotion';
import {FastRefreshContext} from './fast-refresh-context';
declare const __webpack_module__: {
@@ -37,9 +36,7 @@ export const FastRefreshProvider: React.FC<{
return (
<FastRefreshContext.Provider value={value}>
- <Internals.FastRefreshSignalContext.Provider value={fastRefreshes}>
- {children}
- </Internals.FastRefreshSignalContext.Provider>
+ {children}
</FastRefreshContext.Provider>
);
};