-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Merged
+180
−0
Merged
Changes from 1 commit
Commits
Show all changes
10 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 383d3ee
Merge branch 'main' into docs/svgo
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
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
187 changes: 187 additions & 0 deletions
187
src/content/docs/en/reference/experimental-flags/svg.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,187 @@ | ||
| --- | ||
| title: Experimental SVG optimization | ||
| sidebar: | ||
| label: SVG optimization | ||
| i18nReady: true | ||
| --- | ||
|
|
||
| import Since from '~/components/Since.astro' | ||
|
|
||
| <p> | ||
|
|
||
| **Type:** `object`<br /> | ||
| **Default:** `undefined`<br /> | ||
| <Since v="5.8.0" /> | ||
| </p> | ||
|
|
||
| This experimental feature enables automatic optimization of SVG assets using [SVGO](https://svgo.dev/) during build time. | ||
|
|
||
| When enabled, all imported SVG files 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, add the experimental flag in your Astro config: | ||
|
|
||
| ```js title="astro.config.mjs" ins={4-8} | ||
| import { defineConfig } from "astro/config" | ||
|
|
||
| export default defineConfig({ | ||
| experimental: { | ||
| svg: { | ||
| optimize: true | ||
| } | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| ### `optimize` | ||
|
|
||
| **Type:** `boolean`<br /> | ||
| **Default:** `true` | ||
|
|
||
| Whether to enable SVG optimization using SVGO during build time. | ||
|
|
||
| When enabled, all imported SVG files will be optimized for smaller file sizes and better performance while maintaining visual quality. | ||
|
|
||
| ```js title="astro.config.mjs" | ||
| export default defineConfig({ | ||
| experimental: { | ||
| svg: { | ||
| optimize: true | ||
| } | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| ### `svgoConfig` | ||
|
|
||
| **Type:** `Config` (SVGO configuration object)<br /> | ||
| **Default:** `{}` | ||
|
|
||
| Configuration object passed directly to SVGO for customizing SVG optimization. | ||
|
|
||
| See [SVGO documentation](https://svgo.dev/docs/preset-default/) for available options and plugins. | ||
|
|
||
| ```js title="astro.config.mjs" | ||
| export default defineConfig({ | ||
| experimental: { | ||
| svg: { | ||
| optimize: true, | ||
| svgoConfig: { | ||
| plugins: [ | ||
| 'preset-default', | ||
| { | ||
| name: 'removeViewBox', | ||
| active: false | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| Once enabled, SVG optimization will automatically apply to all SVG files imported in your project: | ||
|
|
||
| ```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. | ||
|
|
||
| ## Common use cases | ||
|
|
||
| ### Preserve specific attributes | ||
|
|
||
| You may want to preserve certain SVG attributes that SVGO removes by default: | ||
|
|
||
| ```js title="astro.config.mjs" | ||
| export default defineConfig({ | ||
| experimental: { | ||
| svg: { | ||
| optimize: true, | ||
| svgoConfig: { | ||
| plugins: [ | ||
| 'preset-default', | ||
| { | ||
| name: 'removeViewBox', | ||
| active: false // Preserve viewBox attribute | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| ### Remove specific elements | ||
|
|
||
| Remove unwanted elements like metadata or hidden layers: | ||
|
|
||
| ```js title="astro.config.mjs" | ||
| export default defineConfig({ | ||
| experimental: { | ||
| svg: { | ||
| optimize: true, | ||
| svgoConfig: { | ||
| plugins: [ | ||
| 'preset-default', | ||
| { | ||
| name: 'removeHiddenElems', | ||
| active: true | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| ### Custom precision | ||
|
|
||
| Control the precision of numeric values in path data: | ||
|
|
||
| ```js title="astro.config.mjs" | ||
| export default defineConfig({ | ||
| experimental: { | ||
| svg: { | ||
| optimize: true, | ||
| svgoConfig: { | ||
| floatPrecision: 2 | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| ## Error handling | ||
|
|
||
| If SVGO optimization fails for any reason, Astro will gracefully fall back to using the original, unoptimized SVG content. A warning will be logged to the console, but your build will continue without errors. | ||
|
|
||
| ## Performance considerations | ||
|
|
||
| SVG optimization happens during the build process, not at runtime. This means: | ||
|
|
||
| - **Development:** SVGs are not optimized during development for faster rebuild times | ||
sarah11918 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - **Production:** All SVGs are optimized once during the build | ||
| - **No runtime overhead:** Optimized SVGs are served as static assets | ||
|
|
||
| The optimization process can increase build times slightly, but results in smaller file sizes and faster page loads for your users. | ||
|
|
||
| ## Migration notes | ||
sarah11918 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| This feature is experimental and may have breaking changes in future versions. When this feature becomes stable: | ||
|
|
||
| - The `experimental.svg` configuration will move to a top-level `svg` configuration | ||
| - Default behavior may change based on community feedback | ||
|
|
||
| ## Further reading | ||
|
|
||
| - [SVGO documentation](https://svgo.dev/) | ||
| - [SVGO preset-default plugins](https://svgo.dev/docs/preset-default/) | ||
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.