diff --git a/src/components/metadata/HeroSection.tsx b/src/components/metadata/HeroSection.tsx index 32743cc9..37fd01e2 100644 --- a/src/components/metadata/HeroSection.tsx +++ b/src/components/metadata/HeroSection.tsx @@ -1144,6 +1144,7 @@ const HeroSection: React.FC = memo(({ setTrailerError(false); setTrailerReady(false); setTrailerPreloaded(false); + startedOnReadyRef.current = false; // Small delay to avoid blocking the UI render timerId = setTimeout(async () => { diff --git a/src/components/metadata/TrailerModal.tsx b/src/components/metadata/TrailerModal.tsx index b49428e4..a0d1eeae 100644 --- a/src/components/metadata/TrailerModal.tsx +++ b/src/components/metadata/TrailerModal.tsx @@ -159,28 +159,19 @@ const TrailerModal: React.FC = memo(({ const handleVideoError = useCallback((error: any) => { logger.error('TrailerModal', 'Video error:', error); - const errorCode = error?.error?.code; - const isRetryableError = errorCode === -1102 || errorCode === -1009 || errorCode === -1005; - - if (isRetryableError && retryCount < 2) { - logger.info('TrailerModal', `Retrying video load (attempt ${retryCount + 1}/2)`); + if (retryCount < 2) { + logger.info('TrailerModal', `Re-extracting trailer (attempt ${retryCount + 1}/2)`); setRetryCount(prev => prev + 1); - - // Capture current URL before clearing it - setTrailerUrl(current => { - const urlToRestore = current; - setTimeout(() => { - setTrailerUrl(urlToRestore); - }, 500); - return null; // Clear first to force remount - }); + // Invalidate cache so loadTrailer gets a fresh URL, not the same bad one + if (trailer?.key) TrailerService.invalidateCache(trailer.key); + loadTrailer(); return; } - logger.error('TrailerModal', 'Video error after retries or non-retryable:', error); + logger.error('TrailerModal', 'Video error after retries:', error); setError('Unable to play trailer. Please try again.'); setLoading(false); - }, [retryCount]); + }, [retryCount, loadTrailer, trailer?.key]); const handleTrailerEnd = useCallback(() => { setIsPlaying(false); diff --git a/src/services/trailerService.ts b/src/services/trailerService.ts index 0bb11cae..f2fa5492 100644 --- a/src/services/trailerService.ts +++ b/src/services/trailerService.ts @@ -14,8 +14,8 @@ interface CacheEntry { } export class TrailerService { - // Cache for 3 minutes — just enough to avoid re-extracting on quick re-renders - private static readonly CACHE_TTL_MS = 30 * 1000; + // Cache for 5 seconds — just enough to avoid re-extracting on quick re-renders + private static readonly CACHE_TTL_MS = 5 * 1000; private static urlCache = new Map(); // --------------------------------------------------------------------------- @@ -114,6 +114,11 @@ export class TrailerService { return { url, title, year }; } + static invalidateCache(videoId: string): void { + this.urlCache.delete(videoId); + logger.info('TrailerService', `Cache invalidated for videoId=${videoId}`); + } + static setUseLocalServer(_useLocal: boolean): void {} static getServerStatus(): { usingLocal: boolean; localUrl: string } {