# Astro integration

The Astro integration builds the favicon set from src/logo.svg, serves it in dev, emits it with your client assets and adds the link tags to every page that needs them.

## Set up

Put your SVG at `src/logo.svg`, then run this from your Astro project:

```sh
npx astro add favseed
```

Astro installs the package and adds `import favseed from 'favseed'` with `integrations: [favseed()]`. Edit that call to configure another input or other options.

The package's default export is the Astro integration; named library imports such as `build` and `defineConfig` behave as before. A manual installation can use the explicit `favseed/astro` entry:

```js
// astro.config.mjs
import { defineConfig } from 'astro/config'
import favseed from 'favseed/astro'

export default defineConfig({
  integrations: [favseed({ input: 'src/logo.svg' })],
})
```

## How it works

The integration is [`favseed/vite`](https://favseed.cevdet.ch/docs/vite) plus head injection. In dev the icons are served from memory, and a build emits them with your client assets.

A file you keep in `public/` under the same name wins in dev and fails the build, so nothing of yours is ever replaced. The `<link>` tags go into every page that does not already have a `<link rel="icon">`, with `config.base` prefixed for you.

## Options

`sizes` is an array, and `vars` accepts an object or a `Map`. Astro controls `base` and `out`, so set those through Astro's configuration rather than in this call. A `favseed.config.*` in the project root is read too; options passed here win over it.

| Option | Default | Notes |
|---|---|---|
| `input` | `src/logo.svg` | Path to the mark, relative to the project root. |
| `effort` | `'default'` | `'fast'`, `'default'` or `'max'`; see [Effort and pinned settings](https://favseed.cevdet.ch/docs/cli#effort-and-pinned-settings). Dev always uses `'fast'`. |
| `quality` | none | Pins a PNG quality from 1 to 100 at the full palette: one encode, no search. Below 100 cannot be combined with `colors`. |
| `colors` | none | Pins a palette of 2 to 256 colours at quality 100: one encode, no search. |
| `sizes` | `[192, 512]` | Even pixel sizes for padded maskable PNGs. |
| `bg` | `'#000000'` | Ground for padded icons. `null` or `'none'` keeps every PNG transparent and the numbered icons unpadded; `apple-touch-icon.png` still sits in the safe zone. |
| `padding` | `'auto'` | Measured per mark, or a fixed 0 to 45% per side. |
| `vars` | none | CSS custom properties as an object or `Map`. |
| `animation` | `true` | Preserve animation; `false` removes it from `icon.svg`. |
| `manifest` | `false` | `true` writes icons only; an object merges app metadata. |
| `head` | `'inject'` | Automatic injection (`true` is an alias), `Head.astro`, or manual links (`false` logs them). |

```js
favseed({
  input: 'src/logo.svg',
  sizes: [192, 512],
  vars: { brand: '#0E7C68' },
  bg: '#0E1C28',     // the padded icons' ground; also a good background_color below
  manifest: { name: 'Example', short_name: 'Ex', theme_color: '#0E7C68', background_color: '#0E1C28' },
  head: 'inject',    // true | 'inject' | 'component' | false
})
```

## Validation

Options are validated when the integration is created, including for `sync` and `preview`. Typos, unsupported options and invalid build values fail before generation. Set paths with Astro's `base` and build output (`outDir`, `build.client`) rather than passing `base` or `out` to favseed.

Checks that need the renderer, such as background opacity or unresolved SVG variables, still run during generation. They warn in development and fail production builds, with advice written in the `bg`, `vars` and `animation: false` configuration syntax.

## Caching

The integration does not rebuild on every dev-server restart. It builds the icons on the first request for one, at `effort: 'fast'` and every configured size, and keeps them in a content-addressed cache.

The cache key includes favseed's version, the input bytes, the canonicalised options, and the identities of `@napi-rs/image`, SVGO and the Zopfli build, including its WASM hash. Each of these can change the generated bytes, so a cache keyed only on the SVG could return stale files after an upgrade. The effort and any pinned setting are part of the key, so a dev set never stands in for a build's.

The cache keeps eight recently used complete sets with verified integrity metadata, the current one included. It leaves staging directories, symlinks, modified or unrecognised entries, and older slots without integrity metadata alone. An evicted set is rebuilt when needed.

## Incremental builds

With Astro 7's `experimental.incrementalBuild` enabled, changing the generated head links or automatic injection invalidates cached HTML. Eligible pages stay reusable while those head settings are unchanged, including when only the icon bytes change. `head: true` and `head: 'inject'` are equivalent and behave the same here.

## Loading on demand

Build dependencies load on demand. The explicit `favseed/astro` entry avoids the Node build module during configuration, `sync` and `preview`. The package-root entry also loads the named library API, but neither entry initialises SVGO or the native engine on import.

CLI help, version and early option validation skip SVGO, and the synchronous SVG helper initialises it on first use. None of this changes the generated icons or the cache identity.

## Head tags

Astro has no official head-injection hook. All four `injectScript` stages are JavaScript, and a `<link rel="icon">` written by a script is found after the browser has already asked for `/favicon.ico`.

So `head: 'inject'`, the default, is an `order: 'post'` middleware that splices the tags in before `</head>` and skips any page whose head already has a `<link rel="icon">`. It buffers at most 64 KiB until the closing head tag, then streams the rest of the response. If the closing head tag is missing or comes after that limit, it passes the HTML through unchanged; use the component or manual tags for those pages.

`head: 'component'` gives you `favseed/astro/Head.astro` to place yourself, and `head: false` logs the block for pasting. `head: true` is an alias for `'inject'`.

## Migrating an Astro starter

Delete the starter's `public/favicon.ico` and `public/favicon.svg` if you want favseed's icons, and remove their old `<link rel="icon">` tags from your layout or pages. An existing icon declaration makes automatic injection skip the page.

If you keep manual tags, update them to favseed's generated URLs and choose `head: 'component'` or `head: false` as appropriate.
