Breaking Up with SVG-in-JS in 2023
kurtextrem.de
kurtextrem.de
This article appears to do a reasonable job of weighing the the performance pros and cons to justify such a broad statement. However it fails to consider the use cases beyond plonking static SVG images into a page.
The initial example of "people are embedding SVG icon packages as SVG in JS" seems to be the only type of use case that each abstract justification is being validated in. This advice needs a heavy qualifier of "for static assets", and even then, only when dealing with large individual assets, or large quantities of individual and artificially inseparable SVGs such as icon packages. I'm suspicious this whole argument stems from people blindly "importing" svg bundles into JS without fully understanding what that actually means in the end result of their bundle. It would be more useful to talk about this issue with it's original context, where there are multiple lessons to learn than coerce it into an over generalisation.
This is particularly relevant for icons and buttons in key places in the UI, where it's janky to have them appear even a split second after the rest of the UI.
How can you acheive that using other approaches?
“How else could you possibly have SVGs render inline other than to include a massively bloated JS representation of a string in your bundle?”
- the React ecosystem, 2023
The answer, as you’ve shown, is “use the platform”. Sometimes I feel like React actively works against that.
you can make a SPA using vanilla JS, without any bundle using plain es6 module and be perfectly fine. I made one such scaffold to showcase people how to do exactly that: https://mickael-kerjean.github.io/skeleton/example/index.htm...
I don’t want to sound too negative (but I’m about to): this attitude in web development really bugs me. Shaving 5ms off render time on a crappy Android device should be a desirable goal, not something you have to be cajoled into doing. You should be motivated by that! But instead the industry is all about “DX”: is that change going to complicate my precious build script? Then out the window it goes.
We have an industry that prioritises developer experience over user experience and I think that has a role to play in the shittiness of the web these days.
I just don’t see a convincing (concrete) argument in this article.
And if you check the replies you’ll see examples of other, more efficient ways to do the exact same thing.
It speaks to a bad level of myopia in the React community that when presented with the challenge of “put statically rendered markup in HTML” the accepted solution is “take that markup, transform it into a bloated JS representation, transform it back to markup during server side render while also shipping the bloated representation in the client JS, then unnecessarily hydrate that representation on page load” and no one sees any problem.
of course if what the HTMX cult is chanting is true, then the best way to get the job done and save cycles is to skip as much of the frontend dev stuff as possible. (though this would probably lead to the problem of how to hire a dev versed in the occult.)
Here’s the thing: I would love to worry about 5ms optimizations, but my time is limited and there’s hundreds of 100ms optimizations I can do first. Those haven’t got done because, again, my time is limited and we had plenty of infinite-ms optimizations to do first by making various things possible to do at all.
https://developer.mozilla.org/en-US/docs/Web/SVG/Element/vie...
On the surface I assume it’s because you need to load all the js, create the dom, and then apply css in that order instead of just having a separate file for css loaded quickly at the beginning and letting the dom load in after.
But is there a benefit to css in js where only the css used in the components your rendering is loaded? Like an automatic code splitting for css?