Optimize SVG files
SVG files are optimized with SVGO to remove unnecessary metadata and reduce size.
- 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
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.jsWhy 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
| Element | Example | Safe to Remove? |
|---|---|---|
| Editor metadata | <!-- Generator: Adobe Illustrator --> | Yes |
| Empty groups | <g></g> | Yes |
| Hidden elements | display="none" | Usually |
| Unused definitions | <defs> with no references | Yes |
| Redundant attributes | Default values | Yes |
| 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 CLIManual 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
- Compare optimized SVG with original visually
- Check that animations still work (if applicable)
- Verify accessibility attributes are preserved
- Test scaling at different sizes
- Confirm colors and gradients render correctly
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.