Skip to content

Commit

Permalink
docs: update next documentation (#829)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fritz-c authored Feb 26, 2023
1 parent 35ecbb5 commit 331c92c
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions website/pages/docs/next.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,42 @@ yarn add --dev @svgr/webpack

## Usage

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

**next.config.js**

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

return config
config.module.rules.push(
// Reapply the existing rule, but only for svg imports ending in ?url
{
...fileLoaderRule,
test: /\.svg$/i,
resourceQuery: /url/, // *.svg?url
},
// Convert all other *.svg imports to React components
{
test: /\.svg$/i,
issuer: /\.[jt]sx?$/,
resourceQuery: { not: /url/ }, // exclude if *.svg?url
use: ["@svgr/webpack"],
},
);

// Modify the file loader rule to ignore *.svg, since we have it handled now.
fileLoaderRule.exclude = /\.svg$/i;

return config;
},
}

// ...other config
};
```
**Your code**
Expand All @@ -51,4 +71,17 @@ const Example = () => (
)
```
Or, using the classic (URL) import:
```js
import Image from 'next/image'
import starUrl from './star.svg?url'

const Example = () => (
<div>
<Image src={starUrl} />
</div>
)
```
Please refer to [SVGR webpack guide](/docs/webpack/) for advanced use cases.

1 comment on commit 331c92c

@vercel
Copy link

@vercel vercel bot commented on 331c92c Feb 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

svgr – ./

svgr-git-main-gregberge.vercel.app
svgr-gregberge.vercel.app
api.react-svgr.com

Please sign in to comment.