This commit is contained in:
Iliyan Angelov
2025-11-25 09:21:00 +02:00
parent 82024016cd
commit 4c8b71fe0d
3 changed files with 162 additions and 122 deletions

View File

@@ -427,32 +427,48 @@ export const serviceUtils = {
}).format(numPrice);
},
// Get service image URL
// Get service image URL with cache-busting
// Use relative URLs for same-domain images (Next.js can optimize via rewrites)
// Use absolute URLs only for external images
// Adds updated_at timestamp as query parameter for cache-busting when images change
getServiceImageUrl: (service: Service): string => {
let imageUrl: string = '';
// If service has an uploaded image
if (service.image && typeof service.image === 'string' && service.image.startsWith('/media/')) {
// Use relative URL - Next.js rewrite will handle fetching from backend during optimization
return service.image;
imageUrl = service.image;
}
// If service has an image_url
if (service.image_url) {
else if (service.image_url) {
if (service.image_url.startsWith('http')) {
// External URL - keep as absolute
return service.image_url;
}
if (service.image_url.startsWith('/media/')) {
imageUrl = service.image_url;
} else if (service.image_url.startsWith('/media/')) {
// Same domain media - use relative URL
return service.image_url;
imageUrl = service.image_url;
} else {
// Other relative URLs
imageUrl = service.image_url;
}
// Other relative URLs
return service.image_url;
} else {
// Fallback to default image (relative is fine for public images)
imageUrl = '/images/service/default.png';
}
// Fallback to default image (relative is fine for public images)
return '/images/service/default.png';
// Add cache-busting query parameter using updated_at timestamp
// This ensures images refresh when service is updated
if (service.updated_at && imageUrl && !imageUrl.includes('?')) {
try {
const timestamp = new Date(service.updated_at).getTime();
const separator = imageUrl.includes('?') ? '&' : '?';
imageUrl = `${imageUrl}${separator}v=${timestamp}`;
} catch (error) {
// If date parsing fails, just return the URL without cache-busting
console.warn('Failed to parse updated_at for cache-busting:', error);
}
}
return imageUrl;
},
// Generate service slug from title