-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: add experimetal svgo docs #12637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
azat-io
wants to merge
9
commits into
withastro:main
Choose a base branch
from
azat-io:docs/svgo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+180
−0
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b069776
feat: add experimetal svgo docs
azat-io 81749d7
refactor: make fixes by review
azat-io 4fc5050
refactor: make fixes by review
azat-io 4ed24b9
fix: fix svgo configuration link
azat-io 3005899
feat: use new svgo config format
azat-io a2728a8
refactor: update link to svgo options
azat-io a04876b
update version number for the feature
sarah11918 cbcd2d7
no fallback; throws error now
sarah11918 553426a
Armand boss reviews
sarah11918 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
src/content/docs/en/reference/experimental-flags/svg-optimization.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| --- | ||
| title: Experimental SVG optimization | ||
| sidebar: | ||
| label: SVG optimization | ||
| i18nReady: true | ||
| --- | ||
|
|
||
| import Since from '~/components/Since.astro' | ||
|
|
||
| <p> | ||
|
|
||
| **Type:** `boolean | object`<br /> | ||
| **Default:** `false`<br /> | ||
| <Since v="5.16.0" /> | ||
| </p> | ||
|
|
||
| This experimental feature enables automatic optimization of your [SVG components](/en/guides/images/#svg-components) using [SVGO](https://svgo.dev/) during build time. | ||
|
|
||
| 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. | ||
|
|
||
| To enable this feature with default settings, set it to `true` in your Astro config: | ||
|
|
||
| ```js title="astro.config.mjs" ins={5} | ||
| import { defineConfig } from "astro/config" | ||
|
|
||
| export default defineConfig({ | ||
| experimental: { | ||
| svgo: true | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| 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: | ||
|
|
||
| ```astro title="src/pages/index.astro" | ||
| --- | ||
| import Logo from '../assets/logo.svg'; | ||
| --- | ||
|
|
||
| <Logo /> | ||
| ``` | ||
|
|
||
| The SVG will be optimized during the build process, resulting in smaller file sizes in your production build. | ||
|
|
||
| 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. | ||
|
|
||
| ## Configuration | ||
|
|
||
| You can pass a [SVGO configuration object](https://github.com/svg/svgo/blob/66d503a48c6c95661726262a3068053c429b06a9/lib/types.ts#L335) to customize optimization behavior: | ||
|
|
||
| ```js title="astro.config.mjs" | ||
| export default defineConfig({ | ||
| experimental: { | ||
| svgo: { | ||
| plugins: [ | ||
| 'preset-default', | ||
| { | ||
| name: 'removeViewBox', | ||
| active: false | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| ### `plugins` | ||
|
|
||
| **Type:** `Array<string | PluginConfig>`<br /> | ||
| **Default:** `[]` | ||
|
|
||
| An array of [SVGO plugins](https://svgo.dev/docs/plugins/) that will be used to optimize your SVG component imports. | ||
|
|
||
| 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. | ||
|
|
||
| ```js title="astro.config.mjs" | ||
| export default defineConfig({ | ||
| experimental: { | ||
| svgo: { | ||
| plugins: [ | ||
| 'preset-default', | ||
| { | ||
| name: 'removeViewBox', | ||
| active: false | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| ### Other configuration options | ||
|
|
||
| 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: | ||
|
|
||
| ```js title="astro.config.mjs" | ||
| export default defineConfig({ | ||
| experimental: { | ||
| svgo: { | ||
| floatPrecision: 2, | ||
| multipass: true | ||
| } | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| ## Common use cases | ||
|
|
||
| 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. | ||
|
|
||
| ### Preserve specific attributes | ||
|
|
||
| You may want to preserve certain SVG attributes, such as the `viewBox`, that SVGO removes by default: | ||
|
|
||
| ```js title="astro.config.mjs" | ||
| export default defineConfig({ | ||
| experimental: { | ||
| svgo: { | ||
| plugins: [ | ||
| 'preset-default', | ||
| { | ||
| name: 'removeViewBox', | ||
| active: false // Preserve viewBox attribute | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| ### Remove specific elements | ||
|
|
||
| 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: | ||
|
|
||
| ```js title="astro.config.mjs" | ||
| export default defineConfig({ | ||
| experimental: { | ||
| svgo: { | ||
| plugins: [ | ||
| 'preset-default', | ||
| { | ||
| name: 'removeMetadata', | ||
| active: true | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| ### Custom precision | ||
|
|
||
| Control the precision of numeric values in path data: | ||
|
|
||
| ```js title="astro.config.mjs" | ||
| export default defineConfig({ | ||
| experimental: { | ||
| svgo: { | ||
| floatPrecision: 2 | ||
| } | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| ## How it works | ||
|
|
||
| SVG optimization happens during the build process, not at runtime: | ||
|
|
||
| - In **development mode**, SVG files are not optimized to ensure faster rebuild times and a smoother development experience. | ||
| - In **production builds**, all imported SVG files are optimized once during the build process, resulting in smaller file sizes. | ||
| - There is **no runtime overhead** - optimized SVGs are served as pre-processed static assets. | ||
|
|
||
| While the optimization process may slightly increase your build times, the result is smaller file sizes and faster page loads for your users. | ||
|
|
||
| ## Further reading | ||
|
|
||
| - [SVGO documentation](https://svgo.dev/) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.