Optimize SVG images for the Web
Tags: frontend, ui, react
Published: 2020-08-06 08:27:25

---
title: Optimize SVG images for the Web
published: true
description: SVG graphics are great for creating faster web applications. Optimize your SVG vector images to make your web app more responsive.
tags: FrontEnd, UI, React
cover_image: https://images.ctfassets.net/f1aa79r6xovt/3x2s6HnM6NdpQSkeZe7sey/249374d5cad6a062355d3acc1f7b4011/andrej-lisakov-V2OyJtFqEtY-unsplash.jpg
canonical_url: https://cole.codes/posts/optimize-svg-images-for-the-web
---
  
  
Scalable Vector Graphics (SVG) images are the bee&#39;s knees, that cat&#39;s meow. For those unfamiliar with those idioms, what I mean to say is that I love to use SVG images whenever possible.

**There are a few reasons why SVGs are great:**
- They can be scaled up and down without distortion.
- Vector graphics are usually smaller in file size.
- The ability to use animations and transitions on layers.



## How to Optimize an SVG
As you develop your web application, the SVG files are usually exported from programs like Sketch or Adobe Illustrator. On the other hand, you could also be using icons from websites like [Iconfinder](https://www.iconfinder.com/) or [Flaticon](https://www.flaticon.com/). These files usually contain a rich set of metadata and paths that include points used by designers. Unless you are using those specific paths and points in your application, they can be optimized to further reduce the file size.

**There is a really awesome tool called [SVGO](https://github.com/svg/svgo)** which will process and optimize your SVG files. It can remove excessive markup and metadata, optimize and clean paths, and minify the output. It can be included in your build process, or run independently.

Even better than the command line interface, is a website created by [Jake Archibald (@jaffathecake)](https://twitter.com/jaffathecake), called [&quot;SVGO Missing GUI&quot; or SVGOMG](https://jakearchibald.github.io/svgomg/).

![Demo of SVGOMG optimizing an SVG image.](//images.ctfassets.net/f1aa79r6xovt/3Ycg8OIk4WNxrKLiFtaDVq/56fbf0a16c764ed454dfafa6d959335e/Screen_Shot_2020-08-06_at_12.49.56_AM.png)


From here we can upload or paste our source code into the REPL, toggle the options, and easily gather the output for use in our HTML and JavaScript.

In the example above, we can see how **_it saved 68%_** for such a small icon. When you&#39;re working with a lot of icons, or larger vector graphics, it can make a noticeable impact on your overall application.

**Better bundle sizes mean your web app is faster, more responsive, and reaches more customers around the world. 68% is great!**
[Try it for yourself!](https://jakearchibald.github.io/svgomg/)



## Optimizing an SVG for React
If you&#39;re building your web application with React, it is not as easy to copy and paste the SVG output into your application. React uses the naming convention from JavaScript objects to create HTML elements. Instead of **stroke-width**, we use** strokeWidth** in JSX.

**Fixing all of those attributes takes too much time, and so enter: [React SVGR](https://react-svgr.com/)**

![Demo of React SVGR Playground](//images.ctfassets.net/f1aa79r6xovt/6htUdj8eezTCQYUucvbhra/f1fcd3a099271edf36c55c16cc3f03ea/Screen_Shot_2020-08-06_at_1.06.17_AM.png)


Similarly, React SVGR is also a node API, command line client, and most importantly - [a GUI playground](https://react-svgr.com/playground/). Using this tool, we can paste our SVG output from SVGO and it will output a React component.

### Optimize with SVGO or SVGR Playground
In the example above, we can see that SVGR does have an integration with SVGO if you have a JSON config object on hand. However, I rarely carry that around with me as I am developing - so I generally optimize with SVGO first and then paste the output into SVGR.



### Copy, Paste, Copy, and Paste Again
With these tools, you can deliver the most optimized version of vector graphics in your web application. Whether you&#39;re using React.JS, or vanilla HMTL and JavaScript - these tools have you covered.


> Now we&#39;re cooking with some gas - You



> 


---
- [SVGOMG Playground](https://jakearchibald.github.io/svgomg/)
- [SVGR Playground](https://react-svgr.com/playground/)

---



Photo by [Andrej Lišakov](https://unsplash.com/@lishakov?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)