I have about 80 custom SVG icons that I'm importing into a Svelte front-end app. Building on https://github.com/sveltejs/template, it's built with Rollup and includes Typescript, Tailwind, and all the modern goodies.
The dilemma is how to add the icons into the app. As SVGs, the icons are short XML text strings that do not exceed 2kB.
Option 1: as image assets
- Upload all the icons as
foo.svg
intopublic/assets/icons
. - Create a svelte component
<Icon type="foo' />
that displays an the icon using<img src="foo.svg>
.
This approach means that the icons are not part of the code.
Benefits: icons can be dynamically loaded by frontend code on demand. No need to bundle all icons into app code.
Cons: slow page load if there are a lot of new icons, and the browser has to fetch a dozen 1kB files. Deploying the app as a PWA means we need to manually tell it to cache the icons beforehand.
Option 2: as part of the app build
- Use something like https://github.com/cristovao-trevisan/svelte-icon or https://github.com/codefeathers/rollup-plugin-svelte-svg to directly import each icon into code:
import Home from './icons/home.svg';
- Create a svelte component that selects the right imported component or SVG string and displays it.
Here, the icons are bundled as text strings with the app itself.
Benefits: Icons are delivered as part of the app bundle. Caching is unnecessary. Possible to dynamically modify some of the icon code e.g. colors, viewBox, etc on load.
Cons: It's unnecessary to include all icons in the app to reduce time to first byte. Can't do bundle splitting, etc. without adding more complexity. Makes the rendering slower because Javascript code needs to first turn a string into an SVG instead of just loading an image.
Questions
- It seems that building icons in tthe app is a better way from this analysis, but have I missed something?
- Does the calculus change if the "icons" are detailed images that are 50-100kB instead of the tiny strings here?
<svg-icon>
Custom Element from a string holding onlyd
path data; see iconmeister.github.io - I never did the Svelte version because Svelte handles the<svg-icon>
native element just fine. I have converted over 7000 icons from different IconSets - The JS code is only 800 Bytes GZipped – Danny '365CSI' Engelman