Astro setup
Set svgworm up in an Astro site in three steps. Icons render at build time, so a static site ships no JavaScript for them. You need no React and no @astrojs/react. The Icon component is a plain .astro file that init writes into your project.
Requires Node ^22.20 || >=24.11 and Astro 7.
Steps#
-
Install and set up:
pnpm add -D svgworm && pnpm svgworm init # npm: npm install -D svgworm && npx svgworm init # yarn: yarn add -D svgworm && yarn svgworm initinitsees theastrodependency and does the setup for you:- Files: writes
svgworm.config.json, thepredev/prebuildscripts andsrc/components/Icon.astro. Then it builds once. - Sample icon: if
icons/is missing or empty, it addsicons/svgworm.svgso the first build works. - Flags: with
--yesit asks nothing. Use--framework astrowhen the project has noastrodependency yet.
See the CLI reference for every flag.
- Files: writes
-
Put your SVG files in
icons/(delete the sample) and build: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. While you work, runsvgworm watchnext toastro devto rebuild on each change. -
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>- Names come from the file paths:
homeisicons/home.svgandbrand/sunsetisicons/brand/sunset.svg, lower-cased with-between words. - Type-checked:
nameis typed from the generatedIconName, so a typo failsastro check. - Decorative by default: the icon is
aria-hiddenunless you passtitleorlabel.
- Names come from the file paths:
astro check needs @astrojs/check and typescript as dev dependencies (pnpm add -D @astrojs/check typescript).
As of Astro 7.3, astro check supports TypeScript 6 only. With TypeScript 7 it stops with astro check does not currently support TypeScript 7.0. Pin typescript@6 until Astro adds support. The generated types themselves work with both.
Icon.astro is your file, and init never overwrites it. To refresh it after upgrading svgworm, delete it and run svgworm init again.
Set up by hand#
Without init, run the CLI before dev and build yourself:
{ "scripts": { "predev": "svgworm", "prebuild": "svgworm" } }
Yarn 2+ never runs pre scripts. With it, put the command in front of the scripts instead: "dev": "svgworm && astro dev" and "build": "svgworm && astro build".
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.
-
Cloudflare and Netlify, in
public/_headers:/icons/* Cache-Control: public, max-age=31536000, immutable X-Content-Type-Options: nosniff -
Vercel, in
vercel.json:{ "headers": [ { "source": "/icons/(.*)", "headers": [ { "key": "Cache-Control", "value": "public, max-age=31536000, immutable" }, { "key": "X-Content-Type-Options", "value": "nosniff" } ] } ] }
Performance notes#
- One sprite, every page. All icons live in one sprite, plus one stack file for gradient and mask icons. The first visit downloads the whole set once, then the long cache serves every other page from disk. That is fine at tens of KiB: this site’s sprite is 11.5 KiB for 23 icons, about 0.5 KiB per optimized icon. With a thousand icons the sprite is the biggest asset on the page, so only the icons you use belong in the source folder. The full Lucide set (1,866 icons) is 539 KiB, 75 KiB gzipped. To split a large set by area, build one sprite per config.
- The stack file is one fetch per cold load when it is served with the
immutableheader above, in Chromium, Firefox and WebKit. WebKit fetches it twice only when a mask icon asks for it while the fetch for an<img>icon is still in flight. Earlier notes of 2 to 3 requests were mostly cache hits (ADR-0019, measured in TSK-039). - Do not add
<link rel="preload">for the sprite. A<use>fetch issame-origin, and no preload has that mode. So the hint never feeds it from the preload cache. With the preload still in flight, Playwright WebKit 26 downloads the sprite twice (real Safari not checked). Without a long cache, every engine does. Chromium and WebKit start the<use>fetch at parse time anyway. Details and the browser matrix: ADR-0019, TSK-039 matrix.Icon.astroemits no preload either.
base#
Why: Astro copies public/icons/ to the root of dist/, and your host serves dist/ under /docs/. So the sprite lives at /docs/icons/…. Without publicPath, the manifest still points at /icons/… and every icon 404s. We checked this with a build of this site under base: '/docs'.
Sprites on a CDN#
build.assetsPrefix does not move public/, so the sprite stays on your origin.
To serve it from another origin anyway, see Sprites on a CDN. svgworm init --cdn-url <https-url> also writes SvgwormLoader.astro. Render <SvgwormLoader /> once in your layout’s <head>. With the <ClientRouter />, the loader puts the sprite back after each page swap.
What to commit#
Two choices. Pick one and stick with it:
- Commit
public/icons/andsvgworm/and runsvgworm checkin CI. Every checkout passesastro checkwithout a build step. Reviews show icon changes as diffs. CI fails when someone edits an icon and forgets to rebuild. - Ignore both folders (
public/icons/andsvgworm/in.gitignore) and rely on thepredevandprebuildscripts. Less noise in reviews. But anything that runs beforeastro build, such asastro checkin CI, must runsvgwormfirst. Otherwise it will not findsvgworm/icons.d.ts.
CI#
Run svgworm check before the build. It compiles the icons and compares the result with the committed output. CI fails when someone changed an icon and forgot to rebuild:
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 (source). CI and tests check two things:
- Sprite: CI checks that every icon comes from the hashed sprite.
- Markup: a test checks that
Icon.astroand the ReactIconrender the same markup from the same manifest.
Next steps#
- Icon component: every prop, the markup per delivery, sizing and coloring with CSS.
- How it works: the sprite, the stack, delivery forms, the manifest and determinism.
- CLI reference: every command, flag and exit code.
- Config:
svgworm.config.jsonand per-icon color overrides. - Troubleshooting: blank icons, 404s, wrong colors, collisions,
checkin CI. - Using Next.js? Read the Next.js setup. Using Vite? Read the Vite setup.