fix: restore microdao/agent logos and banner asset urls
- Enhanced normalizeAssetUrl to handle all edge cases - Added normalizeAssetUrl to all avatar/logo/banner usages: - agents/page.tsx - agents/[agentId]/page.tsx - citizens/page.tsx - citizens/[slug]/page.tsx - MicrodaoAgentsSection.tsx - MicrodaoBrandingCard.tsx - AgentSummaryCard.tsx - AgentChatWidget.tsx Task: TASK_PHASE_ASSET_BRANDING_HOTFIX_v1
This commit is contained in:
@@ -1,35 +1,59 @@
|
||||
/**
|
||||
* Normalize asset URL for display
|
||||
* Normalize asset URL for display.
|
||||
* This is the SINGLE source of truth for building static asset URLs.
|
||||
*
|
||||
* Handles various URL formats from the backend:
|
||||
* - /api/static/uploads/... - already correct
|
||||
* - /assets/... - static assets, use as-is
|
||||
* - /api/static/uploads/... - already correct, return as-is
|
||||
* - /assets/... - static assets in public folder, return as-is
|
||||
* - /static/uploads/... - needs /api prefix
|
||||
* - https://... - external URL, use as-is
|
||||
* - https://... or http://... - external URL, return as-is
|
||||
* - uploads/... or static/uploads/... - relative path, needs /api/static prefix
|
||||
*
|
||||
* IMPORTANT: Do NOT manually concatenate /api/static anywhere in the codebase.
|
||||
* Always use this function instead.
|
||||
*/
|
||||
export function normalizeAssetUrl(url: string | null | undefined): string | null {
|
||||
if (!url) return null;
|
||||
|
||||
// Already correct format
|
||||
if (url.startsWith('/api/static')) {
|
||||
return url;
|
||||
}
|
||||
|
||||
// Static assets in public folder
|
||||
if (url.startsWith('/assets/')) {
|
||||
return url;
|
||||
}
|
||||
|
||||
// External URLs
|
||||
// External URLs - return as-is
|
||||
if (url.startsWith('http://') || url.startsWith('https://')) {
|
||||
return url;
|
||||
}
|
||||
|
||||
// Old format - needs /api prefix
|
||||
// Already correct format with /api/static
|
||||
if (url.startsWith('/api/static')) {
|
||||
return url;
|
||||
}
|
||||
|
||||
// Static assets in public folder (/assets/...)
|
||||
if (url.startsWith('/assets/')) {
|
||||
return url;
|
||||
}
|
||||
|
||||
// Old format with /static/ prefix - add /api
|
||||
if (url.startsWith('/static/')) {
|
||||
return `/api${url}`;
|
||||
}
|
||||
|
||||
// Unknown format - return as-is
|
||||
// Relative path without leading slash (uploads/..., static/...)
|
||||
// Remove any duplicate prefixes and normalize
|
||||
let cleaned = url;
|
||||
|
||||
// Remove leading /api/static if somehow duplicated
|
||||
cleaned = cleaned.replace(/^\/api\/static\//, '');
|
||||
// Remove leading /static/
|
||||
cleaned = cleaned.replace(/^\/static\//, '');
|
||||
// Remove leading static/ (no slash)
|
||||
cleaned = cleaned.replace(/^static\//, '');
|
||||
// Remove leading slash
|
||||
cleaned = cleaned.replace(/^\/+/, '');
|
||||
|
||||
// If it looks like a relative upload path, prefix with /api/static/
|
||||
if (cleaned.startsWith('uploads/') || cleaned.includes('/')) {
|
||||
return `/api/static/${cleaned}`;
|
||||
}
|
||||
|
||||
// Unknown format - return as-is (might be a simple filename)
|
||||
return url;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user