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 🙏

© 2025 – Pkg Stats / Ryan Hefner

svgin-react

v0.7.0

Published

Securely fetch and inline SVGs as React components.

Downloads

24

Readme

svgin-react

npm version MIT License

Securely fetch and inline SVGs from URLs as React components. Supports both client and server rendering, with optimal tree-shaking and modern React best practices.


✨ Features

  • Client & Server: Use in React apps, Next.js, RSC, SSR, etc.
  • Sanitization: Secure by default with DOMPurify (customizable or disableable).
  • In-memory cache: Fast, avoids duplicate fetches.
  • Preload API: Preload SVGs for instant rendering.
  • Modern exports: Separate entrypoints for client, server, and core utilities for best tree-shaking.

📦 Install

# If you want SVG sanitization (recommended for untrusted SVGs):
npm install svgin-react dompurify jsdom
# If you trust your SVGs and will disable sanitization:
npm install svgin-react

Note: This library uses DOMPurify directly for sanitization. On the server, it uses jsdom together with DOMPurify. You do not need isomorphic-dompurify.


🧩 Usage

Universal Usage (Automatic Server/Client Resolution)

// Works in both client and server components (auto-resolves)
import { SvgIn } from 'svgin-react';

export default function AlertIcon() {
  return <SvgIn src="/icons/alert.svg" width={24} fill="#f00" />;
}

Note: In Next.js app directory, add 'use client'; at the top of your file if you want a client component.

Forcing a specific entrypoint (advanced/legacy)

// Force client-only or server-only entrypoint if needed
import { SvgIn } from 'svgin-react/client';
import { SvgIn } from 'svgin-react/server';

Preloading SVGs (client only)

import { preloadSvg } from 'svgin-react/core';

preloadSvg('/icons/alert.svg');

🔧 API

<SvgIn /> (client)

Props:

  • src: string (URL to the SVG)
  • width, height: number | string
  • fill: string (for color)
  • fallback: ReactNode (spinner or fallback SVG)
  • className: string
  • ariaLabel: string
  • sanitizeFn?: (svg: string) => Promise (optional, override or disable sanitization)
  • disableSanitization?: boolean (optional, disables all sanitization)

SvgIn(props) (server)

  • Same props as <SvgIn />, but is an async function for use in server components.

preloadSvg(url, sanitizeFn?)

  • Preloads and caches an SVG for later use. Optional custom sanitizer.

🗂️ Entry Points & Tree-shaking

  • svgin-react/client → Only the client component (SvgIn). No server or preload code included.
  • svgin-react/server → Only the server component (SvgIn). No client or preload code included.
  • svgin-react/core → Only core utilities (preloadSvg, types, etc.).

🛡️ Security

  • By default, all SVGs are sanitized with DOMPurify (dynamically imported, not in bundle unless used).
  • You can provide your own sanitizeFn or disable sanitization if you trust your SVG source (no need to install dompurify/jsdom in that case).

📝 Examples

Custom Sanitizer:

<SvgIn src="/icons/alert.svg" sanitizeFn={async (svg) => svg} />

Disable Sanitization:

<SvgIn src="/icons/alert.svg" disableSanitization />

License

MIT