From 379fc1bb77c882352cd95e8b6a3ff058cf39723e Mon Sep 17 00:00:00 2001 From: parsa aghaei Date: Wed, 2 Sep 2026 16:22:16 +0330 Subject: [PATCH] fix(images): route Next.js Image Optimization to remote backend and eliminate hydration mismatch --- frontend/application/components/Hero.tsx | 17 ++++---- frontend/application/lib/media.ts | 50 ++++++++++++------------ frontend/application/next.config.ts | 39 ++++++++++++++++++ 3 files changed, 73 insertions(+), 33 deletions(-) diff --git a/frontend/application/components/Hero.tsx b/frontend/application/components/Hero.tsx index 9fa76ce9..8fe26c63 100644 --- a/frontend/application/components/Hero.tsx +++ b/frontend/application/components/Hero.tsx @@ -120,17 +120,18 @@ export default function Hero({ banners = [], onShopNavigate }: { banners?: Banne
- + {mounted && isInitialized ? getText('hero_badge', "از ۱۹۸۴ در خدمت سلامت حیوانات") : "از ۱۹۸۴ در خدمت سلامت حیوانات"}
-

+

{titleParts[0]}
{titleParts[1] && {titleParts[1]}}

-
+
{mounted && isInitialized ? getText('hero_image_badge', "سرآمد علمی در پزشکی پت‌ها") : "سرآمد علمی در پزشکی پت‌ها"}
-
+
{activeBanner?.title || (mounted && isInitialized ? getText('hero_image_title', "مکمل‌های تایید شده دامپزشکی با گواهی IFS") : "مکمل‌های تایید شده دامپزشکی با گواهی IFS")}
{activeBanner?.linkUrl && ( @@ -283,7 +284,7 @@ export default function Hero({ banners = [], onShopNavigate }: { banners?: Banne >
DE - + {mounted && isInitialized ? getText('hero_quality_standard', "استاندارد کیفی آلمان") : "استاندارد کیفی آلمان"}
@@ -314,7 +315,7 @@ export default function Hero({ banners = [], onShopNavigate }: { banners?: Banne
۱۹۸۴
-
+
{mounted && isInitialized ? getText('hero_stat_founded', "سال تاسیس برند") : "سال تاسیس برند"}
@@ -329,7 +330,7 @@ export default function Hero({ banners = [], onShopNavigate }: { banners?: Banne
-
+
{mounted && isInitialized ? getText('hero_stat_agencies', "نمایندگی فعال") : "نمایندگی فعال"}
@@ -342,7 +343,7 @@ export default function Hero({ banners = [], onShopNavigate }: { banners?: Banne
۱۰۰٪
-
+
{mounted && isInitialized ? getText('hero_stat_german_formula', "فرمول آلمانی") : "فرمول آلمانی"}
diff --git a/frontend/application/lib/media.ts b/frontend/application/lib/media.ts index 33c3e92b..76dc5b6a 100644 --- a/frontend/application/lib/media.ts +++ b/frontend/application/lib/media.ts @@ -17,33 +17,33 @@ export function getMediaUrl(url?: string | null): string { return trimmed; } - // If already proxied via /api/uploads/ or /api/media/ - if (trimmed.startsWith('/api/uploads/') || trimmed.startsWith('/api/media/')) { - return trimmed; + // Extract the clean upload path + let uploadPath = ''; + if (trimmed.startsWith('/api/uploads/')) { + uploadPath = trimmed.replace(/^\/api\/uploads\//, ''); + } else if (trimmed.startsWith('/api/media/')) { + uploadPath = trimmed.replace(/^\/api\/media\//, ''); + } else if (trimmed.startsWith('/uploads/')) { + uploadPath = trimmed.replace(/^\/uploads\//, ''); + } else if (trimmed.startsWith('uploads/')) { + uploadPath = trimmed.replace(/^uploads\//, ''); + } else if (trimmed.startsWith('/media/')) { + uploadPath = trimmed.replace(/^\/media\//, ''); + } else if (trimmed.startsWith('media/')) { + uploadPath = trimmed.replace(/^media\//, ''); + } else { + const uploadsMatch = trimmed.match(/^https?:\/\/[^/]+\/uploads\/(.+)$/i); + if (uploadsMatch && uploadsMatch[1]) { + uploadPath = uploadsMatch[1]; + } } - // If relative path starting with /uploads/ - if (trimmed.startsWith('/uploads/')) { - return `/api/uploads/${trimmed.replace(/^\/uploads\//, '')}`; - } - - // If relative path starting with uploads/ - if (trimmed.startsWith('uploads/')) { - return `/api/uploads/${trimmed.replace(/^uploads\//, '')}`; - } - - // If relative path starting with /media/ or media/ - if (trimmed.startsWith('/media/')) { - return `/api/uploads/${trimmed.replace(/^\/media\//, '')}`; - } - if (trimmed.startsWith('media/')) { - return `/api/uploads/${trimmed.replace(/^media\//, '')}`; - } - - // If full backend URL containing /uploads/ (e.g. http://localhost:4001/uploads/..., https://api.canina.ir/uploads/...) - const uploadsMatch = trimmed.match(/^https?:\/\/[^/]+\/uploads\/(.+)$/i); - if (uploadsMatch && uploadsMatch[1]) { - return `/api/uploads/${uploadsMatch[1]}`; + if (uploadPath) { + const isStage = typeof window !== 'undefined' && window.location.hostname.includes('stage'); + const isDev = process.env.NODE_ENV === 'development'; + const backendBase = process.env.NEXT_PUBLIC_API_URL?.replace(/\/api\/?$/, '') || + (isDev ? 'http://localhost:4400' : isStage ? 'https://stageapi.canina.ir' : 'https://api.canina.ir'); + return `${backendBase}/uploads/${uploadPath}`; } // External URLs (like unsplash, cdn, youtube, aparat) stay as-is diff --git a/frontend/application/next.config.ts b/frontend/application/next.config.ts index 2a46f159..dcd2e700 100644 --- a/frontend/application/next.config.ts +++ b/frontend/application/next.config.ts @@ -29,6 +29,11 @@ const nextConfig: NextConfig = { hostname: 'api.canina.ir', pathname: '/**', }, + { + protocol: 'http', + hostname: 'api.canina.ir', + pathname: '/**', + }, { protocol: 'https', hostname: 'stage.canina.ir', @@ -39,11 +44,21 @@ const nextConfig: NextConfig = { hostname: 'stageapi.canina.ir', pathname: '/**', }, + { + protocol: 'http', + hostname: 'stageapi.canina.ir', + pathname: '/**', + }, { protocol: 'https', hostname: 'apicanina.parsaaghayi.ir', pathname: '/**', }, + { + protocol: 'http', + hostname: 'apicanina.parsaaghayi.ir', + pathname: '/**', + }, { protocol: 'https', hostname: 'canina.de', @@ -67,6 +82,30 @@ const nextConfig: NextConfig = { { protocol: 'http', hostname: 'localhost', + port: '4400', + pathname: '/**', + }, + { + protocol: 'http', + hostname: 'localhost', + port: '4001', + pathname: '/**', + }, + { + protocol: 'http', + hostname: 'localhost', + pathname: '/**', + }, + { + protocol: 'http', + hostname: '127.0.0.1', + port: '4400', + pathname: '/**', + }, + { + protocol: 'http', + hostname: '127.0.0.1', + port: '4001', pathname: '/**', }, {