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#

  1. 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 init

    init sees the astro dependency and does the setup for you:

    • Files: writes svgworm.config.json, the predev/prebuild scripts and src/components/Icon.astro. Then it builds once.
    • Sample icon: if icons/ is missing or empty, it adds icons/svgworm.svg so the first build works.
    • Flags: with --yes it asks nothing. Use --framework astro when the project has no astro dependency yet.

    See the CLI reference for every flag.

  2. Put your SVG files in icons/ (delete the sample) and build:

    pnpm svgworm

    This writes the sprite to public/icons/ and the manifest and types to svgworm/, the same files as in every other setup. Astro serves public/ as is. While you work, run svgworm watch next to astro dev to rebuild on each change.

  3. Use it in any .astro file:

    ---
    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: home is icons/home.svg and brand/sunset is icons/brand/sunset.svg, lower-cased with - between words.
    • Type-checked: name is typed from the generated IconName, so a typo fails astro check.
    • Decorative by default: the icon is aria-hidden unless you pass title or label.

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 immutable header 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 is same-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.astro emits 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/ and svgworm/ and run svgworm check in CI. Every checkout passes astro check without 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/ and svgworm/ in .gitignore) and rely on the predev and prebuild scripts. Less noise in reviews. But anything that runs before astro build, such as astro check in CI, must run svgworm first. Otherwise it will not find svgworm/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.astro and the React Icon render 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.json and per-icon color overrides.
  • Troubleshooting: blank icons, 404s, wrong colors, collisions, check in CI.
  • Using Next.js? Read the Next.js setup. Using Vite? Read the Vite setup.

Edit this page on GitHub