Next.js Icons and SVG: Libraries, SVGR, next/image Setup
How to use icons and SVG files in Next.js App Router: icon packages and optimizePackageImports, SVGR for webpack and Turbopack, next/image with SVG, app icons.
On this page
In a Next.js App Router project, install an icon package such as lucide-react and import icons by name: Next.js already optimizes imports for Lucide, Heroicons, Tabler and react-icons. For your own SVG files, put them in public/ and use <img src="/logo.svg"> or next/image. Importing an .svg file as a React component needs SVGR, configured separately for webpack and Turbopack.
Quick answer
- Icon packages: lucide-react, @heroicons/react, @tabler/icons-react and react-icons are in Next.js's default optimizePackageImports list. Add @phosphor-icons/react yourself.
- Server Components: lucide-react icons can be rendered from a Server Component. Phosphor needs the @phosphor-icons/react/ssr import there.
- SVG in public/: a file at public/logo.svg is served at /logo.svg. next/image serves it unoptimized automatically when src ends in .svg.
- dangerouslyAllowSVG is only for sending SVG through the image optimizer. If you enable it, also set contentDispositionType and contentSecurityPolicy.
- SVG as a component: @svgr/webpack, with one config block for webpack and one for Turbopack.
Icon libraries in Next.js
Any React icon package works in Next.js. What differs is how much work Next.js does for you. The optimizePackageImports docs list packages that are optimized by default, so only the modules you import are loaded in dev and in the build:
| Package | Optimized by default | Server Component note |
|---|---|---|
lucide-react | yes | Import icons directly. |
@heroicons/react | yes (24/outline, 24/solid, 20/solid) | Import icons directly. |
@tabler/icons-react | yes | Import icons directly. |
react-icons/* | yes | Import icons directly. |
@mui/icons-material | yes | Import icons directly. |
@phosphor-icons/react | no, add it | Use @phosphor-icons/react/ssr in Server Components. |
For a package that is not on the list, add it in next.config.js, as the Phosphor README recommends:
// next.config.js
module.exports = {
experimental: {
optimizePackageImports: ['@phosphor-icons/react'],
},
};A typical App Router component:
// app/components/SearchButton.tsx (Server Component)
import { Search } from 'lucide-react';
export function SearchButton() {
return (
<button type="submit" aria-label="Search" className="inline-flex size-10 items-center justify-center">
<Search size={20} />
</button>
);
}lucide-react 1.x marks its base icon module with 'use client', so Next.js treats each icon as a Client Component that renders on the server and hydrates. That is fine for normal use. Phosphor's default entry uses React context, so in a Server Component import from @phosphor-icons/react/ssr instead; those icons ignore IconContext.
Avoid a wrapper that imports a whole package (import * as Icons from 'lucide-react') and looks icons up by string. That pulls every icon into the bundle. Pass the icon component as a prop instead.
Which icon library to use with Next.js
The choice is the same as for any React app. lucide-react is the safe default: about 1,890 icons, ISC license, and already optimized by Next.js. Pick Heroicons if you use Tailwind UI, Phosphor for six weights, or Tabler for the largest outline set. The comparison table with counts, licenses and imports is in our React icon libraries guide.
SVG files in public/
Files in public/ are served from the site root, per the public folder docs. A file at public/logo.svg is available at /logo.svg:
// Plain img: no config, cached by the browser
<img src="/logo.svg" alt="Acme" width={120} height={32} />// next/image: SVG is served as is, no resizing
import Image from 'next/image';
<Image src="/logo.svg" alt="Acme" width={120} height={32} />The Image component docs say Next.js does not optimize SVG by default, and that the unoptimized prop is applied automatically when src ends in .svg. So next/image adds little for an SVG beyond lazy loading and size attributes. A plain <img> is fine.
An SVG loaded through img or next/image cannot be recolored with CSS. For a logo that must follow the theme, inline it (SVGR below) and use fill="currentColor". How that works: change SVG icon color with CSS.
When you need dangerouslyAllowSVG
Only when an SVG goes through the image optimizer, for example a remote URL or an API route that does not end in .svg. SVG can contain scripts, so the docs recommend these settings together:
// next.config.js
module.exports = {
images: {
dangerouslyAllowSVG: true,
contentDispositionType: 'attachment',
contentSecurityPolicy: "default-src 'self'; script-src 'none'; sandbox;",
},
};Import SVG as a React component (SVGR)
Install the loader:
npm install --save-dev @svgr/webpackTurbopack. The Turbopack config docs use this rule:
// next.config.js
module.exports = {
turbopack: {
rules: {
'*.svg': {
loaders: ['@svgr/webpack'],
as: '*.js',
},
},
},
};Webpack. The SVGR Next.js guide keeps the default file loader for imports that end in ?url and sends every other .svg import to SVGR:
// next.config.js
module.exports = {
webpack(config) {
const fileLoaderRule = config.module.rules.find((rule) =>
rule.test?.test?.('.svg'),
);
config.module.rules.push(
{ ...fileLoaderRule, test: /\.svg$/i, resourceQuery: /url/ },
{
test: /\.svg$/i,
issuer: fileLoaderRule.issuer,
resourceQuery: { not: [...fileLoaderRule.resourceQuery.not, /url/] },
use: ['@svgr/webpack'],
},
);
fileLoaderRule.exclude = /\.svg$/i;
return config;
},
};Then:
import Logo from './logo.svg'; // React component
import logoUrl from './logo.svg?url'; // URL, webpack config above
<Logo className="h-8 w-auto text-zinc-900 dark:text-white" />Clean the file in the SVG optimizer first, and replace hard-coded fill colors with currentColor so the class sets the color.
Favicon and app icons
The App Router reads icon files from app/, per the app icons file conventions:
| File | Types | Location | Output |
|---|---|---|---|
favicon | .ico | app/ only | <link rel="icon" href="/favicon.ico" sizes="any" /> |
icon | .ico, .jpg, .jpeg, .png, .svg | app/**/* | <link rel="icon"> |
apple-icon | .jpg, .jpeg, .png | app/**/* | <link rel="apple-touch-icon"> |
So app/icon.svg gives you an SVG favicon with no metadata code. Keep favicon.ico for older browsers and add a PNG apple-icon, since it does not accept SVG. Sizes and tags: favicon sizes guide. To make all files from one logo, use the favicon generator.
FAQ
What is the best icon library for Next.js?
How do I import an SVG in Next.js?
What does dangerouslyAllowSVG do?
Can I use icon libraries in Server Components?
How do I add an SVG favicon in the App Router?
Related posts