(null);
const [error, setError] = useState(false);
useEffect(() => {
if (!src) {
setBlobUrl(null);
setError(false);
return;
}
// Pass-through for data: and blob: URLs, and for absolute http(s) URLs
// (e.g. Supabase signed URLs already carry their own auth token in the
// query string and are fetchable directly by the browser).
if (
src.startsWith('data:') ||
src.startsWith('blob:') ||
src.startsWith('http://') ||
src.startsWith('https://')
) {
setBlobUrl(src);
setError(false);
return;
}
let cancelled = false;
let createdUrl: string | null = null;
setError(false);
// Strip our axios baseURL prefix so the path doesn't double up.
const relative = src.replace(/^\/api\/v1/, '');
api
.get(relative, { responseType: 'blob' })
.then((res) => {
if (cancelled) return;
createdUrl = URL.createObjectURL(res.data);
setBlobUrl(createdUrl);
})
.catch(() => {
if (!cancelled) setError(true);
});
return () => {
cancelled = true;
if (createdUrl) URL.revokeObjectURL(createdUrl);
};
}, [src]);
if (error || !src) return <>{fallback}>;
if (!blobUrl) {
const { className = '', ...rest } = imgProps;
return (
)}
className={`${className} flex items-center justify-center bg-gray-100`}
aria-busy="true"
aria-label="Cargando imagen"
>
);
}
return
;
}