feat: implement image proxy for YouTube CDN images with caching to prevent rate limits

This commit is contained in:
Yusuf İpek
2025-10-11 20:34:25 +03:00
parent edd6c4416f
commit 7b43932fa6
7 changed files with 149 additions and 13 deletions
+27
View File
@@ -0,0 +1,27 @@
const BACKEND_URL = process.env.NEXT_PUBLIC_BACKEND_URL ?? 'http://localhost:4100';
/**
* Converts a YouTube CDN image URL to use our backend proxy
* This prevents 429 rate limit errors from YouTube's CDN
*/
export function proxyImageUrl(url: string | undefined): string | undefined {
if (!url) return undefined;
// Check if it's a YouTube CDN URL
const youtubeCdnDomains = ['yt3.ggpht.com', 'yt4.ggpht.com', 'i.ytimg.com'];
try {
const urlObj = new URL(url);
if (youtubeCdnDomains.includes(urlObj.hostname)) {
// Proxy through our backend
return `${BACKEND_URL}/proxy/image?url=${encodeURIComponent(url)}`;
}
} catch {
// If URL parsing fails, return as-is
return url;
}
// Return non-YouTube URLs as-is
return url;
}