Replies: 4 comments 5 replies
-
| 
         I think in this case you wanna do a sort of, inversion of control pattern (pardon the bad naming): const StyledIcon = ({ icon, size, color }) => {
  const classname = getStyles({size, color});
  
  const Icon = icon // or in the parameters: ({ icon: Icon, size, color}) => etc etc
   
  return <Icon className={className} />;
}So now your component is agnostic to the SVG injected in, AKA, the list of SVG icons doesn't have to be included here. Instead you can import them where you need them. // Purchase.tsx
import { Cart } from 'somewhere-in-the-project';
<Button><StyledIcon icon={Cart} size="small" color="primary" /> Purchase</Button>You kind of give up a bit of control, because, there's no telling what type of icons end up in  Alternatively,   | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         Yes, we have the same idea. Are there any best practices for using svg in nextjs. I recently tried using  Found that someone had tried it before, there was an npm package called  However, the vercel website also uses svgr, filling svg directly into html. I've been trying to figure this out for hours. I'm starting to see things now. Why didn't vercel use svg sprite? And  
 So in fact, the simpler the better In fact, I think sprite has another solution that should consistently outperform  However, coding the webpack plugin will become more difficult. It should work in theory. Look at vue's svg scheme, except for  ref: https://github.com/unplugin/unplugin-icons/blob/main/examples/README.md  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         try with "@svgr/webpack" and in next.config add   webpack: (config, options) => {
    config.module.rules.push({
      test: /\.svg$/,
      use: [
        options.defaultLoaders.babel,
        {
          loader: "@svgr/webpack",
          options: {
            icon: true,
          },
        },
      ],
    });
    return config;
  }, | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         Had the exact same issue with our SVGs. Our solution was to use sprites. And the sprites would be 'per page' instead of having them ALL in a single sprite. Our icon signature is   | 
  
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Hi,
We use Next.js in SSG mode to generate a static site. We have a lot of SVG icons in our application. We use
svgrconfiguration to include them as React components. And we have a genericIconcomponent with anameprops to include icons in our pages an easy way.Something like this :
But this way all icons are bundled in JS files so app weight comes heavy.
Is there a way to have tree shaking for this and have only the icons included in every page ?
I also thought about using SVG as standard images and using Next.js Image component, but it makes it more complicated to deploy icons (we want to share these icons as a NPM library) and I am not sure about performance (many requests).
Any good practice about SVG here ?
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions