Last active
June 9, 2024 23:48
-
-
Save jaydenseric/1b4cc45a61171ff80d2e8ac49527e515 to your computer and use it in GitHub Desktop.
Revisions
-
jaydenseric revised this gist
Jan 8, 2018 . No changes.There are no files selected for viewing
-
jaydenseric created this gist
Jan 8, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,73 @@ # How to optimize SVG Editors like Illustrator can save out some really dumb SVG code sometimes. Properly optimized SVG files can be as much as 80% smaller. Bunches of empty groups, pointless attributes and many other inefficiencies decrease readability and reliability. Every SVG file should be manually optimized in 3 passes using: 1. Your vector graphic editor. 2. The [SVGO command-line optimization tool](https://github.com/svg/svgo). 3. Your text editor. ## 1. Use your vector graphic editor Open the vector file in Illustrator or Sketch and open the layers inspector to take a look. Every Node.js in the panel relates to a separate chunk of code in you SVG. The more you can combine and simplify the better. 1. Delete invisible layers. 2. Carefully consider converting all text to paths. 3. Combine paths. If you are using a combination of shapes, paths and strokes for a symbol, convert them all to paths and combine them. 4. Don't mask; crop by reshaping you paths and actually deleting hidden content. Masked detail is saved in the code even if you can't see it. This includes elements that overflow the canvas (or in SVG terms, the _viewbox_). 5. Simplify groups. In fact, try to avoid using them at all. 6. Scan for non-SVG friendly elements such as embedded raster images. 7. Lastly, trim your canvas. Do this last since the earlier steps often create more whitespace. In Illustrator, select _Object_, _Artboards_, _Fit to Artwork Bounds_. Once you have saved your SVG file, quickly open it in a text editor and check that the `viewBox` attribute begins with `0 0`. If it does not the _x_ and _y_ coordinates originate from a position other than the most efficient _x = 0_, _y = 0_. Illustrator tends to do this from time to time, I find the easiest solution is to select everything, copy it, create a new document, paste the content into the center and trim the canvas. ## 2. Use SVGO [SVGO](https://github.com/svg/svgo) is a Node.js based open-source project hosted on Github. Stick to the command line tool even thought there is a less up-to-date GUI available. It works amazingly, [slicing through filthy code](https://github.com/svg/svgo#what-it-can-do) like a lightsaber through butter. This is what will get you the big file-size savings. A lot of the earlier optimizations made in the editor are simply to give this tool the best shot at success. ### Installation Make sure you have [Node.js and npm](http://nodejs.org) installed on you system, then run: ```shell npm install -g svgo ``` ### Usage Have a read of the [official SVGO instructions](https://github.com/svg/svgo#how-to-use). Remember to back up your SVG files before you optimize them, just in case things go wrong. Optimize a file: ```shell svgo test.svg ``` Optimize all SVG files in a folder: ```shell svgo -f ../path/to/folder/with/svg/files ``` Always open the optimized images in a web browser to check they still look correct before moving on. ## 3. Use your text editor Open up the optimized image in you text editor to check for any remaining fluff that could be removed. Sometimes (particularly when working with icons destined for an icon font) a color that was intended to be black made it all the way thought the process as a slightly gray hexadecimal. You can delete these sightly off path fill attributes since black is the SVG default. Another common one is unnecessary path `fill-rule` attributes. Cautiously remove it and preview the change before ditching it. You may be able to make further common-sense tweaks to the simplified code at this point. Create groups and apply common path attributes where there will be a net code reduction. ## In conclusion Don't be slack! The file-size savings are dramatic. My SVGs tend to have global uses, for example, as the main logo or [compiling to characters in the icon font](https://jaydenseric.com/blog/font-icons-like-a-boss-with-sass-and-font-custom). Reducing the file size of global assets improves the initial page load time for every single visitor to your site. If your SVG assets get reused in later projects like a lot of mine do, saving out the files optimally from the start will make an ongoing difference. The more intimate you are with the code quality of the SVG files being produced the better. With a finger on the pulse you will be able to identify and address any recurring inefficiencies.