Next.js App Router setup
Set svgworm up in a Next.js App Router app in three steps. You end up with:
- a hashed sprite in
public/icons/; - typed icon names;
- an
Iconcomponent that runs only on the server.
Requires Node ^22.20 || >=24.11 and React 19 (@svgworm/react has react ^19 as a peer), so Next.js 15 or later. On React 18 the package manager only warns about the peer, and nothing tests that setup.
Steps#
-
Install and set up:
pnpm add -D svgworm && pnpm add @svgworm/react && pnpm svgworm init # npm: npm install -D svgworm && npm install @svgworm/react && npx svgworm init # yarn: yarn add -D svgworm && yarn add @svgworm/react && yarn svgworm initinitdoes the setup for you:- Config: writes
svgworm.config.json. - Scripts: adds
predevandprebuildscripts that runsvgworm. - Component: creates
components/icon.tsx(src/components/icon.tsxwhensrc/exists). - First build: if
icons/is missing or empty, it adds a sampleicons/svgworm.svg. Then it builds once, so the app runs and type-checks right away.
With
--yesit asks nothing. It never overwrites a file that differs, so re-running it is safe. See the CLI reference for every flag. - Config: writes
-
Put your SVG files in
icons/(delete the sample) and build:pnpm svgwormpnpm devandpnpm buildrun it too, through thepredevandprebuildscripts. It writes the sprite topublic/icons/and the manifest and types tosvgworm/:File Contents public/icons/icons.<hash>.svgSprite (plus icons.stack.<hash>.svgwhen an icon needs it)svgworm/icons.jsManifest (data only) svgworm/icons.d.tsTypes: IconNameand the manifest shapesvgworm/manifest.jsonSame manifest, for scripts and templates -
Use it anywhere on the server:
import { Icon } from "@/components/icon"; export const Nav = () => ( <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. - Decorative by default: the icon is
aria-hiddenunless you passtitleorlabel. - Type-checked: an unknown
nameis a type error, so an editor lists every valid name.
- Names come from the file paths:
Client components#
Icon runs on the server. It reads the manifest and renders markup, with no hooks and no client JavaScript. Import it only from Server Components.
A file with 'use client' that imports Icon pulls the whole manifest into that client chunk: svgworm/icons.js, with every icon’s id, viewBox and delivery. That is about 200 bytes per icon before compression. A 1,500-icon set adds roughly 300 KB of JavaScript that the sprite was meant to avoid. The zero-client-JS check in the example catches this.
When a client component needs an icon, render it in the Server Component and pass the result down:
// app/page.tsx (server)
import { Icon } from "@/components/icon";
import { MenuButton } from "@/components/menu-button";
export default function Page() {
return <MenuButton icon={<Icon name="menu" />} />;
}
// components/menu-button.tsx
"use client";
import type { ReactNode } from "react";
export const MenuButton = ({ icon }: { icon: ReactNode }) => (
<button onClick={() => {}}>{icon} Menu</button>
);
The client component receives rendered markup, not the manifest. A client-only app (Vite, Create React App) has no server to keep the manifest on, so it ships in the bundle. See the Vite setup for the trade-off.
Set up by hand#
Without init, do its part yourself after installing the two packages:
-
Build the sprite from your icon folder:
pnpm svgwormWith no config file, svgworm reads the
icons/folder. For another folder, pass--source <dir>on every run, or createsvgworm.config.json(see Config). -
Create the component once, in a Server Component file:
// components/icon.tsx import { createIcon } from "@svgworm/react"; import { manifest } from "../svgworm/icons.js"; export const Icon = createIcon(manifest); -
Run the CLI before
devandbuild:{ "scripts": { "predev": "svgworm", "prebuild": "svgworm" } }Yarn 2+ never runs
prescripts. With it, put the command in front of the scripts instead:"dev": "svgworm && next dev"and"build": "svgworm && next build".
Cache for a year#
Next serves public/ with max-age=0. The file names are content-hashed, so a long cache is safe:
// next.config.ts
export default {
async headers() {
return [
{
source: "/icons/:file*",
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. React 19’s ownpreloadfor<img>icons is fine and stays.
basePath#
Next serves public/ under the basePath, so the sprite lives at /docs/icons/…. Without publicPath, the manifest still points at /icons/… and every icon 404s.
Sprites on a CDN#
assetPrefix does not move public/, so the sprite stays on your origin and needs nothing else.
To serve it from another origin anyway, see Sprites on a CDN. svgworm init --cdn-url <https-url> adds export const SpriteLoader = createSpriteLoader(manifest) to the component file. Render <SpriteLoader /> once in app/layout.tsx.
What to commit#
Two choices. Pick one and stick with it:
- Commit
public/icons/andsvgworm/and runsvgworm checkin CI. Every checkout type-checks without a build step. Reviews show icon changes as diffs of the sprite and the manifest. 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 beforenext build, such astscor ESLint in 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 (exit 1) when someone changed an icon and forgot to rebuild:
pnpm svgworm check
pnpm next build
Working example#
examples/next-app-router is a complete app with these steps. CI builds it with Turbopack and checks that no icon code ends up in client chunks.
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 Astro? Read the Astro setup. Using Vite? Read the Vite setup.