unplugin-icons icon indicating copy to clipboard operation
unplugin-icons copied to clipboard

How to use with Next.js ?

Open MatteoGauthier opened this issue 2 years ago • 8 comments

MatteoGauthier avatar Oct 19 '21 22:10 MatteoGauthier

I'm trying to use it with Next.js following the directions from the Configuration section (Webpack and React) and what I got:

Screen Shot 2021-10-29 at 15 58 51

maybe I missing something 🤔, check out my repo (webpack config)

iamyuu avatar Oct 29 '21 09:10 iamyuu

Getting the same issue with [email protected] unfortunately. Almost identical context as @iamyuu webpack config. Bumping for visibility.

Knogobert avatar Nov 16 '21 19:11 Knogobert

Also same issue and I find one #46

after reading , still have no idea how to solve this

enpitsuLin avatar Dec 23 '21 06:12 enpitsuLin

Thanks to @enpitsuLin 's comment, I managed to make it works, you will need to:

  • Set the following config into next.config.js:
module.exports = {
  reactStrictMode: true,
  webpack(config) {
    config.plugins.push(
      require('unplugin-icons/webpack')({
        compiler: 'jsx',
        jsx: 'react',
        extension: 'jsx',
      }),
    );

    return config;
  },
};
  • Add .jsx to every icon you import, in order, next.js knows how to load the icon
import IconAccessibility from "~icons/carbon/accessibility.jsx";
import IconAccountBox from "~icons/mdi/account-box.jsx";

I not sure whether this is a desired behavior or it should be changed, but this is how it work with next.js

leosuncin avatar Dec 26 '21 23:12 leosuncin

Ah, we just need to add the extension (.jsx), check this commit https://github.com/iamyuu/nextjs-unplugin-icons-repro/commit/a1a62f766931d6629e4e609b3dc96e55569d0d79

and voila Screen Shot 2021-12-27 at 08 32 26

iamyuu avatar Dec 27 '21 01:12 iamyuu

Had to update from next@10 to next@12 for it to work. Now it runs nicely when using .jsx appended to the imported icons as @leosuncin did above. Thanks for the repo @iamyuu, that helped finding out why.

Knogobert avatar Jan 03 '22 16:01 Knogobert

Do you guys also know how to make the Auto Importing feature work with Next.js ? Or is it not technically possible ?

HassanZahirnia avatar Feb 07 '22 08:02 HassanZahirnia

Do you guys also know how to make the Auto Importing feature work with Next.js ? Or is it not technically possible ?

Haven’t tried myself yet, but I think it has something to do with getting the icons for SSR during build-time being quite tricky. Saw some relevant thoughts on it in Anthony's discord in #unplugin-icons

Probably not the best way to paste this here, but at least you can see it without discord: image

Hope it nudges you in the right direction. Please tell us if it worked or not and what you did 😃

Knogobert avatar Feb 07 '22 10:02 Knogobert