fix(media): resolve product images to relative /uploads/ path and strip loopback addresses

This commit is contained in:
iFixKart Dev 2026-11-07 13:21:05 +05:30
parent 7db6748ffb
commit 148ebceae4
3 changed files with 16 additions and 8 deletions

View file

@ -9,6 +9,7 @@ import { FloatingButtons } from '@/components/FloatingButtons';
import { StickyHeaderSpacer } from '@/components/StickyHeaderSpacer';
import { catalogServerService } from '@/services/api/catalogServerService';
import { getImageUrl } from '@/lib/utils';
import { ProductInteractiveGrid } from '@/components/pdp/ProductInteractiveGrid';
@ -392,9 +393,7 @@ export default async function ProductDetailPage({ params }: ProductDetailPagePro
const primaryVariant = product.variants?.[0];
const primaryImage = product.images?.[0]?.image_url || '/images/placeholder-product.webp';
const resolvedImageUrl = primaryImage.startsWith('http')
? primaryImage
: `https://ifixkartbe.trionixsolution.com/uploads/${primaryImage.replace(/^\//, '')}`;
const resolvedImageUrl = getImageUrl(primaryImage);
const jsonLd = {
'@context': 'https://schema.org/',

View file

@ -112,6 +112,11 @@ export function getImageUrl(path?: string | null | unknown): string {
trimmed.startsWith('data:') ||
trimmed.startsWith('blob:')
) {
// Strip loopback addresses like http://127.0.0.1:8010 or http://localhost:8000
if (trimmed.includes('127.0.0.1') || trimmed.includes('localhost')) {
const pathOnly = trimmed.replace(/^https?:\/\/(127\.0\.0\.1|localhost)(:\d+)?/, '');
return pathOnly.startsWith('/uploads/') ? pathOnly : `/uploads${pathOnly.startsWith('/') ? pathOnly : `/${pathOnly}`}`;
}
return trimmed;
}
@ -136,12 +141,15 @@ export function getImageUrl(path?: string | null | unknown): string {
cleanPath = `/${withoutLeadingSlash}`;
} else if (withoutLeadingSlash.startsWith('product/')) {
cleanPath = `/uploads/${withoutLeadingSlash}`;
} else {
cleanPath = `/uploads/${withoutLeadingSlash}`;
}
}
const backendUrl =
process.env.NEXT_PUBLIC_API_URL || 'https://ifixkartbe.trionixsolution.com';
return `${backendUrl.replace(/\/$/, '')}${cleanPath}`;
// Always return relative /uploads/... so that:
// 1. In production (https://ifixkart.trionixsolution.com), Nginx serves /uploads/ directly from disk with SSL & caching.
// 2. In local dev, Next.js rewrites proxy /uploads/ transparently to https://ifixkartbe.trionixsolution.com/uploads/.
// 3. No loopback IP (127.0.0.1) or mixed-content (HTTP on HTTPS) errors can ever occur in the client browser.
return cleanPath;
}

View file

@ -9,6 +9,7 @@ const nextConfig: NextConfig = {
},
async rewrites() {
const backendUrl = process.env.NEXT_PUBLIC_API_URL || 'https://ifixkartbe.trionixsolution.com';
const mediaUrl = 'https://ifixkartbe.trionixsolution.com';
return [
{
source: '/api/v1/:path*',
@ -16,7 +17,7 @@ const nextConfig: NextConfig = {
},
{
source: '/uploads/:path*',
destination: `${backendUrl}/uploads/:path*`,
destination: `${mediaUrl}/uploads/:path*`,
},
];
},