@@ -17,12 +17,12 @@ const globalIconsCache: IconsCache = new Map();
1717
1818export const renderSpriteSheetToString = async (
1919 markupString : string ,
20- knownIcons : IconsCache
20+ knownIcons : IconsCache ,
2121) => {
2222 const arr = await Promise . all ( Array . from ( knownIcons . values ( ) ) ) ;
2323
2424 const spriteSheet = renderToStaticMarkup (
25- < SpriteSheet icons = { arr . filter ( ( a ) : a is IconData => a != null ) } />
25+ < SpriteSheet icons = { arr . filter ( ( a ) : a is IconData => a != null ) } /> ,
2626 ) ;
2727
2828 return markupString . replace ( ssrEmptySpriteSheet , spriteSheet ) ;
@@ -34,7 +34,7 @@ export interface IconData {
3434 attributes : { [ key : string ] : string } ;
3535}
3636
37- const noop = ( ) => void 0 ;
37+ const noop = ( ) => undefined ;
3838
3939interface SpriteContextValue {
4040 /**
@@ -78,7 +78,7 @@ const addIcon = (icon: IconData) => {
7878
7979const parseSVG = (
8080 url : string ,
81- svgString : string | undefined
81+ svgString : string | undefined ,
8282) : IconData | undefined => {
8383 if ( svgString ) {
8484 const svgRegex = / < s v g ( [ \s \S ] * ?) > ( [ \s \S ] * ?) < \/ s v g > / gim;
@@ -103,7 +103,7 @@ const parseSVG = (
103103const registerIconInCache = (
104104 url : string ,
105105 svgString : string | undefined ,
106- knownIcons : IconsCache
106+ knownIcons : IconsCache ,
107107) => {
108108 const iconData = parseSVG ( url , svgString ) ;
109109
@@ -149,12 +149,12 @@ export const SpriteContextProvider: FC<SpriteContext> = ({
149149 if ( knownIcons . has ( url ) ) {
150150 return ;
151151 }
152- const iconPromise = loadSVG ( url ) . then ( svgString =>
153- registerIconInCache ( url , svgString , knownIcons )
152+ const iconPromise = loadSVG ( url ) . then ( ( svgString ) =>
153+ registerIconInCache ( url , svgString , knownIcons ) ,
154154 ) ;
155155 knownIcons . set ( url , iconPromise ) ;
156156 } ,
157- [ knownIcons ]
157+ [ knownIcons ] ,
158158 ) ;
159159
160160 const contextValue = useMemo ( ( ) => ( { registerSVG } ) , [ registerSVG ] ) ;
@@ -172,7 +172,14 @@ export const Icon: FC<{ url: string } & React.SVGProps<SVGSVGElement>> = ({
172172 ...props
173173} ) => {
174174 const { registerSVG } = useContext ( spriteContext ) ;
175- registerSVG ( url ) ;
175+
176+ if ( typeof document !== 'undefined' ) {
177+ registerSVG ( url ) ;
178+ } else {
179+ useEffect ( ( ) => {
180+ registerSVG ( url ) ;
181+ } , [ url ] ) ;
182+ }
176183
177184 return (
178185 < svg { ...props } >
@@ -205,7 +212,7 @@ const SpriteSheet: FC<{ icons: IconData[] }> = ({ icons }) => {
205212 dangerouslySetInnerHTML = { svgString }
206213 />
207214 ) ;
208- }
215+ } ,
209216 ) }
210217 </ svg >
211218 ) ;
@@ -218,7 +225,7 @@ const mapNodeAttributes = (rawAttributes: NamedNodeMap) =>
218225
219226 return attributes ;
220227 } ,
221- { }
228+ { } ,
222229 ) ;
223230
224231export const initOnClient = ( knownIcons : IconsCache = globalIconsCache ) => {
@@ -228,7 +235,7 @@ export const initOnClient = (knownIcons: IconsCache = globalIconsCache) => {
228235 if ( spriteSheet ) {
229236 const sprites = spriteSheet . querySelectorAll ( 'symbol' ) ;
230237
231- sprites . forEach ( node => {
238+ sprites . forEach ( ( node ) => {
232239 const { id, attributes : rawAttributes , innerHTML } = node ;
233240 const attributes = mapNodeAttributes ( rawAttributes ) ;
234241 const iconData = {
@@ -238,7 +245,7 @@ export const initOnClient = (knownIcons: IconsCache = globalIconsCache) => {
238245 } ;
239246 addIcon ( iconData ) ;
240247
241- knownIcons . set ( id , new Promise ( resolve => resolve ( iconData ) ) ) ;
248+ knownIcons . set ( id , new Promise ( ( resolve ) => resolve ( iconData ) ) ) ;
242249 } ) ;
243250 }
244251} ;
0 commit comments