Use when Icon or SVG sprite components fetch sprites.svg repeatedly causing excessive bandwidth, or when asset() is called inside a component render function for a constant URL.
Use when Icon or SVG sprite components fetch sprites.svg repeatedly causing excessive bandwidth, or when asset() is called inside a component render function for a constant URL.
SVG Sprites — prevent repeated fetch loop
Problem
Calling asset() inside the component body recomputes the URL on every render. Since <Icon> is rendered once per icon on the page, each instance triggers a separate fetch to sprites.svg — causing the sprite file to be downloaded repeatedly in a loop instead of once.
// ❌ recomputed on every <Icon> renderfunctionIcon({ id }: Props) {
const spritesUrl = asset("/sprites.svg");
return<svg><usehref={`${spritesUrl}#${id}`} /></svg>;
}
Fix — hoist to module level
// ✅ computed once, shared across all rendersconst spritesUrl = asset("/sprites.svg");
functionIcon({ id }: Props) {
return<svg><usehref={`${spritesUrl}#${id}`} /></svg>;
}
General rule
Any asset() call whose result does not depend on props belongs outside the component.