I want to use the same interface to import svg into both astro and tsx files. I can import the icons as jsx, but it seems inefficient for the client code (see https://twitter.com/_developit/status/1382838799420514317?lang=en).
It would be ideal to import them with <use link />, although I don't know how to do this for svg imports individually without loading the full sprite list (per page, component):
<!-- sprite -->
<svg style="display: none;">
<defs>
<symbol id="arrow-icon">
<path d=""></path>
</symbol>
</defs>
<use href="#arrow-icon"></use>
</svg>
<!-- usage -->
<svg>
<use href="#arrow-icon" ></use>
</svg>
Here's one such plugin, unfortunately it doesn't work with Vite 4 (I guess):
https://github.com/meowtec/vite-plugin-svg-sprite
import createSvgSpritePlugin from 'vite-plugin-svg-sprite';
const config = {
plugins: [
createSvgSpritePlugin({
symbolId: 'icon-[name]-[hash]',
}),
],
}
import appIconId from './path/to/icons/app.svg';
// react or vue component, as you want
export default function App() {
return (
<svg>
<use
xlinkHref={`#${appIconId}`}
/>
</svg>
);
}
Please don't import SVGs as JSX. It's the most expensive form of sprite sheet: costs a minimum of 3x more than other techniques, and hurts both runtime (rendering) performance and memory usage.
This bundle from a popular site is almost 50% SVG icons (250kb), and most are unused.
1584
292
