🎥 Make videos programmatically with React
`@remotion/gif`: Reuse cached GIF instead of re-decoding on every remount (#8672)
Neil Volungis1630ec51e9df
diff --git a/packages/gif/src/GifForDevelopment.tsx b/packages/gif/src/GifForDevelopment.tsxindex 381373a20c..96a0f3e70b 100644--- a/packages/gif/src/GifForDevelopment.tsx+++ b/packages/gif/src/GifForDevelopment.tsx@@ -61,6 +61,14 @@ export const GifForDevelopment = forwardRef<currentOnError.current = onError;useEffect(() => {+ const parsedGif =+ volatileGifCache.get(cacheKey) ?? manuallyManagedGifCache.get(cacheKey);+ if (parsedGif !== undefined) {+ update(parsedGif as GifState);+ currentOnLoad.current?.(parsedGif as GifState);+ return;+ }+let done = false;let aborted = false;const {prom, cancel} = parseWithWorker({diff --git a/packages/gif/src/test/Gif.test.tsx b/packages/gif/src/test/Gif.test.tsxindex faf3a64162..224ae0f489 100644--- a/packages/gif/src/test/Gif.test.tsx+++ b/packages/gif/src/test/Gif.test.tsx@@ -5,12 +5,19 @@ import {Internals} from 'remotion';import {Gif} from '../Gif';import {manuallyManagedGifCache} from '../gif-cache';import type {GifState} from '../props';+import {getGifCacheKey} from '../request-init';+import {resolveGifSource} from '../resolve-gif-source';type RegisteredSequence = {readonly refForOutline: React.RefObject<HTMLElement | null> | null;};class MockWorker {+ public static instances = 0;+ public constructor() {+ MockWorker.instances++;+ }+public addEventListener = () => undefined;public removeEventListener = () => undefined;public postMessage = () => undefined;@@ -200,3 +207,28 @@ test('<Gif> registers its canvas as the outline ref', async () => {.refForOutline as React.RefObject<HTMLCanvasElement | null>;expect(ref.current).toBe(refForOutline.current);});++test('reuses a cached GIF without spawning a decode worker', async () => {+ // The GIF is already decoded and cached, so mounting must reuse it instead of+ // re-spawning the parse worker. Without this, every remount re-decodes the GIF+ // (a fresh ImageData per frame); repeated remounts while scrubbing accumulate+ // memory until the tab runs out of memory.+ const cacheKey = getGifCacheKey({+ resolvedSrc: resolveGifSource('test.gif'),+ requestInit: undefined,+ });+ manuallyManagedGifCache.set(cacheKey, gifState);+ MockWorker.instances = 0;++ render(+ <SequenceRegistrationWrapper onRegisterSequence={() => undefined}>+ <Gif src="test.gif" />+ </SequenceRegistrationWrapper>,+ );++ await waitFor(() => {+ expect(document.querySelector('canvas')).toBeInstanceOf(HTMLCanvasElement);+ });++ expect(MockWorker.instances).toBe(0);+});