Skip to main content
Beta: Front-End Checklist is currently in beta. Some issues are still being fixed. Thanks for your patience.

Optimize SVG files

SVG files are optimized with SVGO to remove unnecessary metadata and reduce size.

Utilities
Quick take
Typical fix time 10 min
  • SVGO can reduce SVG file size by 50-80%
  • Removes editor metadata, comments, and redundant attributes
  • Preserve viewBox and accessibility attributes
  • Integrate into build process for automatic optimization
Why it matters: SVGs exported from design tools contain unnecessary metadata, comments, and redundant code—optimization removes this bloat, often cutting file size in half.

Rule Details

SVG optimization removes unnecessary code while preserving visual quality.

Code Example

# Install SVGO
npm install -g svgo
 
# Optimize single file
svgo input.svg -o output.svg
 
# Optimize directory
svgo -f ./icons -o ./icons-optimized
 
# With specific plugins
svgo input.svg --config=svgo.config.js

Why It Matters

SVGs exported from design tools contain unnecessary metadata, comments, and redundant code—optimization removes this bloat, often cutting file size in half.

What SVGO Removes

ElementExampleSafe to Remove?
Editor metadata<!-- Generator: Adobe Illustrator -->Yes
Empty groups<g></g>Yes
Hidden elementsdisplay="none"Usually
Unused definitions<defs> with no referencesYes
Redundant attributesDefault valuesYes
Comments<!-- comment -->Yes
IDs (if unused)id="Layer_1"Usually

SVGO Configuration

// svgo.config.js
module.exports = {
  plugins: [
    'preset-default',
    'removeDimensions', // Use viewBox instead of width/height
    {
      name: 'removeAttrs',
      params: {
        attrs: ['data-name', 'class'] // Remove specific attributes
      }
    },
    {
      name: 'addAttributesToSVGElement',
      params: {
        attributes: [{ 'aria-hidden': 'true' }]
      }
    }
  ]
}

Safe Configuration (Preserve Accessibility)

// svgo.config.js - safe defaults
module.exports = {
  plugins: [
    {
      name: 'preset-default',
      params: {
        overrides: {
          removeViewBox: false, // Keep viewBox for scaling
          removeTitle: false,   // Keep title for accessibility
          removeDesc: false,    // Keep desc for accessibility
        }
      }
    }
  ]
}

Build Integration

// webpack.config.js
module.exports = {
  module: {
    rules: [
      {
        test: /\.svg$/,
        use: [
          {
            loader: '@svgr/webpack',
            options: {
              svgo: true,
              svgoConfig: {
                plugins: [
                  { name: 'removeViewBox', active: false }
                ]
              }
            }
          }
        ]
      }
    ]
  }
}

Vite Integration

// vite.config.js
import svgr from 'vite-plugin-svgr'
 
export default {
  plugins: [
    svgr({
      svgrOptions: {
        svgo: true,
        svgoConfig: {
          plugins: [
            { name: 'removeViewBox', active: false }
          ]
        }
      }
    })
  ]
}

React SVGR Component

// After SVGR processing, import SVG as component
import { ReactComponent as Logo } from './logo.svg'
 
function Header() {
  return (
    <header>
      <Logo className="logo" aria-label="Company Logo" />
    </header>
  )
}

Online Tool: SVGOMG

1. Go to https://jakearchibald.github.io/svgomg/
2. Paste SVG code or upload file
3. Adjust settings visually
4. Download optimized SVG
5. Copy configuration for SVGO CLI

Manual Optimization Tips

<!-- Before: Illustrator export -->
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 26.0.0 -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     x="0px" y="0px" width="100px" height="100px"
     viewBox="0 0 100 100"
     style="enable-background:new 0 0 100 100;"
     xml:space="preserve">
  <g id="Layer_1">
    <circle cx="50" cy="50" r="40" fill="#FF0000"/>
  </g>
</svg>
 
<!-- After: Optimized -->
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
  <circle cx="50" cy="50" r="40" fill="#F00"/>
</svg>

Preserving Animations

// svgo.config.js for animated SVGs
module.exports = {
  plugins: [
    {
      name: 'preset-default',
      params: {
        overrides: {
          removeHiddenElems: false, // Animations may use hidden elements
          removeUselessDefs: false, // Keep animation definitions
          removeUnknownsAndDefaults: false // Keep animation attributes
        }
      }
    }
  ]
}

Measuring Results

# Compare file sizes
ls -la icons/
# Before: icon.svg 15KB
 
svgo icons/icon.svg -o icons/icon.optimized.svg
 
ls -la icons/
# After: icon.optimized.svg 3KB (80% reduction)

Verification

  1. Compare optimized SVG with original visually
  2. Check that animations still work (if applicable)
  3. Verify accessibility attributes are preserved
  4. Test scaling at different sizes
  5. Confirm colors and gradients render correctly
Test After Optimization

Aggressive optimization can break complex SVGs with filters, masks, or animations. Always test optimized SVGs visually before deploying.

Use with AI

Copy these prompts to use with your AI assistant, or install the MCP server to use directly from Claude, Cursor, or Windsurf.

Check

Verify implementation

Check if SVG files are optimized by removing unnecessary metadata and formatting.

Fix

Auto-fix issues

Optimize SVG files using SVGO or similar tools to reduce file size.

Explain

Learn more

Explain how SVG optimization can reduce file sizes by 50-80% without quality loss.

Review

Code review

Review image assets, markup, and delivery configuration related to Optimize SVG files. Flag exact files or components where format choice, sizing, or loading behavior violates the rule, and describe how to confirm the fix in DevTools.

Sources

References used to support the guidance in this rule.

Further Reading

Tools and supplementary material for exploring the topic in more depth.

Squoosh
squoosh.appTool

Rules that often go hand-in-hand with this one.

Optimise images for faster loading

All images are compressed and metadata-stripped before deployment, removing unnecessary bytes without visible quality loss.

Images
Compress images without quality loss

All images are compressed without significant quality loss to reduce file sizes.

Images
Manage inline SVG size and complexity

Large or complex SVGs inlined in HTML are extracted to external files or components, preventing them from bloating the HTML document and blocking parsing.

Images
Serve images from a CDN

Images are served from a CDN with automatic optimization, resizing, and format conversion.

Images

Was this rule helpful?

Your feedback helps improve rule quality. This stays internal for now.

Loading feedback...
0 / 385