SVG libraries
register('lucide', {
resolver: (name) => `https://cdn.jsdelivr.net/npm/lucide-static/icons/${name}.svg`,
mutator: (svg) => svg.setAttribute('stroke', 'currentColor'),
})
Loading…
The resolver may return
| Return | Effect |
|---|---|
| SVG markup | Used as is |
A URL (https://, /, ./) | Fetched |
import('./x.svg?raw') | Unwrapped - bundled icons, one chunk each |
Throw for a name you don't have. Every result is parsed and rejected if it isn't <svg>, so an HTML error page never gets injected.
register('bundled', {
resolver: (name) => import(`./icons/${name}.svg?raw`),
})
A map of strings
Have the SVGs already? Skip the resolver:
register('app', {
icons: {
heart: '<svg …>',
lock: '<svg …>',
bell: {
default: '<svg …>',
active: '<svg …>',
},
},
})
Values are markup or URLs; a nested object is a variant map with a default. A promise of the map works too - see From a JSON file.
Variants
resolver(name, variant) - use it however your URL scheme likes:
register('app', {
resolver: (name, variant) => `/icons/${name}${variant === 'active' ? '-filled' : ''}.svg`,
})
Loading…
Caching
Per name and variant. Concurrent requests share one fetch. Failures aren't cached. Re-registering the name drops the cache.
Warm it by resolving the names you know a page needs:
import { getIcon } from '@munsonlabs/sigil'
await Promise.all(['heart', 'lock', 'star'].map((name) => getIcon(name, { library: 'lucide' })))
Output: <span class="icon-svg" aria-hidden="true">…</span>. Markup goes in via innerHTML - trust your source.
On a server
Without a DOMParser there is nothing to parse with, so markup is checked by its root tag only, mutator is skipped, and nothing is cached - the client does the real work once it hydrates. Nothing unvalidated ever sits in the process-wide registry.