fix(media): resolve product images to relative /uploads/ path and strip loopback addresses
This commit is contained in:
parent
7db6748ffb
commit
148ebceae4
3 changed files with 16 additions and 8 deletions
|
|
@ -9,6 +9,7 @@ import { FloatingButtons } from '@/components/FloatingButtons';
|
||||||
import { StickyHeaderSpacer } from '@/components/StickyHeaderSpacer';
|
import { StickyHeaderSpacer } from '@/components/StickyHeaderSpacer';
|
||||||
|
|
||||||
import { catalogServerService } from '@/services/api/catalogServerService';
|
import { catalogServerService } from '@/services/api/catalogServerService';
|
||||||
|
import { getImageUrl } from '@/lib/utils';
|
||||||
|
|
||||||
|
|
||||||
import { ProductInteractiveGrid } from '@/components/pdp/ProductInteractiveGrid';
|
import { ProductInteractiveGrid } from '@/components/pdp/ProductInteractiveGrid';
|
||||||
|
|
@ -392,9 +393,7 @@ export default async function ProductDetailPage({ params }: ProductDetailPagePro
|
||||||
|
|
||||||
const primaryVariant = product.variants?.[0];
|
const primaryVariant = product.variants?.[0];
|
||||||
const primaryImage = product.images?.[0]?.image_url || '/images/placeholder-product.webp';
|
const primaryImage = product.images?.[0]?.image_url || '/images/placeholder-product.webp';
|
||||||
const resolvedImageUrl = primaryImage.startsWith('http')
|
const resolvedImageUrl = getImageUrl(primaryImage);
|
||||||
? primaryImage
|
|
||||||
: `https://ifixkartbe.trionixsolution.com/uploads/${primaryImage.replace(/^\//, '')}`;
|
|
||||||
|
|
||||||
const jsonLd = {
|
const jsonLd = {
|
||||||
'@context': 'https://schema.org/',
|
'@context': 'https://schema.org/',
|
||||||
|
|
|
||||||
16
lib/utils.ts
16
lib/utils.ts
|
|
@ -112,6 +112,11 @@ export function getImageUrl(path?: string | null | unknown): string {
|
||||||
trimmed.startsWith('data:') ||
|
trimmed.startsWith('data:') ||
|
||||||
trimmed.startsWith('blob:')
|
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;
|
return trimmed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -136,12 +141,15 @@ export function getImageUrl(path?: string | null | unknown): string {
|
||||||
cleanPath = `/${withoutLeadingSlash}`;
|
cleanPath = `/${withoutLeadingSlash}`;
|
||||||
} else if (withoutLeadingSlash.startsWith('product/')) {
|
} else if (withoutLeadingSlash.startsWith('product/')) {
|
||||||
cleanPath = `/uploads/${withoutLeadingSlash}`;
|
cleanPath = `/uploads/${withoutLeadingSlash}`;
|
||||||
|
} else {
|
||||||
|
cleanPath = `/uploads/${withoutLeadingSlash}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const backendUrl =
|
// Always return relative /uploads/... so that:
|
||||||
process.env.NEXT_PUBLIC_API_URL || 'https://ifixkartbe.trionixsolution.com';
|
// 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/.
|
||||||
return `${backendUrl.replace(/\/$/, '')}${cleanPath}`;
|
// 3. No loopback IP (127.0.0.1) or mixed-content (HTTP on HTTPS) errors can ever occur in the client browser.
|
||||||
|
return cleanPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ const nextConfig: NextConfig = {
|
||||||
},
|
},
|
||||||
async rewrites() {
|
async rewrites() {
|
||||||
const backendUrl = process.env.NEXT_PUBLIC_API_URL || 'https://ifixkartbe.trionixsolution.com';
|
const backendUrl = process.env.NEXT_PUBLIC_API_URL || 'https://ifixkartbe.trionixsolution.com';
|
||||||
|
const mediaUrl = 'https://ifixkartbe.trionixsolution.com';
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
source: '/api/v1/:path*',
|
source: '/api/v1/:path*',
|
||||||
|
|
@ -16,7 +17,7 @@ const nextConfig: NextConfig = {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
source: '/uploads/:path*',
|
source: '/uploads/:path*',
|
||||||
destination: `${backendUrl}/uploads/:path*`,
|
destination: `${mediaUrl}/uploads/:path*`,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue