-
-
Notifications
You must be signed in to change notification settings - Fork 421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@svgr/webpack does not work with Next.js #897
Comments
And for extended details. Selections when creating next app✔ Would you like to use TypeScript? … Yes Full version of
|
Same problem |
Same problem here |
@jarimustonen @Karina1703 I've found the solution in case you still need it |
Thank you. To state the obvious: This fixed version should be used here: https://react-svgr.com/docs/next/ |
Feel free to submit a PR to update the doc please! |
I know the fix works but I don't understand it. Anyway, I did make a pull request for it. Here: #898 |
FWIW I just ran into this issue with a brand new nextjs install (first time trying it out). The instructions at https://react-svgr.com/docs/next/ didn't work for me, but cribbing from there I figured out this solution: /** @type {import('next').NextConfig} */
const nextConfig = {
webpack(config) {
// grab the default rule for handling all images
const fileLoaderRule = config.module.rules.find((rule) => rule.test?.test?.('.svg'))
config.module.rules = [
// keep all rules except the default image loader rule
...config.module.rules.filter((rule) => rule !== fileLoaderRule),
// re-add the default image loader rule, but exclude svg
{
...fileLoaderRule,
exclude: /\.svg$/i,
},
// add a new rule for svg files, excluding svg files that are imported as React components
{
...fileLoaderRule,
test: /\.svg$/i,
resourceQuery: {
...fileLoaderRule.resourceQuery,
not: [
...fileLoaderRule.resourceQuery.not,
/component/, // *.svg?component
],
},
},
// add a new rule for svg files that are imported as React components
{
test: /\.svg$/i,
issuer: /\.[jt]sx?$/,
use: '@svgr/webpack',
resourceQuery: /component/, // *.svg?component
},
]
return config
},
}
module.exports = nextConfig With the above config, importing an SVG as a component is opt-in, using ?component on the path, so you can still use regular SVG imports with Next's Image component if you want. import PngImage from './some.png'
import JpegImage from './some.jpg'
import GifImage from './some.gif'
import SvgImage from './some.svg'
import SvgComponent from './some.svg?component'
export default function App() {
return (
<main>
<Image src={PngImage} alt="PNG Image" />
<Image src={JpegImage} alt="JPEG Image" />
<Image src={GifImage} alt="GIF Image" />
<Image src={SvgImage} alt="SVG Image" />
<SvgComponent />
</main>
)
} Finally, if you're using TypeScript, you'll want to declare a module for this import type. I put this in src/types.ts: declare module '*.svg?component' {
import { FC, SVGProps } from 'react'
const content: FC<SVGProps<SVGElement>>
export default content
} Doing that gets you nice autocomplete for passing SVG props to your SVG components: Original commentUpdate: erg I just discovered that the setup I described below breaks non-component SVG imports. Will update again when I have a fix. FWIW I just ran into this issue with a brand new nextjs install (first time trying it out). The instructions at https://react-svgr.com/docs/next/ didn't work for me, but cribbing from there I figured out this solution: // next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
webpack(config) {
config.module.rules.push({
test: /\.svg$/i,
issuer: /\.[jt]sx?$/,
use: ['@svgr/webpack'],
resourceQuery: /component/, // *.svg?component
})
return config
},
}
module.exports = nextConfig So importing an SVG as a component is opt-in, using ?component on the path, leaving the existing NextJS SVG rules alone. import SomeIcon from './some-icon.svg?component'
export default App() {
return (
<SomeIcon/>
)
} Finally, if you're using TypeScript, you'll want to declare a module for this import type. I put this in src/types.ts: declare module '*.svg?component' {
import { FC, SVGProps } from 'react'
const content: FC<SVGProps<SVGElement>>
export default content
} Doing that gets you nice autocomplete for passing SVG props to your SVG components: |
Do you have svgs working in server components? I just have such a mistake: Uncaught Error: Unsupported Server Component type: {...} |
have you had problems with server components? |
I was also having a nightmare with this. I've just started a new Material UI project, using the Next.js App router. None of the code examples in official documentation [SVGR docs, MUI docs] seem to work with this software stack. I was trying to use MUI's A couple of GitHub Issue comments pointed me in the right direction:
These suggest adding those various changes to However, following the MUI docs, I used the code
Anyway, what fixed it was the recommendation in the next.js docs:
For example //layout.tsx
import * as React from 'react';
import SvgIcon from '@mui/material/SvgIcon';
import MyIcon from '/public/images/my-icon.svg';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<SvgIcon>
<MyIcon />
</SvgIcon>
</body>
</html>
) Next steps:
I'm not sure if there's anything that could be done within @svgr/webpack to help simplify this, or if it's just an issue with the various documentation online? |
I was facing the same issue, figured out it was because I was using turbopack (via |
For those struggling with SVGR in next 14+ with Typescript, here's what worked for me:
declare module '*.svg' {
import { FC, SVGProps } from 'react'
const content: FC<SVGProps<SVGElement>>
export default content
}
declare module '*.svg?url' {
const content: any
export default content
}
"include": [
"svgr.d.ts",
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
], You should now have everything working with VSCode autocomplete, e.g.: For configuration of svgr, add a config file to the root (e.g. {
"dimensions": false,
"svgoConfig": {
"plugins": [
"removeDimensions",
{
"name": "convertColors",
"params": {
"currentColor": true
}
},
{
"name": "preset-default",
"params": {
"overrides": {
"removeTitle": false
}
}
}
]
}
} |
🐛 Bug Report
@svgr/webpack
does not work withnext 13.4.19
. It generates error message:To Reproduce
Run the basic project creation:
npx create-next-app@latest
.Run
npm install --save-dev @svgr/webpack
Update
next.config.js
per https://react-svgr.com/docs/next/In the file
src/app/page.tsx
replace the next.svg reference withimport Logo from '../../public/next.svg';
and
<Logo />
Expected behavior
Expect the image to work.
Run
npx envinfo --system --binaries --npmPackages @svgr/core,@svgr/cli,@svgr/webpack,@svgr/rollup --markdown --clipboard
Paste the results here:
The text was updated successfully, but these errors were encountered: