fix(images): route Next.js Image Optimization to remote backend and eliminate hydration mismatch
This commit is contained in:
1 parent
08ad51152f
commit
379fc1bb77
3 files changed
+73
-33
No files matched your search
@@ -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
|
||||
|
||||
Reference in new issue
Block a user