Optimization methods
There are three ways to optimize SVGs:1. Command palette
Open the command palette and search for “Optimize SVG”:- Open an SVG file or select one in the editor
- Press
Cmd+Shift+P(Mac) orCtrl+Shift+P(Windows/Linux) - Type “Optimize SVG” and press Enter
- The file is optimized and saved
2. Editor title button
When viewing an SVG file, a lightning bolt icon appears in the editor title bar:- Click the ⚡ icon to optimize the current SVG file
- The button only appears when the active file has a
.svgextension - Located in the navigation section of the editor title
3. Preview panel button
The preview panel includes an optimize button in the header:- Click the lightning bolt icon in the preview panel header
- Optimizes the currently previewed SVG file
- Works even if the file hasn’t been saved yet
4. Hover preview link
When hovering over SVG elements in supported files:- Click the ”⚡ Optimizar SVG” link in the hover tooltip
- Optimizes only the specific SVG element under the cursor
- Preserves surrounding code and framework syntax
SVGO configuration
Better SVG uses SVGO with the following plugins:Preset default
Base configuration with overrides:cleanupIds: false- Preserves ID attributes to maintain referencesremoveUnknownsAndDefaults- Behavior depends on optimization context
Additional plugins
The
multipass: true option ensures SVGO runs multiple optimization passes for maximum file size reduction.File vs. inline optimization
The extension uses different optimization strategies depending on the context:File optimization
When optimizing entire.svg files:
- Class removal: Controlled by
betterSvg.removeClassessetting (default:true) - Unknown attributes: Removed (e.g.,
onClick,data-*) - Target: Entire document content
- Trigger: Command palette, editor button, or preview panel button
Inline optimization
When optimizing SVG elements in code:- Class preservation: Classes are always preserved
- Unknown attributes: Kept to maintain framework functionality
- JSX support: Converts JSX ↔ SVG syntax as needed
- Target: Only the specific SVG element under cursor
- Trigger: Hover preview link
Configuration options
Remove classes
Control whether class attributes are removed during file optimization:- Default:
true - Type:
boolean - Description: Remove class attributes from SVG elements during optimization
- Scope: Only affects file optimization, not inline optimization
true:
classattributes are removed from all SVG elements- Reduces file size by eliminating unused CSS selectors
- Recommended for standalone SVG files
false:
classattributes are preserved- Useful if your SVG relies on external CSS
- Better for SVGs used with stylesheets
JSX transformation
For inline optimization in React/JSX files, the extension:Before optimization (JSX → SVG)
- Converts
classNametoclass - Transforms camelCase to kebab-case (e.g.,
strokeWidth→stroke-width) - Encodes JSX expressions (e.g.,
{value}→ Base64) - Preserves spread operators as
data-spread-*attributes - Removes JSX comments (
{/* */}and//)
After optimization (SVG → JSX)
- Converts
classback toclassName - Transforms kebab-case back to camelCase
- Decodes JSX expressions from Base64
- Restores spread operators
- Maintains event handlers and dynamic attributes
Language detection
The extension automatically detects the file language to determine if camelCase conversion is needed:javascriptreact(.jsx)typescriptreact(.tsx)
- All other languages (Astro, Vue, Svelte, HTML, SVG, etc.)
Size reporting
After optimization, a notification displays the file size reduction:For files ≥ 1 KB
For files < 1 KB
- Original size
- Optimized size
- Percentage saved
- Formatted in KB or bytes depending on size
Size calculation uses
Buffer.byteLength with UTF-8 encoding for accurate byte counts.Advanced: Plugin details
The extension uses SVGO 4.0.0 (browser version) with these optimizations:removeDoctype
Removes XML DOCTYPE declarations:
removeComments
Removes XML comments:
removeAttrs
Removes specified attributes:
xmlns:xlink- Legacy XLink namespacexml:space- XML whitespace handlingclass- Only ifremoveClassesis true
cleanupIds: false
Preserves ID attributes:
- Prevents breaking internal references (e.g.,
url(#gradient)) - Maintains links between elements
- Ensures filters and masks continue working
Technical implementation
Optimization functions are defined inextension.ts:
optimizeSvgDocument(extension.ts:301) - Optimizes entire SVG filesoptimizeSvgInline(extension.ts:336) - Optimizes inline SVG elementsgetSvgoPlugins(extension.ts:270) - Returns plugin configuration
prepareForOptimization(svgTransform.ts:537) - Converts JSX to SVG before SVGOfinalizeAfterOptimization(svgTransform.ts:559) - Converts SVG back to JSX after SVGO
Best practices
- Test before committing: Use the preview panel to verify optimization results
- Preserve classes when needed: Set
removeClasses: falsefor SVGs with external CSS - Optimize inline SVGs: Use hover preview optimization for embedded icons
- Check file size: Compare before/after sizes in the notification
- Keep IDs: The extension preserves IDs by default to maintain functionality
Error handling
If optimization fails, you’ll see an error notification:- Invalid SVG syntax
- Malformed XML
- SVGO parser errors
- Unresolvable references