π₯ Make videos programmatically with React
remotion: Fix retention of replaced components and callbacks
Jonny Burgerf7592662d7a0
diff --git a/packages/browser-studio/e2e/sequence-retention.test.ts b/packages/browser-studio/e2e/sequence-retention.test.tsindex 418e7fe5fd..55718b6778 100644--- a/packages/browser-studio/e2e/sequence-retention.test.ts+++ b/packages/browser-studio/e2e/sequence-retention.test.ts@@ -20,7 +20,7 @@ for (const layout of ['none', 'absolute-fill'] as const) {context: import.meta.dirname,entry: './sequence-retention-app.tsx',mode: 'production',- optimization: {minimize: false},+ optimization: {minimize: true},output: {path: output, filename: 'app.js'},resolve: {extensions: ['.tsx', '.ts', '.js'],diff --git a/packages/core/src/Composition.tsx b/packages/core/src/Composition.tsxindex 8e772698cf..f04d7b63ff 100644--- a/packages/core/src/Composition.tsx+++ b/packages/core/src/Composition.tsx@@ -1,11 +1,5 @@import type {ComponentType} from 'react';-import React, {- Suspense,- useCallback,- useContext,- useEffect,- useMemo,-} from 'react';+import React, {Suspense, useContext, useEffect, useMemo} from 'react';import {createPortal} from 'react-dom';import type {z} from 'zod';import type {AnyZodObject} from './any-zod-type.js';@@ -244,18 +238,9 @@ const InnerComposition = <const resolved = useResolvedVideoConfig(id);- const {setError, clearError} = useContext(CompositionRenderErrorContext);-- const onError = useCallback(- (error: Error) => {- setError(error);- },- [setError],+ const {setError: onError, clearError: onClear} = useContext(+ CompositionRenderErrorContext,);-- const onClear = useCallback(() => {- clearError();- }, [clearError]);const wrapRegistration = (content: React.ReactNode) => (<CommittedMetadataProvider value={null} _remotionCommitMetadata={metadata}>{content}diff --git a/packages/core/src/Img.tsx b/packages/core/src/Img.tsxindex 07efe8c285..139d2afca2 100644--- a/packages/core/src/Img.tsx+++ b/packages/core/src/Img.tsx@@ -1,5 +1,4 @@import React, {- useCallback,useContext,useLayoutEffect,useMemo,@@ -102,6 +101,221 @@ type ImgContentProps = Omit<| 'cropBottom'>;+// Bind only the inputs each persistent callback needs. A render-scope closure+// can otherwise keep replaced handlers alive, even when it never calls them.+const setImageRef = (+ imageRef: React.RefObject<HTMLImageElement | null>,+ ref: React.Ref<HTMLImageElement> | null,+ img: HTMLImageElement | null,+) => {+ imageRef.current = img;+ if (typeof ref === 'function') {+ ref(img);+ } else if (ref) {+ ref.current = img;+ }+};++const retryImage = (+ imageRef: React.RefObject<HTMLImageElement | null>,+ timeout: number,+) => {+ if (!imageRef.current) {+ return;+ }++ const currentSrc = imageRef.current.src;++ setTimeout(() => {+ if (!imageRef.current) {+ // Component has been unmounted, do not retry+ return;+ }++ const newSrc = imageRef.current?.src;+ if (newSrc !== currentSrc) {+ // src has changed, do not retry+ return;+ }++ imageRef.current.removeAttribute('src');+ imageRef.current.setAttribute('src', newSrc);+ }, timeout);+};++const handleImageError = (+ {+ errors,+ imageRef,+ onError,+ onImageError,+ maxRetries,+ cancelRender,+ retryIn,+ }: {+ errors: React.RefObject<Record<string, number>>;+ imageRef: React.RefObject<HTMLImageElement | null>;+ onError: NonNullable<ImgProps['onError']> | null;+ onImageError: NonNullable<ImgProps['onImageError']> | null;+ maxRetries: number;+ cancelRender: ReturnType<typeof useDelayRender>['cancelRender'];+ retryIn: (timeout: number) => void;+ },+ e: React.SyntheticEvent<HTMLImageElement, Event>,+) => {+ if (!errors.current) {+ return;+ }++ errors.current[imageRef.current?.src as string] =+ (errors.current[imageRef.current?.src as string] ?? 0) + 1;+ if (+ (onError || onImageError) &&+ (errors.current[imageRef.current?.src as string] ?? 0) > maxRetries+ ) {+ onError?.(e);+ onImageError?.(+ new Error(+ 'Error loading image with src: ' ++ truncateSrcForLabel(imageRef.current?.src as string),+ ),+ );+ return;+ }++ if ((errors.current[imageRef.current?.src as string] ?? 0) <= maxRetries) {+ const backoff = exponentialBackoff(+ errors.current[imageRef.current?.src as string] ?? 0,+ );+ // eslint-disable-next-line no-console+ console.warn(+ `Could not load image with source ${truncateSrcForLabel(+ imageRef.current?.src as string,+ )}, retrying again in ${backoff}ms`,+ );++ retryIn(backoff);+ return;+ }++ try {+ cancelRender(+ 'Error loading image with src: ' ++ truncateSrcForLabel(imageRef.current?.src as string),+ );+ } catch {+ // cancelRender() intentionally throws after storing the error in scope.+ // In async image callbacks, we rely on the stored error for renderer propagation.+ }+};++// Keep the loading cleanup outside the render scope too: it can outlive many+// handler replacements while the source and onImageFrame stay unchanged.+const loadImage = ({+ imageRef,+ errors,+ actualSrc,+ delayRenderRetries,+ delayRenderTimeoutInMilliseconds,+ onImageFrame,+ delayRender,+ continueRender,+ setIsLoading,+}: {+ imageRef: React.RefObject<HTMLImageElement | null>;+ errors: React.RefObject<Record<string, number>>;+ actualSrc: string;+ delayRenderRetries: number | null;+ delayRenderTimeoutInMilliseconds: number | null;+ onImageFrame: NonNullable<ImgProps['onImageFrame']> | null;+ delayRender: ReturnType<typeof useDelayRender>['delayRender'];+ continueRender: ReturnType<typeof useDelayRender>['continueRender'];+ setIsLoading: React.Dispatch<React.SetStateAction<boolean>>;+}) => {+ if (window.process?.env?.NODE_ENV === 'test') {+ if (imageRef.current) {+ imageRef.current.src = actualSrc;+ }++ return;+ }++ const {current} = imageRef;+ if (!current) {+ return;+ }++ setIsLoading(true);+ const newHandle = delayRender(+ 'Loading <Img> with src=' + truncateSrcForLabel(actualSrc),+ {+ retries: delayRenderRetries ?? undefined,+ timeoutInMilliseconds: delayRenderTimeoutInMilliseconds ?? undefined,+ },+ );++ let unmounted = false;++ const onComplete = () => {+ // the decode() promise isn't cancelable -- it may still resolve after unmounting+ if (unmounted) {+ continueRender(newHandle);+ return;+ }++ if ((errors.current[imageRef.current?.src as string] ?? 0) > 0) {+ delete errors.current[imageRef.current?.src as string];+ // eslint-disable-next-line no-console+ console.info(+ `Retry successful - ${truncateSrcForLabel(+ imageRef.current?.src as string,+ )} is now loaded`,+ );+ }++ if (current) {+ onImageFrame?.(current);+ }++ setIsLoading(false);+ continueRender(newHandle);+ };++ if (!imageRef.current) {+ onComplete();+ return;+ }++ current.src = actualSrc;+ current+ .decode()+ .then(onComplete)+ .catch((err) => {+ // fall back to onload event if decode() fails+ // eslint-disable-next-line no-console+ console.warn(err);++ // HTMLImageElement.complete is also true for broken images (e.g. 404),+ // so only treat it as loaded if intrinsic dimensions are available.+ if (+ current.complete &&+ current.naturalWidth > 0 &&+ current.naturalHeight > 0+ ) {+ onComplete();+ } else {+ current.addEventListener('load', onComplete);+ }+ });++ // If tag gets unmounted, clear pending handles because image is not going to load+ return () => {+ unmounted = true;+ current.removeEventListener('load', onComplete);+ continueRender(newHandle);+ };+};+const ImgContent: React.FC<ImgContentProps> = ({onError,onImageError,@@ -128,98 +342,30 @@ const ImgContent: React.FC<ImgContentProps> = ({throw new Error('typecheck error');}- const imageCallbackRef = useCallback(- (img: HTMLImageElement | null) => {- imageRef.current = img;-- if (typeof ref === 'function') {- ref(img);- } else if (ref) {- ref.current = img;- }- },+ const imageCallbackRef = useMemo(+ () => setImageRef.bind(null, imageRef, ref ?? null),[ref],);const actualSrc = usePreload(src as string);- const retryIn = useCallback((timeout: number) => {- if (!imageRef.current) {- return;- }-- const currentSrc = imageRef.current.src;-- setTimeout(() => {- if (!imageRef.current) {- // Component has been unmounted, do not retry- return;- }-- const newSrc = imageRef.current?.src;- if (newSrc !== currentSrc) {- // src has changed, do not retry- return;- }-- imageRef.current.removeAttribute('src');- imageRef.current.setAttribute('src', newSrc);- }, timeout);- }, []);+ const retryIn = useMemo(() => retryImage.bind(null, imageRef), []);const {delayRender, continueRender, cancelRender} = useDelayRender();const isPremounting = Boolean(sequenceContext?.premounting);const isPostmounting = Boolean(sequenceContext?.postmounting);- const didGetError = useCallback(- (e: React.SyntheticEvent<HTMLImageElement, Event>) => {- if (!errors.current) {- return;- }-- errors.current[imageRef.current?.src as string] =- (errors.current[imageRef.current?.src as string] ?? 0) + 1;- if (- (onError || onImageError) &&- (errors.current[imageRef.current?.src as string] ?? 0) > maxRetries- ) {- onError?.(e);- onImageError?.(- new Error(- 'Error loading image with src: ' +- truncateSrcForLabel(imageRef.current?.src as string),- ),- );- return;- }-- if (- (errors.current[imageRef.current?.src as string] ?? 0) <= maxRetries- ) {- const backoff = exponentialBackoff(- errors.current[imageRef.current?.src as string] ?? 0,- );- // eslint-disable-next-line no-console- console.warn(- `Could not load image with source ${truncateSrcForLabel(- imageRef.current?.src as string,- )}, retrying again in ${backoff}ms`,- );-- retryIn(backoff);- return;- }-- try {- cancelRender(- 'Error loading image with src: ' +- truncateSrcForLabel(imageRef.current?.src as string),- );- } catch {- // cancelRender() intentionally throws after storing the error in scope.- // In async image callbacks, we rely on the stored error for renderer propagation.- }- },+ const didGetError = useMemo(+ () =>+ handleImageError.bind(null, {+ errors,+ imageRef,+ onError: onError ?? null,+ onImageError: onImageError ?? null,+ maxRetries,+ cancelRender,+ retryIn,+ }),[cancelRender, maxRetries, onError, onImageError, retryIn],);@@ -239,98 +385,31 @@ const ImgContent: React.FC<ImgContentProps> = ({pauseWhenLoading,]);- // eslint-disable-next-line react-hooks/rules-of-hooks- useLayoutEffect(() => {- if (window.process?.env?.NODE_ENV === 'test') {- if (imageRef.current) {- imageRef.current.src = actualSrc;- }-- return;- }-- const {current} = imageRef;- if (!current) {- return;- }-- setIsLoading(true);- const newHandle = delayRender(- 'Loading <Img> with src=' + truncateSrcForLabel(actualSrc),- {- retries: delayRenderRetries ?? undefined,- timeoutInMilliseconds: delayRenderTimeoutInMilliseconds ?? undefined,- },- );-- let unmounted = false;-- const onComplete = () => {- // the decode() promise isn't cancelable -- it may still resolve after unmounting- if (unmounted) {- continueRender(newHandle);- return;- }-- if ((errors.current[imageRef.current?.src as string] ?? 0) > 0) {- delete errors.current[imageRef.current?.src as string];- // eslint-disable-next-line no-console- console.info(- `Retry successful - ${truncateSrcForLabel(- imageRef.current?.src as string,- )} is now loaded`,- );- }-- if (current) {- onImageFrame?.(current);- }-- setIsLoading(false);- continueRender(newHandle);- };-- if (!imageRef.current) {- onComplete();- return;- }-- current.src = actualSrc;- current- .decode()- .then(onComplete)- .catch((err) => {- // fall back to onload event if decode() fails- // eslint-disable-next-line no-console- console.warn(err);-- // HTMLImageElement.complete is also true for broken images (e.g. 404),- // so only treat it as loaded if intrinsic dimensions are available.- if (- current.complete &&- current.naturalWidth > 0 &&- current.naturalHeight > 0- ) {- onComplete();- } else {- current.addEventListener('load', onComplete);- }- });-- // If tag gets unmounted, clear pending handles because image is not going to load- return () => {- unmounted = true;- current.removeEventListener('load', onComplete);- continueRender(newHandle);- };- }, [- actualSrc,- delayRenderRetries,- delayRenderTimeoutInMilliseconds,- onImageFrame,- continueRender,- delayRender,- ]);+ // Binding prevents production minification from inlining this scope+ // back into the component and retaining unrelated handlers.+ // eslint-disable-next-line react-hooks/rules-of-hooks, react-hooks/exhaustive-deps+ useLayoutEffect(+ loadImage.bind(null, {+ imageRef,+ errors,+ actualSrc,+ delayRenderRetries: delayRenderRetries ?? null,+ delayRenderTimeoutInMilliseconds:+ delayRenderTimeoutInMilliseconds ?? null,+ onImageFrame: onImageFrame ?? null,+ delayRender,+ continueRender,+ setIsLoading,+ }),+ [+ actualSrc,+ delayRenderRetries,+ delayRenderTimeoutInMilliseconds,+ onImageFrame,+ continueRender,+ delayRender,+ ],+ );}const {isClientSideRendering, isRendering} = useRemotionEnvironment();diff --git a/packages/core/src/Sequence.tsx b/packages/core/src/Sequence.tsxindex 6bcaa16b15..257fa6383f 100644--- a/packages/core/src/Sequence.tsx+++ b/packages/core/src/Sequence.tsx@@ -1,7 +1,6 @@import React, {createContext,forwardRef,- useCallback,useContext,useLayoutEffect,useMemo,@@ -174,31 +173,31 @@ export type SequenceProps = {readonly durationInFrames?: number;} & SequencePropsWithoutDuration;-// These callbacks can survive many renders. Keep their closure scopes separate-// from the render scope, which also captures children in memo factories.-const useSequenceValueGetter = <T,>(value: T) => {- return useCallback(() => value, [value]);+// Bind explicit arguments instead of capturing render variables. Extracted hooks+// can be inlined by minifiers and retain children through their shared scope.+const getSequenceRegistration = (sequence: TSequence): TSequence => {+ return sequence;+};++const getSequenceStack = (stack: string | null): string | null => {+ return stack;};-const useSequenceRefGetter = <T,>(ref: React.RefObject<T>) => {- return useCallback(() => ref.current, [ref]);+const getSequenceCurrentFrame = (ref: React.RefObject<number>): number => {+ return ref.current;};-const useSequenceWrapperRef = (+const setSequenceWrapperRef = (ref: React.ForwardedRef<HTMLDivElement>,wrapperRef: React.RefObject<HTMLDivElement | null>,+ node: HTMLDivElement | null,) => {- return useCallback(- (node: HTMLDivElement | null) => {- wrapperRef.current = node;- if (typeof ref === 'function') {- ref(node);- } else if (ref) {- ref.current = node;- }- },- [ref, wrapperRef],- );+ wrapperRef.current = node;+ if (typeof ref === 'function') {+ ref(node);+ } else if (ref) {+ ref.current = node;+ }};const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<@@ -595,7 +594,10 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<const registrationStack = controls? (getStackForControls(controls) ?? stack ?? null): (stack ?? null);- const getStack = useSequenceValueGetter(registrationStack);+ const getStack = useMemo(+ () => getSequenceStack.bind(null, registrationStack),+ [registrationStack],+ );const registeredFrozenFrame = typeof freeze === 'number' ? freeze : null;const currentFrame =registeredFrozenFrame ??@@ -604,7 +606,10 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<useLayoutEffect(() => {currentFrameRef.current = currentFrame;}, [currentFrame]);- const getCurrentFrame = useSequenceRefGetter(currentFrameRef);+ const getCurrentFrame = useMemo(+ () => getSequenceCurrentFrame.bind(null, currentFrameRef),+ [currentFrameRef],+ );const registeredTrimBefore = trimBefore === 0 ? null : trimBefore;const parentCumulatedNegativeFrom =parentSequence?.cumulatedNegativeFrom ?? 0;@@ -834,8 +839,9 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<frozenMediaFrame,singleChildComponent,]);- const getSequenceForRegistration = useSequenceValueGetter(- sequenceForRegistration,+ const getSequenceForRegistration = useMemo(+ () => getSequenceRegistration.bind(null, sequenceForRegistration),+ [sequenceForRegistration],);const registration = useSequenceRegistration({getSequence:@@ -910,7 +916,10 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<style: styleIfThere,});- const sequenceRef = useSequenceWrapperRef(ref, wrapperRefForOutline);+ const sequenceRef = useMemo(+ () => setSequenceWrapperRef.bind(null, ref, wrapperRefForOutline),+ [ref, wrapperRefForOutline],+ );const defaultStyle: React.CSSProperties = useMemo(() => {return {diff --git a/packages/core/src/audio/AudioForPreview.tsx b/packages/core/src/audio/AudioForPreview.tsxindex a4e3f4b118..1c993c2c7f 100644--- a/packages/core/src/audio/AudioForPreview.tsx+++ b/packages/core/src/audio/AudioForPreview.tsx@@ -3,10 +3,8 @@ import type {AudioHTMLAttributes} from 'react';import React, {forwardRef,useContext,- useEffect,useImperativeHandle,useMemo,- useRef,useState,} from 'react';import {@@ -20,6 +18,10 @@ import {random} from '../random.js';import {SequenceContent} from '../sequence-activity-context.js';import {SequenceContext} from '../SequenceContext.js';import {useVolume} from '../use-amplification.js';+import {+ scheduleMediaSourceCleanup,+ useMediaDuration,+} from '../use-media-duration.js';import {useMediaInTimelineRegistration} from '../use-media-in-timeline.js';import {useMediaPlayback} from '../use-media-playback.js';import {useMediaTag} from '../use-media-tag.js';@@ -209,41 +211,14 @@ const AudioForDevelopmentForwardRefFunction: React.ForwardRefRenderFunction<// But useInsertionEffect will fire before other effects, meaning the// nodes might still be used. Using rAF to ensure it's after other effects.effectToUse(() => {- return () => {- requestAnimationFrame(() => {- cleanupOnMediaTagUnmount();- });- };+ return scheduleMediaSourceCleanup.bind(null, cleanupOnMediaTagUnmount);}, [cleanupOnMediaTagUnmount]);useImperativeHandle(ref, () => {return audioRef.current as HTMLAudioElement;}, [audioRef]);- const currentOnDurationCallback =- useRef<AudioForPreviewProps['onDuration']>(onDuration);- currentOnDurationCallback.current = onDuration;-- useEffect(() => {- const {current} = audioRef;- if (!current) {- return;- }-- if (current.duration) {- currentOnDurationCallback.current?.(current.src, current.duration);- return;- }-- const onLoadedMetadata = () => {- currentOnDurationCallback.current?.(current.src, current.duration);- };-- current.addEventListener('loadedmetadata', onLoadedMetadata);- return () => {- current.removeEventListener('loadedmetadata', onLoadedMetadata);- };- }, [audioRef, src]);+ useMediaDuration(audioRef, onDuration, src, true);if (initialShouldPreMountAudioElements) {return null;diff --git a/packages/core/src/audio/html5-audio.tsx b/packages/core/src/audio/html5-audio.tsxindex 07e0bde733..e94afc5143 100644--- a/packages/core/src/audio/html5-audio.tsx+++ b/packages/core/src/audio/html5-audio.tsx@@ -1,5 +1,5 @@/* eslint-disable @typescript-eslint/no-use-before-define */-import React, {forwardRef, useCallback, useContext} from 'react';+import React, {forwardRef, useCallback, useContext, useMemo} from 'react';import {getAbsoluteSrc} from '../absolute-src.js';import {calculateMediaDuration,@@ -19,7 +19,7 @@ import {validateMediaTrimProps,} from '../validate-start-from-props.js';import {validateDurationInFrames} from '../validation/validate-duration-in-frames.js';-import {DurationsContext} from '../video/duration-state.js';+import {DurationsContext, onMediaDuration} from '../video/duration-state.js';import {AudioForPreview} from './AudioForPreview.js';import {AudioForRendering} from './AudioForRendering.js';import type {RemotionAudioProps, RemotionMainAudioProps} from './props.js';@@ -105,10 +105,8 @@ const AudioRefForwardingFunction: React.ForwardRefRenderFunction<[loop, onRemotionError, preloadedSrc],);- const onDuration = useCallback(- (src: string, durationInSeconds: number) => {- setDurations({type: 'got-duration', durationInSeconds, src});- },+ const onDuration = useMemo(+ () => onMediaDuration.bind(null, setDurations),[setDurations],);diff --git a/packages/core/src/audio/shared-audio-tags.tsx b/packages/core/src/audio/shared-audio-tags.tsxindex bb94ef4862..2ffc02cacd 100644--- a/packages/core/src/audio/shared-audio-tags.tsx+++ b/packages/core/src/audio/shared-audio-tags.tsx@@ -865,6 +865,15 @@ export const SharedAudioTagsContextProvider: React.FC<{);};+const unregisterSharedAudio = (+ tagsContext: SharedAudioTagsContextValue | null,+ id: number,+) => {+ if (tagsContext && tagsContext.numberOfAudioTags > 0) {+ tagsContext.unregisterAudio(id);+ }+};+export const useSharedAudio = ({aud,audioId,@@ -933,11 +942,7 @@ export const useSharedAudio = ({}, [aud, tagsCtx, elem.id, audioId, premounting, postmounting]);effectToUse(() => {- return () => {- if (tagsCtx && tagsCtx.numberOfAudioTags > 0) {- tagsCtx.unregisterAudio(elem.id);- }- };+ return unregisterSharedAudio.bind(null, tagsCtx, elem.id);}, [tagsCtx, elem.id]);}diff --git a/packages/core/src/canvas-image/CanvasImage.tsx b/packages/core/src/canvas-image/CanvasImage.tsxindex f935eb90e3..d140ed18f8 100644--- a/packages/core/src/canvas-image/CanvasImage.tsx+++ b/packages/core/src/canvas-image/CanvasImage.tsx@@ -1,6 +1,5 @@import {forwardRef,- useCallback,useContext,useImperativeHandle,useLayoutEffect,@@ -80,6 +79,42 @@ type PendingLoadDelay = {continued: boolean;};+const continueImageLoadDelay = (+ pendingLoadDelayRef: React.MutableRefObject<PendingLoadDelay | null>,+ setIsLoadPending: React.Dispatch<React.SetStateAction<boolean>>,+ continueRender: ReturnType<typeof useDelayRender>['continueRender'],+ {markAsReady}: {readonly markAsReady: boolean},+) => {+ const pending = pendingLoadDelayRef.current;+ if (!pending || pending.continued) {+ return;+ }++ pending.continued = true;+ if (markAsReady) {+ setIsLoadPending(false);+ }++ continueRender(pending.handle);+ pendingLoadDelayRef.current = null;+};++const setCanvasRef = (+ setOutputCanvas: React.Dispatch<+ React.SetStateAction<HTMLCanvasElement | null>+ >,+ ref: React.ForwardedRef<HTMLCanvasElement>,+ canvas: HTMLCanvasElement | null,+) => {+ setOutputCanvas(canvas);++ if (typeof ref === 'function') {+ ref(canvas);+ } else if (ref) {+ ref.current = canvas;+ }+};+const makeAbortError = () => {if (typeof DOMException !== 'undefined') {return new DOMException('Image loading was aborted', 'AbortError');@@ -252,21 +287,15 @@ const CanvasImageContent = forwardRef<const isPremounting = Boolean(sequenceContext?.premounting);const isPostmounting = Boolean(sequenceContext?.postmounting);- const continuePendingLoadDelay = useCallback(- ({markAsReady}: {readonly markAsReady: boolean}) => {- const pending = pendingLoadDelayRef.current;- if (!pending || pending.continued) {- return;- }-- pending.continued = true;- if (markAsReady) {- setIsLoadPending(false);- }-- continueRender(pending.handle);- pendingLoadDelayRef.current = null;- },+ // Bound callbacks keep only these inputs, including after minification.+ const continuePendingLoadDelay = useMemo(+ () =>+ continueImageLoadDelay.bind(+ null,+ pendingLoadDelayRef,+ setIsLoadPending,+ continueRender,+ ),[continueRender],);@@ -278,16 +307,8 @@ const CanvasImageContent = forwardRef<return document.createElement('canvas');}, []);- const canvasRef = useCallback(- (canvas: HTMLCanvasElement | null) => {- setOutputCanvas(canvas);-- if (typeof ref === 'function') {- ref(canvas);- } else if (ref) {- ref.current = canvas;- }- },+ const canvasRef = useMemo(+ () => setCanvasRef.bind(null, setOutputCanvas, ref),[ref],);diff --git a/packages/core/src/composition-registry-fallback.ts b/packages/core/src/composition-registry-fallback.tsindex 24943c64f0..32c42e32b5 100644--- a/packages/core/src/composition-registry-fallback.ts+++ b/packages/core/src/composition-registry-fallback.ts@@ -12,14 +12,31 @@ export type CommittedCompositionEntry =| {readonly type: 'composition'; readonly value: AnyComposition}| {readonly type: 'folder'; readonly value: TFolder};-export const CompositionRegistryFallbackContext = createContext<{+type CompositionRegistryFallback = {readonly entries: Map<object, CommittedCompositionEntry>;readonly notifyCommit: () => void;-} | null>(null);+};++export const CompositionRegistryFallbackContext =+ createContext<CompositionRegistryFallback | null>(null);const useIsomorphicLayoutEffect =typeof window === 'undefined' ? useEffect : useLayoutEffect;+const createEntryCleanup = (+ registry: CompositionRegistryFallback | null,+ identity: object,+) => {+ if (registry === null) {+ return;+ }++ return () => {+ registry.entries.delete(identity);+ registry.notifyCommit();+ };+};+// The fallback stages descriptors during layout effects and publishes one// complete snapshot after the commit, including mount and unmount changes.export const useCommittedCompositionEntry = (@@ -40,15 +57,10 @@ export const useCommittedCompositionEntry = (registry.notifyCommit();}, [entry, registry]);- useIsomorphicLayoutEffect(() => {- if (registry === null) {- return;- }-- const identity = key.current;- return () => {- registry.entries.delete(identity);- registry.notifyCommit();- };- }, [registry]);+ // The cleanup outlives entry changes. Binding a separate factory prevents it+ // from sharing a closure with the original entry, even after minification.+ useIsomorphicLayoutEffect(+ createEntryCleanup.bind(null, registry, key.current),+ [registry],+ );};diff --git a/packages/core/src/use-lazy-component.ts b/packages/core/src/use-lazy-component.tsindex 0eeaf68d91..b231bff0e5 100644--- a/packages/core/src/use-lazy-component.ts+++ b/packages/core/src/use-lazy-component.ts@@ -5,7 +5,7 @@ import type {} from 'react';import React, {useEffect, useLayoutEffect, useMemo} from 'react';import type {CompProps} from './Composition.js';-import {createRegistryStore} from './registry-store.js';+import {createRegistryStore, type RegistryStore} from './registry-store.js';import {useSyncExternalStore} from './use-sync-external-store.js';const useIsomorphicLayoutEffect =@@ -17,6 +17,21 @@ type LazyExoticComponent<T extends ComponentType<any>> = ExoticComponent<readonly _result: T;};+const createComponentWrapper = <Props>(+ componentStore: RegistryStore<ComponentType<Props> | null>,+): ComponentType<Props> => {+ const Wrapper = (props: Props) => {+ const Comp = useSyncExternalStore(+ componentStore.subscribe,+ componentStore.getSnapshot,+ componentStore.getSnapshot,+ )!;+ return React.createElement(Comp as React.ComponentType<{}>, props as {});+ };++ return Wrapper;+};+// Expected, it can be any component propsexport const useLazyComponent = <Props>({compProps,@@ -75,6 +90,13 @@ export const useLazyComponent = <Props>({componentStore.setSnapshot(() => componentFromProps);}}, [componentFromProps, componentStore]);+ // Passing a bound factory keeps its closure separate from the render, including+ // after minification. The wrapper must only retain the store, not initial props.+ const createWrapper = createComponentWrapper<Props>;+ // eslint-disable-next-line react-hooks/exhaustive-deps+ const Wrapper = useMemo(createWrapper.bind(null, componentStore), [+ componentStore,+ ]);const lazy = useMemo(() => {if ('component' in compProps) {@@ -85,19 +107,7 @@ export const useLazyComponent = <Props>({>;}- const Wrapper = (props: Props) => {- const Comp = useSyncExternalStore(- componentStore.subscribe,- componentStore.getSnapshot,- componentStore.getSnapshot,- )!;- return React.createElement(- Comp as React.ComponentType<{}>,- props as {},- );- };-- return Wrapper as ComponentType<Props>;+ return Wrapper;}if (@@ -125,6 +135,6 @@ export const useLazyComponent = <Props>({// compProps.component β the stable wrapper subscribes to componentStore instead.// eslint-disable-next-line react-hooks/exhaustive-deps- }, [lazyComponentFromProps, componentStore]);+ }, [lazyComponentFromProps, componentStore, Wrapper]);return lazy;};diff --git a/packages/core/src/use-media-duration.ts b/packages/core/src/use-media-duration.tsnew file mode 100644index 0000000000..53ebe5849f--- /dev/null+++ b/packages/core/src/use-media-duration.ts@@ -0,0 +1,55 @@+import {useEffect, useRef} from 'react';++type OnDuration = (src: string, durationInSeconds: number) => void;++const subscribeToMediaDuration = (+ mediaRef: React.RefObject<HTMLMediaElement | null>,+ onDurationRef: React.RefObject<OnDuration>,+ src: string | null,+) => {+ const {current} = mediaRef;+ if (!current) {+ return;+ }++ if (current.duration) {+ onDurationRef.current?.(src ?? current.src, current.duration);+ return;+ }++ const onLoadedMetadata = () => {+ onDurationRef.current?.(src ?? current.src, current.duration);+ };++ current.addEventListener('loadedmetadata', onLoadedMetadata);+ return () => {+ current.removeEventListener('loadedmetadata', onLoadedMetadata);+ };+};++export const useMediaDuration = (+ mediaRef: React.RefObject<HTMLMediaElement | null>,+ onDuration: OnDuration,+ src: string,+ useCurrentSrc: boolean,+) => {+ const onDurationRef = useRef(onDuration);+ onDurationRef.current = onDuration;++ // The listener and its cleanup must not share the component render scope.+ // Binding prevents production minifiers from inlining that scope back in.+ // eslint-disable-next-line react-hooks/exhaustive-deps+ useEffect(+ subscribeToMediaDuration.bind(+ null,+ mediaRef,+ onDurationRef,+ useCurrentSrc ? null : src,+ ),+ [mediaRef, src, useCurrentSrc],+ );+};++export const scheduleMediaSourceCleanup = (cleanup: (() => void) | null) => {+ requestAnimationFrame(() => cleanup?.());+};diff --git a/packages/core/src/video/VideoForPreview.tsx b/packages/core/src/video/VideoForPreview.tsxindex cb8fdd765e..5c44815c0a 100644--- a/packages/core/src/video/VideoForPreview.tsx+++ b/packages/core/src/video/VideoForPreview.tsx@@ -24,6 +24,10 @@ import {usePreload} from '../prefetch.js';import {SequenceContent} from '../sequence-activity-context.js';import {SequenceContext} from '../SequenceContext.js';import {useVolume} from '../use-amplification.js';+import {+ scheduleMediaSourceCleanup,+ useMediaDuration,+} from '../use-media-duration.js';import {useMediaInTimelineRegistration} from '../use-media-in-timeline.js';import {useMediaPlayback} from '../use-media-playback.js';import {useMediaTag} from '../use-media-tag.js';@@ -93,11 +97,7 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<// But useInsertionEffect will fire before other effects, meaning the// nodes might still be used. Using rAF to ensure it's after other effects.effectToUse(() => {- return () => {- requestAnimationFrame(() => {- sharedSource?.cleanup();- });- };+ return scheduleMediaSourceCleanup.bind(null, sharedSource?.cleanup ?? null);}, [sharedSource]);const {@@ -264,32 +264,9 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<};}, [onError, src]);- const currentOnDurationCallback =- useRef<VideoForPreviewProps['onDuration']>(onDuration);- currentOnDurationCallback.current = onDuration;-useEmitVideoFrame({ref: videoRef, onVideoFrame});- useEffect(() => {- const {current} = videoRef;- if (!current) {- return;- }-- if (current.duration) {- currentOnDurationCallback.current?.(src as string, current.duration);- return;- }-- const onLoadedMetadata = () => {- currentOnDurationCallback.current?.(src as string, current.duration);- };-- current.addEventListener('loadedmetadata', onLoadedMetadata);- return () => {- current.removeEventListener('loadedmetadata', onLoadedMetadata);- };- }, [src]);+ useMediaDuration(videoRef, onDuration, src as string, false);useEffect(() => {const {current} = videoRef;diff --git a/packages/core/src/video/duration-state.tsx b/packages/core/src/video/duration-state.tsxindex 5c7736b942..97469bea04 100644--- a/packages/core/src/video/duration-state.tsx+++ b/packages/core/src/video/duration-state.tsx@@ -35,6 +35,16 @@ type TDurationsContext = {setDurations: React.Dispatch<DurationAction>;};+// Bind this function outside component render scopes so the stable duration+// callback cannot keep unrelated, replaced media props alive.+export const onMediaDuration = (+ setDurations: TDurationsContext['setDurations'],+ src: string,+ durationInSeconds: number,+) => {+ setDurations({type: 'got-duration', durationInSeconds, src});+};+export const DurationsContext = createContext<TDurationsContext>({durations: {},setDurations: () => {diff --git a/packages/core/src/video/html5-video.tsx b/packages/core/src/video/html5-video.tsxindex 99bde3f79c..f11f2404fe 100644--- a/packages/core/src/video/html5-video.tsx+++ b/packages/core/src/video/html5-video.tsx@@ -1,5 +1,5 @@/* eslint-disable @typescript-eslint/no-use-before-define */-import React, {forwardRef, useCallback, useContext} from 'react';+import React, {forwardRef, useContext, useMemo} from 'react';import {getAbsoluteSrc} from '../absolute-src.js';import {Html5MediaTrimContext} from '../audio/use-audio-frame.js';import {@@ -19,7 +19,7 @@ import {validateMediaTrimProps,} from '../validate-start-from-props.js';import {validateDurationInFrames} from '../validation/validate-duration-in-frames.js';-import {DurationsContext} from './duration-state.js';+import {DurationsContext, onMediaDuration} from './duration-state.js';import type {RemotionMainVideoProps, RemotionVideoProps} from './props';import {VideoForPreview} from './VideoForPreview.js';import {VideoForRendering} from './VideoForRendering.js';@@ -76,10 +76,8 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<const preloadedSrc = usePreload(props.src);- const onDuration = useCallback(- (src: string, durationInSeconds: number) => {- setDurations({type: 'got-duration', durationInSeconds, src});- },+ const onDuration = useMemo(+ () => onMediaDuration.bind(null, setDurations),[setDurations],);diff --git a/packages/studio/src/components/Timeline/TimelineExpandArrowButton.tsx b/packages/studio/src/components/Timeline/TimelineExpandArrowButton.tsxindex 64ef5f30ce..e65a4365f3 100644--- a/packages/studio/src/components/Timeline/TimelineExpandArrowButton.tsx+++ b/packages/studio/src/components/Timeline/TimelineExpandArrowButton.tsx@@ -25,6 +25,14 @@ const arrowSpacer: React.CSSProperties = {...arrowButton,};+const stopPropagation = (+ e:+ | React.MouseEvent<HTMLButtonElement>+ | React.PointerEvent<HTMLButtonElement>,+) => {+ e.stopPropagation();+};+export const TimelineExpandArrowButton: React.FC<{readonly isExpanded: boolean;readonly onClick: () => void;@@ -39,17 +47,6 @@ export const TimelineExpandArrowButton: React.FC<{[onClick],);- const stopPropagation = React.useCallback(- (- e:- | React.MouseEvent<HTMLButtonElement>- | React.PointerEvent<HTMLButtonElement>,- ) => {- e.stopPropagation();- },- [],- );-const style: React.CSSProperties = useMemo(() => {return {...arrowButton,diff --git a/packages/studio/src/components/Timeline/TimelineLayerEye.tsx b/packages/studio/src/components/Timeline/TimelineLayerEye.tsxindex 237a680c67..e9cf265dd6 100644--- a/packages/studio/src/components/Timeline/TimelineLayerEye.tsx+++ b/packages/studio/src/components/Timeline/TimelineLayerEye.tsx@@ -6,7 +6,6 @@ import {WHITE,} from '../../helpers/colors';import {startCapturedPointerSession} from '../../helpers/pointer-session';-import type {RenderInlineAction} from '../InlineAction';const eyeIcon: React.CSSProperties = {width: 12,@@ -43,51 +42,61 @@ let lastPaintedLayerEye: Element | null = null;const layerEyeActions = new WeakMap<Element, () => void>();const TIMELINE_LAYER_EYE_ATTR = 'data-timeline-layer-eye';-export const TimelineLayerEye: React.FC<{- readonly onInvoked: ((type: 'enable' | 'disable') => void) | null;- readonly hidden: boolean;- readonly type: 'eye' | 'speaker' | 'effect';-}> = ({onInvoked, hidden, type}) => {- const renderAction: RenderInlineAction = useCallback(- (color) => {- if (hidden) {- return null;- }+const onDragStart: React.DragEventHandler<HTMLDivElement> = (e) => {+ e.preventDefault();+ e.stopPropagation();+};- if (type === 'speaker') {- return (- <svg viewBox="0 0 10 14" fill="none" style={speakerIcon}>- <path- d="M9.40938 0.0869018C9.76875 0.249402 10 0.605652 10 0.999402V12.9994C10 13.3932 9.76875 13.7494 9.40938 13.9119C9.05 14.0744 8.62813 14.0088 8.33438 13.7463L4.11875 9.9994H2C0.896875 9.9994 0 9.10253 0 7.9994V5.9994C0 4.89628 0.896875 3.9994 2 3.9994H4.11875L8.33438 0.252527C8.62813 -0.0099732 9.05 -0.0724732 9.40938 0.0869018Z"- fill={color}- />- </svg>- );- }+const stopClickPropagation: React.MouseEventHandler<HTMLDivElement> = (e) => {+ e.stopPropagation();+};- if (type === 'effect') {- return (- <svg viewBox="0 0 16 16" fill="none" style={effectIcon}>- <path- d="M4.405 4.48C4.575 3.82 4.865 3.325 5.275 2.995C5.695 2.665 6.25 2.5 6.94 2.5H9.235V4.06H7.045C6.555 4.06 6.235 4.3 6.085 4.78L5.83 5.68H7.975V7.255H5.395L3.805 13H2.02L3.625 7.255H1.96V5.68H4.075L4.405 4.48ZM8.57102 9.085L6.87602 5.68H8.79602L9.86102 7.99L11.991 5.68H14.331L10.686 9.415L12.426 13H10.491L9.35102 10.585L7.02602 13H4.68602L8.57102 9.085Z"- fill={WHITE}- />- </svg>- );- }+const renderLayerEyeIcon = (+ hidden: boolean,+ type: 'eye' | 'speaker' | 'effect',+ color: string,+) => {+ if (hidden) {+ return null;+ }++ if (type === 'speaker') {+ return (+ <svg viewBox="0 0 10 14" fill="none" style={speakerIcon}>+ <path+ d="M9.40938 0.0869018C9.76875 0.249402 10 0.605652 10 0.999402V12.9994C10 13.3932 9.76875 13.7494 9.40938 13.9119C9.05 14.0744 8.62813 14.0088 8.33438 13.7463L4.11875 9.9994H2C0.896875 9.9994 0 9.10253 0 7.9994V5.9994C0 4.89628 0.896875 3.9994 2 3.9994H4.11875L8.33438 0.252527C8.62813 -0.0099732 9.05 -0.0724732 9.40938 0.0869018Z"+ fill={color}+ />+ </svg>+ );+ }++ if (type === 'effect') {+ return (+ <svg viewBox="0 0 16 16" fill="none" style={effectIcon}>+ <path+ d="M4.405 4.48C4.575 3.82 4.865 3.325 5.275 2.995C5.695 2.665 6.25 2.5 6.94 2.5H9.235V4.06H7.045C6.555 4.06 6.235 4.3 6.085 4.78L5.83 5.68H7.975V7.255H5.395L3.805 13H2.02L3.625 7.255H1.96V5.68H4.075L4.405 4.48ZM8.57102 9.085L6.87602 5.68H8.79602L9.86102 7.99L11.991 5.68H14.331L10.686 9.415L12.426 13H10.491L9.35102 10.585L7.02602 13H4.68602L8.57102 9.085Z"+ fill={WHITE}+ />+ </svg>+ );+ }- return (- <svg style={eyeIcon} viewBox="0 0 24 16" fill="none">- <path- d="M24 7.551C24 7.551 19.748 16 12.015 16C4.835 16 0 7.551 0 7.551C0 7.551 4.446 0 12.015 0C19.709 0 24 7.551 24 7.551ZM17 8C17 5.243 14.757 3 12 3C9.243 3 7 5.243 7 8C7 10.757 9.243 13 12 13C14.757 13 17 10.757 17 8Z"- fill={color}- />- </svg>- );- },- [hidden, type],+ return (+ <svg style={eyeIcon} viewBox="0 0 24 16" fill="none">+ <path+ d="M24 7.551C24 7.551 19.748 16 12.015 16C4.835 16 0 7.551 0 7.551C0 7.551 4.446 0 12.015 0C19.709 0 24 7.551 24 7.551ZM17 8C17 5.243 14.757 3 12 3C9.243 3 7 5.243 7 8C7 10.757 9.243 13 12 13C14.757 13 17 10.757 17 8Z"+ fill={color}+ />+ </svg>);+};+export const TimelineLayerEye: React.FC<{+ readonly onInvoked: ((type: 'enable' | 'disable') => void) | null;+ readonly hidden: boolean;+ readonly type: 'eye' | 'speaker' | 'effect';+}> = ({onInvoked, hidden, type}) => {const onPointerDown: React.PointerEventHandler<HTMLDivElement> = useCallback((e) => {if (e.button !== 0) {@@ -128,14 +137,6 @@ export const TimelineLayerEye: React.FC<{[hidden, onInvoked],);- const onDragStart: React.DragEventHandler<HTMLDivElement> = useCallback(- (e) => {- e.preventDefault();- e.stopPropagation();- },- [],- );-const onPointerEnter = useCallback(() => {if (layerPointedDown && onInvoked !== null) {onInvoked(layerPointedDown);@@ -155,11 +156,6 @@ export const TimelineLayerEye: React.FC<{[onInvoked],);- const stopClickPropagation: React.MouseEventHandler<HTMLDivElement> =- useCallback((e) => {- e.stopPropagation();- }, []);-return (<divref={registerLayerEye}@@ -172,7 +168,7 @@ export const TimelineLayerEye: React.FC<{onPointerEnter={onInvoked === null ? undefined : onPointerEnter}onPointerDown={onPointerDown}>- {renderAction(LIGHT_COLOR)}+ {renderLayerEyeIcon(hidden, type, LIGHT_COLOR)}</div>);};