Skip to content

thedaviddias/Front-End-Checklist / skills/svg-optimization

svg-optimization

Verified

Use when reviewing image assets, markup, and CDN or build transforms related to Optimize SVG files. Check encoded size, rendered size, loading strategy, and above-the-fold impact together.

FileKindSize
SKILL.mdskill md1.5 KB
references/rule.mdmarkdown4.6 KB

references/rule.md

View on GitHub
# Optimize SVG files

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

**Priority:** medium · **Difficulty:** beginner · **Time:** 10 min

---
SVG optimization removes unnecessary code while preserving visual quality.

## Code Example

```bash
# 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

| 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

```javascript
// 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)

```javascript
// 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

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

## Vite Integration

```javascript
// vite.config.js

  plugins: [
    svgr({
      svgrOptions: {
        svgo: true,
        svgoConfig: {
          plugins: [
            { name: 'removeViewBox', active: false }
          ]
        }
      }
    })
  ]
}
```

## React SVGR Component

```tsx
// After SVGR processing, import SVG as component

function Header() {
  return (
    <header>
      
    </header>
  )
}
```

## Online Tool: SVGOMG

```text
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

```xml
<!-- 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

```javascript
// 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

```bash
# 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

Aggressive optimization can break complex SVGs with filters, masks, or animations. Always test optimized SVGs visually before deploying.
receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementationStars: 322kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 322kUpdated Claude CodeCodexVerifiedInstall
using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallbackStars: 322kUpdated Claude CodeCodexVerifiedInstall
verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions alwaysStars: 322kUpdated Claude CodeCodexVerifiedInstall