Skip to content

πŸŽ₯ Make videos programmatically with React

`remotion`: Fix InvalidStateError when HtmlInCanvas layout effect re-runs (#10511)

Alec Wattsecd2b67d05a3
diff --git a/packages/core/src/HtmlInCanvas.tsx b/packages/core/src/HtmlInCanvas.tsx
index 0c9a50f64f..ad55298d6a 100644
--- a/packages/core/src/HtmlInCanvas.tsx
+++ b/packages/core/src/HtmlInCanvas.tsx
@@ -191,6 +191,31 @@ export type HtmlInCanvasOnPaintParams = {
readonly pixelDensity: number;
};
+// `transferControlToOffscreen()` may only be called once per canvas β€” a second
+// call throws an `InvalidStateError`. The layout effect that needs the
+// `OffscreenCanvas` can run more than once for the same canvas element: its
+// dependencies (e.g. the paint callback) can change identity without the
+// canvas remounting, and React StrictMode intentionally double-invokes
+// effects. Cache the transferred `OffscreenCanvas` per canvas element so
+// re-runs reuse it instead of throwing.
+const transferredOffscreenCanvases = new WeakMap<
+ HTMLCanvasElement,
+ OffscreenCanvas
+>();
+
+const getTransferredOffscreenCanvas = (
+ canvas: HTMLCanvasElement,
+): OffscreenCanvas => {
+ const existing = transferredOffscreenCanvases.get(canvas);
+ if (existing) {
+ return existing;
+ }
+
+ const offscreen = canvas.transferControlToOffscreen();
+ transferredOffscreenCanvases.set(canvas, offscreen);
+ return offscreen;
+};
+
// Memoize the support check across the session β€” neither the platform
// capability nor the chrome://flags toggle can change between calls.
// SSR results are not cached so the check runs again once `document` exists.
@@ -631,7 +656,7 @@ const HtmlInCanvasContent = forwardRef<
const paintTarget = usesDirectLayoutCanvas
? placeholder
- : placeholder.transferControlToOffscreen();
+ : getTransferredOffscreenCanvas(placeholder);
paintTargetRef.current = paintTarget;
resizePaintTarget({
diff --git a/packages/core/src/test/html-in-canvas.test.tsx b/packages/core/src/test/html-in-canvas.test.tsx
index 06141e7f0c..f2997b0516 100644
--- a/packages/core/src/test/html-in-canvas.test.tsx
+++ b/packages/core/src/test/html-in-canvas.test.tsx
@@ -67,6 +67,7 @@ const stub2dContext = () => {
};
let transferControlToOffscreenCalls = 0;
+const transferredCanvases = new WeakSet<HTMLCanvasElement>();
Object.defineProperties(HTMLCanvasElement.prototype, {
getContext: {
@@ -96,6 +97,15 @@ Object.defineProperties(HTMLCanvasElement.prototype, {
transferControlToOffscreen: {
configurable: true,
value(this: HTMLCanvasElement) {
+ // Real browsers only allow one transfer per canvas.
+ if (transferredCanvases.has(this)) {
+ throw new DOMException(
+ 'Cannot transfer control from a canvas for more than one time.',
+ 'InvalidStateError',
+ );
+ }
+
+ transferredCanvases.add(this);
transferControlToOffscreenCalls++;
let contextMode: string | null = null;
const webgl2Context = {
@@ -474,6 +484,27 @@ test('<HtmlInCanvas> can use a higher backing density', async () => {
expect(transferControlToOffscreenCalls).toBe(1);
});
+test('<HtmlInCanvas> tolerates layout effect re-runs on the same canvas', async () => {
+ // StrictMode double-invokes effects on the same canvas element. A second
+ // transferControlToOffscreen() call throws an InvalidStateError in real
+ // browsers, so the transferred OffscreenCanvas must be reused.
+ const {container} = render(
+ <React.StrictMode>
+ <SequenceTestWrapper onRegisterSequence={() => undefined}>
+ <HtmlInCanvas width={50} height={50} onPaint={() => undefined}>
+ <div>Test</div>
+ </HtmlInCanvas>
+ </SequenceTestWrapper>
+ </React.StrictMode>,
+ );
+
+ await waitFor(() => {
+ expect(container.querySelector('canvas')).not.toBeNull();
+ });
+
+ expect(transferControlToOffscreenCalls).toBe(1);
+});
+
test('<HtmlInCanvas> does not apply pixel density to the live DOM transform', async () => {
const {container} = render(
<SequenceTestWrapper onRegisterSequence={() => undefined}>