What this thread does
Use @svgr/webpack for SVG components and a separate ?url asset rule for URL imports, with mutually exclusive matching.
Setup recipe
Do first: exclude .svg from overlapping rules in your existing webpack configuration, including catch-all image rules and oneOf branches, before adding these rules. The SVGR webpack guide documents component imports and resourceQuery; checked 2026-10-28.
Install the stable loader:
npm install --save-dev @svgr/webpack@8.1.0
Merge the following into the existing webpack configuration. For an ESM config, use its existing export style instead of module.exports:
module.exports = {
module: {
rules: [
{
test: /\.svg$/i,
oneOf: [
{ resourceQuery: /url/, type: 'asset/resource' },
{
issuer: /\.[jt]sx?$/,
use: ['@svgr/webpack'],
},
{ type: 'asset/resource' },
],
},
],
},
};
This authored composition makes CSS SVG references use the URL fallback. Other rules outside this oneOf must still exclude SVGs. webpack's Asset Modules guide defines asset/resource as emitting a file and exporting its URL. Checked 2026-10-28.
Use both forms in a rendered component:
import Logo from './logo.svg';
import logoUrl from './logo.svg?url';
export function Example() {
return <>
<Logo width={24} height={24} aria-hidden="true" />
<img src={logoUrl} alt="Company logo" />
</>;
}
Run the project's normal webpack build. Inspect both the rendered icon and the image request; also check an SVG referenced from CSS. A successful compilation alone does not confirm every import has the right runtime shape.
For plain TypeScript webpack projects, add an included declaration file:
declare module '*.svg' {
import type { FunctionComponent, SVGProps } from 'react';
const Component: FunctionComponent<SVGProps<SVGSVGElement>>;
export default Component;
}
declare module '*.svg?url' {
const url: string;
export default url;
}
These declarations describe this configuration; they do not transform assets. Check them against the actual emitted values.
Pick the import that matches the job
| Option | Wins | Loses | Pick this if |
|---|---|---|---|
| SVGR component | React props and inline SVG styling | Adds SVG markup to the rendered component | You need a UI icon with React-controlled attributes |
| asset/resource URL | Ordinary image and CSS usage | Cannot address internal SVG elements through React props | You need an image resource rather than component markup |
Solo and small-team default: support both with the explicit ?url convention. Enterprise default: codify this in the shared build config, then test representative icons and CSS consumers before rolling it across packages.
Versions and cost, checked 2026-10-28
The SVGR releases page marks 8.1.0 latest; webpack's release page lists stable 5.112.2. This is setup on the existing webpack asset-module line, not an upgrade recipe. SVGR's licence is MIT. No hosted service is required; measure bundle and build cost on your own icons.
Public evidence
y-hsgw asked for “a stricter type definition” in SVGR issue 1012, 2025-09-09, concerning Next.js *.svg?url declarations. This supports checking framework-specific types; it does not prove a webpack runtime defect.
Agent prompt and mistakes
Inspect our current SVG rules. Add component and ?url imports without double processing. Build a page using both plus a CSS background. Show emitted types before changing declarations.
Do not copy Create React App's named ReactComponent import into this default-export setup. SVGR documents named exports when another loader owns the default export. Do not add deprecated file-loader or url-loader just to emit URLs.
Limits
No install, build or browser check was run. This is a custom webpack configuration; Next.js and other managed frameworks require their own integration. TypeScript declarations and accessibility need review against actual usage. Public evidence is one adjacent typing report.
Started from a post by @SVGR maintainers, packaged by the editor. Original post