Common Pitfalls when Serving SVGs and How to Solve Them

Optimizing SVGs is great for performance, but can we go beyond? In this post I will show some common issues with inlined and responsive SVGs applied to a real website.

José M. Pérez
The Startup
13 min readSep 22, 2018

--

Scalable Vector Graphics (SVG) are ideal to show logos and drawings on the web due to their small size. SVGs also have other useful applications, such as placeholders for lazy loaded images and animations. In a past post I also explained how to optimize a SVG with several tools that remove unneeded code and simplify shapes.

Even if we optimize our SVG assets, we can still make mistakes delivering that SVG to the browser. In this post I am going to explain some issues I found on Spotify’s website, where the same SVG is inlined several times and where a responsive SVG logo could save additional network requests.

If there is something that all Spotify sites have in common, it’s the Spotify logo. It is usually shown in the header of the page, and also in the footer. In addition the Spotify circle logo might appear somewhere in the middle.

Spotify’s home page on www.spotify.com. The logo is shown in the header and the footer.

Spotify’s logo is served using SVG. This vector format is ideal for drawings and icons. They look crisp on every screen, are usually small in bytes, are compressible, stylable through CSS, scriptable through JS… What’s not to like?

There are still ways to misuse SVGs or not take advantage of its capabilities. In this post I want to explain some improvements that can be applied to the Spotify logo and the way it is served.

Tip 1: Include it once

SVGs can be inserted on web pages in different ways. In the case of the Spotify logo above it is inlined in CSS, setting it as a background-image:

Inspecting the Spotify logo in the header.

Chrome Developer Tools doesn’t show the whole code for the logo. If we inspect it we get this not showing the whole code for the logo, which is this:

The size of the image, not compressed, is 4,960 bytes. It’s not huge for an image, but we’ll see there are ways to improve it.

The screenshot shows the style applied to .navbar-logo, which is the one shown in the header. The same logo is inlined a second time for the footer, applied to the .footer .footer-logo>a CSS rule:

Inspecting the Spotify logo in the footer.

If that wasn’t enough, it is inlined a third time in the .navbar-transparent-signup-b .navbar-logo CSS rule, which is not being applied to this specific page.

Even though gzip helps with the size of the CSS, a better way would be to include it just once in a CSS class that can be reused as needed. Stylesheets are part of the critical path, so any savings here means that the page will render faster.

Tip 2) Optimize the logo

If you take a close look at the SVG code for the Spotify logo you will see an id and style that are not needed. Optimizing SVGs by hand can be tricky, but fortunately there are tools like SVGOMG that will make these and more changes for us.

Passing the image through the tool, keeping the same shape precision, gives us this code:

That is 4,383 bytes, amounting to 11,6% savings respect the original image.

We can achieve further improvements by simplifying paths and rounding the numbers. For example, instead of having a viewbox of 566.93 x 170.04 we could convert it to 567 x 170. Same thing with the position of the points in the paths.

If you are interested in this topic, I recommend you to read this series of posts by Andreas Larsen:

I spent some time optimizing the Spotify logo following Andreas’ tips. You can read more about it on a post I wrote. I managed to get the logo down to 1,859 bytes, or 62,7% reduction from the original image. The resulting image is visually almost identical to the original one.

I created a codepen so you can switch between the 2 versions and see it more clearly.

Tip 3: Responsive logo!

SVG is really powerful and people are discovering more use cases every day. We can use SVG to create a responsive logo that changes its contents depending on the size of the image. That’s right, the size of the image, not the viewport. This is similar to what container queries are supposed to do, but we can do it today.

Take for example the Spotify for Artists site. On narrow windows it shows the Spotify logo + “for Artists”.

On wider windows it overrides the logo, using a wider one composed of the Spotify logo with “Spotify” + “for Artists”.

Instead of serving 2 different SVGs it’s possible to serve a single one. Then we define the width and height for the logo and let the logo render one of the versions. The advantage is a smaller payload since we can reuse elements like the Spotify circle logo inside the SVG.

Building a responsive image” is a post that explains how to achieve this. It shows how to create a responsive logo with 4 different versions, including a rotated/vertical one. You can see a demo on codepen.

I thought I would do the same for the Spotify logo, switching between the “circle” and the “circle + Spotify” versions. I created a codepen to demo it.

The code for the responsive Spotify logo looks like this:

After minification, the logo weights 4,423 bytes, which is still smaller than the original logo (4,960 bytes).

Conclusion

There are many ways we can optimize the delivery of SVGs. Even though they are highly compressible and usually small, there is usually room for delivering them better.

I hope this post has showed you some ways to optimize SVGs and some of its capabilities.

Originally published at jmperezperez.com.

This story is published in The Startup, Medium’s largest entrepreneurship publication followed by + 373,446 people.

Subscribe to receive our top stories here.

--

--

José M. Pérez
The Startup

Engineer at Meta (prev. Facebook). Ex-Spotify. Google Dev Expert in Web technologies. I like JS and performance optimization.