Skip to content

🎥 Make videos programmatically with React

fail hard if a GIF cannot be rendered due to CORS

Jonny Burgere4ebe84221bc
diff --git a/packages/gif/src/GifForRendering.tsx b/packages/gif/src/GifForRendering.tsx
index 904d474190..def96df1ef 100644
--- a/packages/gif/src/GifForRendering.tsx
+++ b/packages/gif/src/GifForRendering.tsx
@@ -1,6 +1,7 @@
import {Canvas, useParser} from '@react-gifs/tools';
import {forwardRef, useState} from 'react';
import {continueRender, delayRender} from 'remotion';
+import {isCorsError} from './is-cors-error';
import type {GifState, RemotionGifProps} from './props';
import {useCurrentGifIndex} from './useCurrentGifIndex';
@@ -13,6 +14,7 @@ export const GifForRendering = forwardRef<HTMLCanvasElement, RemotionGifProps>(
width: 0,
height: 0,
});
+ const [error, setError] = useState<Error | null>(null);
const [id] = useState(() =>
delayRender(`Rendering <Gif/> with src="${resolvedSrc}"`)
@@ -25,11 +27,7 @@ export const GifForRendering = forwardRef<HTMLCanvasElement, RemotionGifProps>(
if (onError) {
onError(info.error);
} else {
- console.error(
- 'Error loading GIF:',
- info.error,
- 'Handle the event using the onError() prop to make this message disappear.'
- );
+ setError(info.error);
}
} else {
onLoad?.(info);
@@ -39,6 +37,19 @@ export const GifForRendering = forwardRef<HTMLCanvasElement, RemotionGifProps>(
continueRender(id);
});
+ if (error) {
+ console.error(error.stack);
+ if (isCorsError(error)) {
+ throw new Error(
+ `Failed to render GIF with source ${src}: "${error.message}". You must enable CORS for this URL.`
+ );
+ }
+
+ throw new Error(
+ `Failed to render GIF with source ${src}: "${error.message}". Render with --log=verbose to see the full stack.`
+ );
+ }
+
return (
<Canvas
fit={fit}
diff --git a/packages/gif/src/is-cors-error.ts b/packages/gif/src/is-cors-error.ts
new file mode 100644
index 0000000000..0503cdbd5e
--- /dev/null
+++ b/packages/gif/src/is-cors-error.ts
@@ -0,0 +1,10 @@
+export const isCorsError = (error: Error) => {
+ return (
+ // Chrome
+ error.message.includes('Failed to fetch') ||
+ // Safari
+ error.message.includes('Load failed') ||
+ // Firefox
+ error.message.includes('NetworkError when attempting to fetch resource')
+ );
+};