Skip to content

Commit 331c92c

Browse files
authored
docs: update next documentation (#829)
The new installation instructions are for a more advanced configuration than originally appeared in these docs, but the original did not actually work in Next 13, and the workaround took nearly as much code, so I've stuck with the slightly longer config here.
1 parent 35ecbb5 commit 331c92c

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

website/pages/docs/next.mdx

+41-8
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,42 @@ yarn add --dev @svgr/webpack
2121

2222
## Usage
2323

24-
Using SVGR into Next.js is possible by configuring `@svgr/webpack`.
24+
Using SVGR in Next.js is possible with `@svgr/webpack`.
2525

2626
**next.config.js**
2727

2828
```js
2929
module.exports = {
3030
webpack(config) {
31-
config.module.rules.push({
32-
test: /\.svg$/i,
33-
issuer: /\.[jt]sx?$/,
34-
use: ['@svgr/webpack'],
35-
})
31+
// Grab the existing rule that handles SVG imports
32+
const fileLoaderRule = config.module.rules.find((rule) =>
33+
rule.test?.test?.(".svg")
34+
);
3635

37-
return config
36+
config.module.rules.push(
37+
// Reapply the existing rule, but only for svg imports ending in ?url
38+
{
39+
...fileLoaderRule,
40+
test: /\.svg$/i,
41+
resourceQuery: /url/, // *.svg?url
42+
},
43+
// Convert all other *.svg imports to React components
44+
{
45+
test: /\.svg$/i,
46+
issuer: /\.[jt]sx?$/,
47+
resourceQuery: { not: /url/ }, // exclude if *.svg?url
48+
use: ["@svgr/webpack"],
49+
},
50+
);
51+
52+
// Modify the file loader rule to ignore *.svg, since we have it handled now.
53+
fileLoaderRule.exclude = /\.svg$/i;
54+
55+
return config;
3856
},
39-
}
57+
58+
// ...other config
59+
};
4060
```
4161
4262
**Your code**
@@ -51,4 +71,17 @@ const Example = () => (
5171
)
5272
```
5373
74+
Or, using the classic (URL) import:
75+
76+
```js
77+
import Image from 'next/image'
78+
import starUrl from './star.svg?url'
79+
80+
const Example = () => (
81+
<div>
82+
<Image src={starUrl} />
83+
</div>
84+
)
85+
```
86+
5487
Please refer to [SVGR webpack guide](/docs/webpack/) for advanced use cases.

0 commit comments

Comments
 (0)