unplugin / unplugin-icons

šŸ¤¹ Access thousands of icons as components on-demand universally.

Home Page:https://www.npmjs.com/package/unplugin-icons

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Strip out namespace attributes for inline SVGs

bcairns opened this issue Ā· comments

Clear and concise description of the problem

Namespace attributes (xmlns and xmlns:xlink) are not needed for SVGs that are inlined in text/html documents, so they are just adding unnecessary page weight.

It would be nice if this library featured an easy config option (perhaps even enabled by default) which would strip these out automatically for inline SVG output.

Suggested solution

A stripNamespaces (or similar) boolean option available in config (I would even suggest it could be defaulted to true), which removes xmlns and xmlns:xlink attributes for inline SVG output.

Alternative

We already process our icon set using Iconify dev tools and we could strip namespaces out at this stage, but this is not an ideal solution because some icons may not be inlined. It would be better to strip out namespaces only at the point of inline usage (ie when using this plugin).

This can already be accomplished nicely in the plugin config using transform. Eg:

Icons({
    ...
    transform(svg, collection, icon) {
        return svg.replace(/ xmlns(?::xlink)?=".*?"/g, '');
    },
    ...
})

A built-in config option would just make things easier for people and see this practice more universally adopted.

Additional context

The "downside" of inlining SVGs is sending the code over and over again (vs caching an external file), this can help reduce that issue somewhat.

References:

Validations

Sounds good, PR welcome :)