Optimize SVGs for Better Performance using svgo

Damon Bauer
InstructorDamon Bauer
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 6 years ago

Just like a bitmap image, you can compress an SVG by removing various pieces of code that aren’t necessary to it rendering properly. This reduces file size & download time, ultimately leading to a faster page load speed. You’ll learn how to automate the process of compressing your SVGs using svgo in the command line.

For a full list of options and plugins available to use with svgo, check out their documentation.

Note: This lesson assumes you have npm installed.

[00:06] Let's install SVGO by typing npm install -g SVGO. This just installs it globally so we can use it anywhere. With install, let's use it by typing SVGO and the path of the filename that you want to compress. In this case, I want to compress icons plug.

[00:19] By default, SVGO overwrites input files. I want to send it to a different file to output. I'll type -o for output, and I'll type icons plug .min.svg.

[00:33] Run that command. Once it's finished, SVGO responds with the original file size, the percentage reduced, and the resulting optimized file size.

[00:41] Let's take a look at optimizing a folder of SVGs. Again, let's start by writing SVGO. I'll pass the F flag which stands for folder. I'll pass it the icons folder. Then, I'll pass it the O flag for output. I'll pass it icons/out. Now, when I run this command, SVGO displays the results of compressing each icon.

[01:06] SVGO does a good job of compressing SVGs out of the box, but we can customize it by setting a few options and enabling plug-ins. Let's run the same folder optimization again. This time, I'll add the -P flag which stands for precision, provide it a value of one. This just sets the number of digits to use after a decimal point in the SVG paths.

[01:28] Next, I'll add the multi-pass flag which tells SVG to keep trying to compress the files until it can't any longer. Finally, I'll add the enable flag, set it equals and some quotes. Inside the quotes, I'm going to add a few plug-ins to help further reduce the icon size.

[01:52] These are not all of the plug-ins that you can use. This is just a small sampling. Once this completes, SVGO now displays the results from each SVG it found...

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today