diff --git a/app/components/YouTubeEmbed.tsx b/app/components/YouTubeEmbed.tsx index 1294447c0..9841b8ee3 100644 --- a/app/components/YouTubeEmbed.tsx +++ b/app/components/YouTubeEmbed.tsx @@ -64,19 +64,6 @@ export function YouTubeEmbed({ useEffect(() => { if (!enableApi || !isApiReady || !containerRef.current) return; - if (playerRef.current) { - try { - playerRef.current.loadVideoById({ videoId: id, startSeconds: start }); - } catch { - toastQueue.add({ - message: t("vods:errors.youtubePreviewFailed"), - variant: "error", - }); - setHasError(true); - } - return; - } - try { const player = new window.YT.Player(containerRef.current, { height: "281", @@ -103,12 +90,18 @@ export function YouTubeEmbed({ }, }, }); + + return () => { + player.destroy(); + playerRef.current = null; + }; } catch { toastQueue.add({ message: t("vods:errors.youtubePreviewFailed"), variant: "error", }); setHasError(true); + return; } }, [enableApi, isApiReady, id, start, autoplay, onPlayerReady, t]); diff --git a/types/youtube.d.ts b/types/youtube.d.ts index 4bcef598b..bcb316ffd 100644 --- a/types/youtube.d.ts +++ b/types/youtube.d.ts @@ -26,5 +26,6 @@ declare namespace YT { ); loadVideoById(options: { videoId: string; startSeconds?: number }): void; getCurrentTime(): number; + destroy(): void; } }