npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@steinjun0/nextjs-svg-embedder

v2.0.0

Published

Embedding SVGs in Next.js Server Side Rendered Pages

Downloads

4

Readme

Next.js SVG Embedder

A library to handle SVG as a <svg> tag not an <img> tag in Server Side Rendering.

Motivation

When using <Image> of next/image for svg image, we can't control svg attribute because it rendered with <img> tag. Commonly we use @svgr/webpack. Therefore, it increase build time with additional operation in webpack. Even there is few svg images.

Thanks to Next.js v13 app router, we can use server component comfortably which fully using server side functions.

nextjs-svg-embedder embed svg images with <svg> tags in redering time easily.

How to use

import SvgEmbedder from '@steinjun0/nextjs-svg-embedder';
import svgImage from 'path/of/svg/image.svg'; // only change attribute of <svg>
import vercelIcon from 'public/vercel.svg'; // should change attribute of <path>

export default function page(){
    return <div>
    <SvgEmbedder src={svgImage} fill="red" />
    
    {/* add and override path props */}
    <SvgEmbedder src={vercelIcon} pathProps={{fill:"red", fillOpacity: "50%"}}/> 
    
    {/* override existing props. fillOpacity doesn't change */}
    <SvgEmbedder src={vercelIcon} overrideProps={{fill:"red", fillOpacity: "50%"}}/> 
  </div>;
}

src

Give the imported image.

props

Use all props of React.SVGProps<SVGSVGElement>(fill, width, height,...) like common tag in React.

  <SvgEmbedder src={svgImage} fill="red" fillOpacity="50%" {...originProps}/>
  {/* Same code */}
  <svg fill="red" fillOpacity="50%" {...originProps}>
    {/* path of svgImage */}
  </svg>

pathProps

Set pathProps to set attribute of every <path>. This option will be useful when you can't edit original svg file. This update all <path> eqaully.

  <SvgEmbedder src={svgImage} pathProps={{fill:"red", fillOpacity:"50%"}}/>
  {/* Same code */}
  <svg {...originProps}>
    <path fill="red" fillOpacity="50%" {...originProps}/>
    {/* path of svgImage */}
  </svg>

overrideProps

Set overrideProps to override every attribute of not only every <path> but also <svg>. This option will be useful when you can't edit original svg file. This affects all <svg>, <path> equally.

(Recommend) Edit original svg

If you can edit original svg, it helps codes become more simple.

Here is vercel.svg which is in Next.js default project.

<svg width="283" height="64" viewBox="0 0 283 64" fill="none" 
    xmlns="http://www.w3.org/2000/svg">
    <path d="M141.04 16c-11.04 0-19 7.2-19 18s8.96 ... " fill="#000"/>
</svg>

There is fill="#000" in <path>, we should use override option.

import SvgEmbedder from '@steinjun0/nextjs-svg-embedder';
import vercelIcon from 'public/vercel.svg'; // should change attribute of <path>, <svg>

export default function page(){
    return <div>
    <SvgEmbedder src={vercelIcon} pathProps={{fill:"red"}} />
  </div>;
}

So, when remove fill in <path>, we don't need to use override option.

<svg width="283" height="64" viewBox="0 0 283 64" fill="none" 
    xmlns="http://www.w3.org/2000/svg">
    <path d="M141.04 16c-11.04 0-19 7.2-19 18s8.96 ... "/>
</svg>
import SvgEmbedder from '@steinjun0/nextjs-svg-embedder';
import vercelIcon from 'public/vercel.svg'; // no fill in path

export default function page(){
    return <div>
    <SvgEmbedder src={vercelIcon} fill="red" /> {/* <- just props(no pathProps) */}
  </div>;
}

result

result_image

⚠️ Caution

This is only works in Server Side because it should use server builtin functions and insert new html while redering. If you want to control svg in client side, consider @svgr/webpack.