Astro setup
Set svgworm up in an Astro site. Icons render at build time, so a static site ships no JavaScript for them. There is no React and no @astrojs/react: the component is a plain .astro file that init generates in your project (ADR-0017).
Requires Node ^22.20 || >=24.11 and Astro 7.
Steps
-
Put your SVG files in
icons/. -
Install and set up:
pnpm add -D svgworm pnpm svgworm initinitsees theastrodependency and writessvgworm.config.json, thepredev/prebuildscripts andsrc/components/Icon.astro. With--yesit asks nothing. Use--framework astrowhen the project has noastrodependency yet. -
Build the sprite:
pnpm svgwormThis writes the sprite to
public/icons/and the manifest and types tosvgworm/, the same files as in every other setup. Astro servespublic/as is. -
Run the CLI before
devandbuild:{ "scripts": { "predev": "svgworm", "prebuild": "svgworm" } }While you work,
svgworm watchrebuilds on each change, next toastro dev. -
Use it in any
.astrofile:--- import Icon from '../components/Icon.astro'; --- <nav> <Icon name="home" /> <Icon name="brand/sunset" width={32} height={32} /> <Icon name="home" title="Go home" /> </nav>nameis typed from the generatedIconName, so a typo failsastro check. The icon isaria-hiddenunless you passtitleorlabel.
Icon.astro is your file. init never overwrites it. To refresh it after upgrading svgworm, delete it and run svgworm init again.
Cache for a year
The file names are content-hashed, so a long cache is safe. Astro does not set cache headers; your host does.
| Host | File | Rule |
|---|---|---|
| Cloudflare, Netlify | public/_headers |
/icons/* then Cache-Control: public, max-age=31536000, immutable |
| Vercel | vercel.json |
{ "headers": [{ "source": "/icons/(.*)", "headers": [{ "key": "Cache-Control", "value": "public, max-age=31536000, immutable" }] }] } |
base
This was checked with a build of this site with base: '/docs': public/icons/ lands in the dist/ root and /icons/… URLs are left unchanged, so they only work when the host serves dist/ under /docs/.
CI
Run svgworm check before the build. It compares the committed output with a fresh compile, so it catches a forgotten rebuild only on files as committed:
pnpm svgworm check
pnpm astro build
Working example
This website is an Astro 7 static site that renders every icon through the generated Icon.astro; its source is in the repo. CI checks that every icon comes from the hashed sprite (the site itself may use client JS; only what svgworm generates must not need it), and a test renders Icon.astro and createIcon from the same manifest and compares the markup.
Next
- CLI reference: every command, flag and exit code.
- Config:
svgworm.config.jsonand per-icon color overrides. - Using Next.js? Read the Next.js setup.