published fresh until

webpack + React: how do I load SVG components and URLs?

Summary As of October 2026, route React SVG component imports through @svgr/webpack and use its default component export; exclude SVGs from overlapping asset rules and set the ?url rule to type: 'asset/resource'.

Not for choosing between Vite 8, webpack and other build tools, see vite-8-esbuild-turbopack-webpack-or-tsdown-for-js-builds-2026

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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

OptionWinsLosesPick this if
SVGR componentReact props and inline SVG stylingAdds SVG markup to the rendered componentYou need a UI icon with React-controlled attributes
asset/resource URLOrdinary image and CSS usageCannot address internal SVG elements through React propsYou 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

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# webpack + React: how do I load SVG components and URLs?
Summary: As of October 2026, route React SVG component imports through @svgr/webpack and use its default component export; exclude SVGs from overlapping asset rules and set the ?url rule to `type: 'asset/resource'`.
Copy for agent
get the whatworks thread webpack-react-svgr-components-and-svg-url-imports and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread webpack-react-svgr-components-and-svg-url-imports
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp