Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions astro.sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export const sidebar = [
'reference/experimental-flags/static-import-meta-env',
'reference/experimental-flags/chrome-devtools-workspace',
'reference/experimental-flags/fail-on-prerender-conflict',
'reference/experimental-flags/svg-optimization',
],
}),
'reference/legacy-flags',
Expand Down
6 changes: 4 additions & 2 deletions src/content/docs/en/reference/cli-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,11 @@ Starts a local server to serve the contents of your static directory (`dist/` by

This command allows you to preview your site locally [after building](#astro-build) to catch any errors in your build output before deploying it. It is not designed to be run in production. For help with production hosting, check out our guide on [Deploying an Astro Website](/en/guides/deploy/).

Since Astro 1.5.0, the [Node adapter](/en/guides/integrations-guide/node/) supports `astro preview` for builds generated with on-demand rendering.
The following hotkeys can be used in the terminal where the Astro preview server is running:
- `o` + `enter` to open your Astro site in the browser.
- `q` + `enter` to quit the preview server.

Can be combined with the [common flags](#common-flags) documented below.
The `astro preview` command can be combined with the [common flags](#common-flags) documented below to further control the preview experience.

## `astro check`

Expand Down
1 change: 1 addition & 0 deletions src/content/docs/en/reference/error-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,4 @@ The following reference is a complete list of the errors you may encounter while
- [**SessionWithoutSupportedAdapterOutputError**](/en/reference/errors/session-without-supported-adapter-output-error/)<br/>Sessions cannot be used with an adapter that doesn't support server output.
- [**SessionConfigMissingError**](/en/reference/errors/session-config-missing-error/)<br/>Session storage was enabled but not configured.
- [**SessionConfigWithoutFlagError**](/en/reference/errors/session-config-without-flag-error/)<br/>Session flag not set
- [**CannotOptimizeSvg**](/en/reference/errors/cannot-optimize-svg/)<br/>Cannot optimize SVG
20 changes: 20 additions & 0 deletions src/content/docs/en/reference/errors/cannot-optimize-svg.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
# NOTE: This file is auto-generated from 'scripts/error-docgen.mjs'
# Do not make edits to it directly, they will be overwritten.
# Instead, change this file: https://github.com/withastro/astro/blob/main/packages/astro/src/core/errors/errors-data.ts
# Translators, please remove this note and the <DontEditWarning/> component.

title: Cannot optimize SVG
i18nReady: true
githubURL: https://github.com/withastro/astro/blob/main/packages/astro/src/core/errors/errors-data.ts
---
import DontEditWarning from '~/components/DontEditWarning.astro'

<DontEditWarning />


> An error occurred while optimizing the SVG file with SVGO.




179 changes: 179 additions & 0 deletions src/content/docs/en/reference/experimental-flags/svg-optimization.mdx
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/)
27 changes: 27 additions & 0 deletions src/content/docs/en/reference/modules/astro-actions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ import type {
ActionAPIContext,
ActionClient,
ActionErrorCode,
ActionInputSchema,
ActionReturnType,
SafeResult,
} from 'astro:actions';
Expand Down Expand Up @@ -546,6 +547,32 @@ button?.addEventListener('click', async () => {

A union type of standard HTTP status codes [defined by IANA](https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml) using the human-readable versions as uppercase strings separated by an underscore (e.g. `BAD_REQUEST` or `PAYLOAD_TOO_LARGE`).

### `ActionInputSchema`

<p>

**Type:** `ZodType`
<Since v="5.16.0" />
</p>

A utility type that automatically infers the TypeScript type of an action's input based on its Zod schema. This can be useful to reference an action's [`input` validator type](#input-validator) as an object in your own type definitions.

Returns `never` when [`input` validator](#input-validator) is omitted.

The following example uses `ActionInputSchema` on an action named `contact` to:
* Retrieve the Zod schema type for the input of the action.
* Retrieve the expected input type of the action's validator.

```astro title="src/components/Form.astro" {5}
---
import { actions, ActionInputSchema } from 'astro:actions';
import { z } from 'astro/zod';

type ContactSchema = ActionInputSchema<typeof actions.contact>;
type ContactInput = z.input<ContactSchema>;
---
```

### `ActionReturnType`

<p>
Expand Down