Skip to content

Commit 68925c9

Browse files
azat-iosarah11918ArmandPhilippotflorian-lefebvreyanthomasdev
authored
feat: add experimetal svgo docs (#12637)
Co-authored-by: Sarah Rainsberger <[email protected]> Co-authored-by: Armand Philippot <[email protected]> Co-authored-by: florian-lefebvre <[email protected]> Co-authored-by: yanthomasdev <[email protected]>
1 parent d8df7a0 commit 68925c9

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed

astro.sidebar.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ export const sidebar = [
152152
'reference/experimental-flags/static-import-meta-env',
153153
'reference/experimental-flags/chrome-devtools-workspace',
154154
'reference/experimental-flags/fail-on-prerender-conflict',
155+
'reference/experimental-flags/svg-optimization',
155156
],
156157
}),
157158
'reference/legacy-flags',
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
---
2+
title: Experimental SVG optimization
3+
sidebar:
4+
label: SVG optimization
5+
i18nReady: true
6+
---
7+
8+
import Since from '~/components/Since.astro'
9+
10+
<p>
11+
12+
**Type:** `boolean | object`<br />
13+
**Default:** `false`<br />
14+
<Since v="5.16.0" />
15+
</p>
16+
17+
This experimental feature enables automatic optimization of your [SVG components](/en/guides/images/#svg-components) using [SVGO](https://svgo.dev/) during build time.
18+
19+
When enabled, your imported SVG files used as components will be optimized for smaller file sizes and better performance while maintaining visual quality. This can significantly reduce the size of your SVG assets by removing unnecessary metadata, comments, and redundant code.
20+
21+
To enable this feature with default settings, set it to `true` in your Astro config:
22+
23+
```js title="astro.config.mjs" ins={5}
24+
import { defineConfig } from "astro/config"
25+
26+
export default defineConfig({
27+
experimental: {
28+
svgo: true
29+
}
30+
})
31+
```
32+
33+
## Usage
34+
35+
No change to using SVG components is required to take advantage of this feature. With experimental `svgo` enabled, all your SVG component import files will be automatically optimized:
36+
37+
```astro title="src/pages/index.astro"
38+
---
39+
import Logo from '../assets/logo.svg';
40+
---
41+
42+
<Logo />
43+
```
44+
45+
The SVG will be optimized during the build process, resulting in smaller file sizes in your production build.
46+
47+
Note that this optimization applies to every SVG component import in your project. It is not possible to opt out on a per-component basis.
48+
49+
## Configuration
50+
51+
You can pass a [SVGO configuration object](https://github.com/svg/svgo/blob/66d503a48c6c95661726262a3068053c429b06a9/lib/types.ts#L335) to customize optimization behavior:
52+
53+
```js title="astro.config.mjs"
54+
export default defineConfig({
55+
experimental: {
56+
svgo: {
57+
plugins: [
58+
'preset-default',
59+
{
60+
name: 'removeViewBox',
61+
active: false
62+
}
63+
]
64+
}
65+
}
66+
})
67+
```
68+
69+
### `plugins`
70+
71+
**Type:** `Array<string | PluginConfig>`<br />
72+
**Default:** `[]`
73+
74+
An array of [SVGO plugins](https://svgo.dev/docs/plugins/) that will be used to optimize your SVG component imports.
75+
76+
This can include any plugins by ID name, including SVGO's `preset-default` collection of plugins. A plugin can optionally be passed as an object including both its `name` and `active` status, to enable or disable as necessary.
77+
78+
```js title="astro.config.mjs"
79+
export default defineConfig({
80+
experimental: {
81+
svgo: {
82+
plugins: [
83+
'preset-default',
84+
{
85+
name: 'removeViewBox',
86+
active: false
87+
}
88+
]
89+
}
90+
}
91+
})
92+
```
93+
94+
### Other configuration options
95+
96+
You can also pass [other SVGO configuration options](https://github.com/svg/svgo/blob/66d503a48c6c95661726262a3068053c429b06a9/lib/types.ts#L335), such as `floatPrecision` and `multipass`, directly to your config object:
97+
98+
```js title="astro.config.mjs"
99+
export default defineConfig({
100+
experimental: {
101+
svgo: {
102+
floatPrecision: 2,
103+
multipass: true
104+
}
105+
}
106+
})
107+
```
108+
109+
## Common use cases
110+
111+
SVGO provides an extensive [default plugin list](https://svgo.dev/docs/preset-default/) with opinionated optimizations that is more convenient than adding each plugin individually. However, you may need to customize it further for your needs. For example, it may remove items or clean up too aggressively for your situation.
112+
113+
### Preserve specific attributes
114+
115+
You may want to preserve certain SVG attributes, such as the `viewBox`, that SVGO removes by default:
116+
117+
```js title="astro.config.mjs"
118+
export default defineConfig({
119+
experimental: {
120+
svgo: {
121+
plugins: [
122+
'preset-default',
123+
{
124+
name: 'removeViewBox',
125+
active: false // Preserve viewBox attribute
126+
}
127+
]
128+
}
129+
}
130+
})
131+
```
132+
133+
### Remove specific elements
134+
135+
You can configure plugins to remove specific unwanted elements like metadata or hidden layers. Note that many plugins are already included in `preset-default`, so you typically only need to configure their behavior:
136+
137+
```js title="astro.config.mjs"
138+
export default defineConfig({
139+
experimental: {
140+
svgo: {
141+
plugins: [
142+
'preset-default',
143+
{
144+
name: 'removeMetadata',
145+
active: true
146+
}
147+
]
148+
}
149+
}
150+
})
151+
```
152+
153+
### Custom precision
154+
155+
Control the precision of numeric values in path data:
156+
157+
```js title="astro.config.mjs"
158+
export default defineConfig({
159+
experimental: {
160+
svgo: {
161+
floatPrecision: 2
162+
}
163+
}
164+
})
165+
```
166+
167+
## How it works
168+
169+
SVG optimization happens during the build process, not at runtime:
170+
171+
- In **development mode**, SVG files are not optimized to ensure faster rebuild times and a smoother development experience.
172+
- In **production builds**, all imported SVG files are optimized once during the build process, resulting in smaller file sizes.
173+
- There is **no runtime overhead** - optimized SVGs are served as pre-processed static assets.
174+
175+
While the optimization process may slightly increase your build times, the result is smaller file sizes and faster page loads for your users.
176+
177+
## Further reading
178+
179+
- [SVGO documentation](https://svgo.dev/)

0 commit comments

Comments
 (0)