33<h1 align =" center " >react-lazy-svg</h1 >
44<p align =" center " style =" font-size : 1.2rem ;" >The easy way to use SVG sprite-sheets</p >
55
6- [ ![ GitHub license] ( https://img.shields.io/github/license/kaoDev/react-lazy-svg?style=flat-square )] ( https://github.com/kaoDev/react-lazy-svg )
6+ [ ![ GitHub license] ( https://img.shields.io/github/license/kaoDev/react-lazy-svg?style=flat-square )] ( https://github.com/kaoDev/react-lazy-svg/blob/main/license.md )
77[ ![ npm] ( https://img.shields.io/npm/dm/react-lazy-svg?style=flat-square )] ( https://www.npmjs.com/package/react-lazy-svg )
88[ ![ npm] ( https://img.shields.io/npm/v/react-lazy-svg?style=flat-square )] ( https://www.npmjs.com/package/react-lazy-svg )
99[ ![ GitHub issues] ( https://img.shields.io/github/issues/kaoDev/react-lazy-svg?style=flat-square )] ( https://github.com/kaoDev/react-lazy-svg/issues )
10+ [ ![ minified bundle size] ( https://img.shields.io/bundlephobia/minzip/react-lazy-svg?style=flat-square )] ( https://bundlephobia.com/package/react-lazy-svg )
1011
1112react-lazy-svg is a simple way to use SVGs with the performance benefits of a
1213sprite-sheet and svg css styling possibilities. Without bloating the bundle. It
1314automatically creates a sprite-sheet for all used SVGs on the client but also
1415provides a function to create a server side rendered sprite-sheet for icons used
15- in the first paint.
16+ in the first paint. So you can inject the critical svg
1617
1718## Usage
1819
1920``` bash
2021npm install --save react-lazy-svg
2122```
2223
24+ Examples on how to use react-lazy-svg with remix and next.js can be found in the
25+ [ ./example-nextjs/] ( ./example-nextjs/ ) and [ ./example-remix/] ( ./example-remix/ )
26+ directory.
27+
2328Wrap the App with the contextProvider and provide a function to resolve SVG
2429strings by URL. If you are using server side rendering you should also call
2530` initOnClient() ` to hydrate the sprite sheet context.
@@ -46,26 +51,23 @@ contents from the file system.
4651
4752``` ts
4853const svgIconFiles = new Map <string , string >();
54+ const readFile = promisify (fs .readFile );
55+ const cdnBase = ' http://localhost:3001/static/media/' ;
56+
4957export const readSvg = async (url : string ) => {
5058 if (svgIconFiles .has (url )) {
5159 return svgIconFiles .get (url );
5260 }
5361
54- const readFile = promisify (fs .readFile );
55-
56- const cdnBase = ' http://localhost:3001/static/media/' ;
57-
5862 if (url .startsWith (cdnBase )) {
5963 url = path .join (
6064 process .cwd (),
6165 url .replace (cdnBase , ' ./build/public/static/media/' ),
6266 );
63- }
6467
65- // ignore external assets on server side
66- if (! url .startsWith (' http' )) {
6768 const svgString = await readFile (url , { encoding: ' utf8' });
6869 svgIconFiles .set (url , svgString );
70+
6971 return svgString ;
7072 }
7173
0 commit comments