Is it possible to import SVGs from a shared package in next 15?
In Next 14, I was able to import SVG files just fine as
StaticImageData
. After upgrading to Next 15, the only way I was able to get it to work was by adding this to my next config
This works fine, but in my PNPM monorepo I do some imports like this:
This no longer works and gives me this error:
How is this supposed to work? I saw a similar issue here but its slightly unrelated.GitHub
@svgr/webpack Not working with svg's from shared package inside mon...
🐛 Bug Report Inside my monorepo I have a NextJS project and a shared 'icons' package which is just a folder full of svg files. Whenever I want to use @svgr/webpack with the below next.confi...
2 Replies
Try to tweak your next config
Try this:
* We're using
oneOf
to apply different rules to SVG imports.
* issuer
: Checks where the SVG is being imported from. This lets us treat your monorepo source files differently.
* resourceQuery
: It checks if the svg import ends with ?url
.
* The first rule in oneOf
uses @svgr/webpack
for your monorepo's source files (during development). You'll need to install it: pnpm add @svgr/webpack -D
* The second rule in oneOf
handles svg imports, when svg files end with ?url
.
* The third handles all other cases, using Next.js image features.
* fileLoaderRule.exclude
: We exclude your monorepo's source directory from the original SVG rule, so Next.js's default handling doesn't interfere.
If you're using TypeScript, add this to global.d.ts
:
After making these changes delete your .next
folder.wish i had seen this sooner. funily enough ended up with basically the same solution