I've mastered the process of getting SVG icons to load on my website, but I am facing a challenge in meeting all the criteria listed below:
- Need to be able to use SVG icons in CSS
- Avoid any flash of missing icons (FOMI)
- Minimize the initial page size
- Ensure cached SVGs
- Ability to utilize a CDN
- Must support
fill: currentColor
for consistent text color matching, similar to icon-fonts - Bonus: Implement Pixel-aligning the SVGs for sharp appearance
To meet criteria 1, 2, 3, and 4, we can use an external sprite map as shown below:
<svg viewBox="0 0 100 100">
<use xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:href="/assets/sprite-4faa5ef477.svg#icon-asterisk-50af6"></use>
</svg>
However, due to the ongoing CORS issue with browsers (read more here), using a CDN is currently not possible.
We could potentially address this by implementing patches for support on external domains. Yet, this solution may not benefit CSS since it primarily monitors the DOM. Additionally, it triggers numerous failed requests to inaccessible files, one for each icon on the webpage.
If we intend to leverage a CDN, alternative methods such as inline SVG insertion (increasing page size without caching) or AJAX loading (leading to FOMI) can be considered.
Hence, are there any viable solutions that fulfill all 5 7 requirements?
In essence, I aspire for SVGs to offer the same convenience as icon-fonts; otherwise, the transition seems futile. While SVGs allow for varied colors and enhanced accessibility, achieving an optimal aesthetic and efficient loading has proven challenging.