3
3
<h1 align =" center " >react-lazy-svg</h1 >
4
4
<p align =" center " style =" font-size : 1.2rem ;" >The easy way to use SVG sprite-sheets</p >
5
5
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 )
7
7
[ ![ npm] ( https://img.shields.io/npm/dm/react-lazy-svg?style=flat-square )] ( https://www.npmjs.com/package/react-lazy-svg )
8
8
[ ![ npm] ( https://img.shields.io/npm/v/react-lazy-svg?style=flat-square )] ( https://www.npmjs.com/package/react-lazy-svg )
9
9
[ ![ 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 )
10
11
11
12
react-lazy-svg is a simple way to use SVGs with the performance benefits of a
12
13
sprite-sheet and svg css styling possibilities. Without bloating the bundle. It
13
14
automatically creates a sprite-sheet for all used SVGs on the client but also
14
15
provides 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
16
17
17
18
## Usage
18
19
19
20
``` bash
20
21
npm install --save react-lazy-svg
21
22
```
22
23
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
+
23
28
Wrap the App with the contextProvider and provide a function to resolve SVG
24
29
strings by URL. If you are using server side rendering you should also call
25
30
` initOnClient() ` to hydrate the sprite sheet context.
@@ -46,26 +51,23 @@ contents from the file system.
46
51
47
52
``` ts
48
53
const svgIconFiles = new Map <string , string >();
54
+ const readFile = promisify (fs .readFile );
55
+ const cdnBase = ' http://localhost:3001/static/media/' ;
56
+
49
57
export const readSvg = async (url : string ) => {
50
58
if (svgIconFiles .has (url )) {
51
59
return svgIconFiles .get (url );
52
60
}
53
61
54
- const readFile = promisify (fs .readFile );
55
-
56
- const cdnBase = ' http://localhost:3001/static/media/' ;
57
-
58
62
if (url .startsWith (cdnBase )) {
59
63
url = path .join (
60
64
process .cwd (),
61
65
url .replace (cdnBase , ' ./build/public/static/media/' ),
62
66
);
63
- }
64
67
65
- // ignore external assets on server side
66
- if (! url .startsWith (' http' )) {
67
68
const svgString = await readFile (url , { encoding: ' utf8' });
68
69
svgIconFiles .set (url , svgString );
70
+
69
71
return svgString ;
70
72
}
71
73
0 commit comments