react-inlinesvg 4.1.7 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +57 -33
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +57 -33
- package/dist/index.mjs.map +1 -1
- package/dist/provider.js +1 -1
- package/dist/provider.js.map +1 -1
- package/dist/provider.mjs +1 -1
- package/dist/provider.mjs.map +1 -1
- package/package.json +6 -6
- package/src/index.tsx +50 -27
- package/src/modules/cache.ts +21 -11
- package/src/modules/helpers.ts +1 -1
- package/src/types.ts +2 -2
package/README.md
CHANGED
|
@@ -192,7 +192,7 @@ But there's an alternative that sidesteps these issues: load the SVG with a GET
|
|
|
192
192
|
|
|
193
193
|
### Note
|
|
194
194
|
|
|
195
|
-
The SVG [`<use>`](http://css-tricks.com/svg-use-external-source) element can be used to achieve something similar to this component. See [this article](http://taye.me/blog/svg/a-guide-to-svg-use-elements
|
|
195
|
+
The SVG [`<use>`](http://css-tricks.com/svg-use-external-source) element can be used to achieve something similar to this component. See [this article](http://taye.me/blog/svg/a-guide-to-svg-use-elements) for more information and [this table](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/use#Browser_compatibility) for browser support and caveats.
|
|
196
196
|
|
|
197
197
|
## Credits
|
|
198
198
|
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { SVGProps, ReactNode,
|
|
1
|
+
import React, { SVGProps, ReactNode, Ref } from 'react';
|
|
2
2
|
|
|
3
3
|
declare const STATUS: {
|
|
4
4
|
readonly IDLE: "idle";
|
|
@@ -19,7 +19,7 @@ type Props = Simplify<Omit<SVGProps<SVGElement>, 'onLoad' | 'onError' | 'ref'> &
|
|
|
19
19
|
children?: ReactNode;
|
|
20
20
|
description?: string;
|
|
21
21
|
fetchOptions?: RequestInit;
|
|
22
|
-
innerRef?:
|
|
22
|
+
innerRef?: Ref<SVGElement | null>;
|
|
23
23
|
loader?: ReactNode;
|
|
24
24
|
onError?: ErrorCallback;
|
|
25
25
|
onLoad?: LoadCallback;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { SVGProps, ReactNode,
|
|
1
|
+
import React, { SVGProps, ReactNode, Ref } from 'react';
|
|
2
2
|
|
|
3
3
|
declare const STATUS: {
|
|
4
4
|
readonly IDLE: "idle";
|
|
@@ -19,7 +19,7 @@ type Props = Simplify<Omit<SVGProps<SVGElement>, 'onLoad' | 'onError' | 'ref'> &
|
|
|
19
19
|
children?: ReactNode;
|
|
20
20
|
description?: string;
|
|
21
21
|
fetchOptions?: RequestInit;
|
|
22
|
-
innerRef?:
|
|
22
|
+
innerRef?: Ref<SVGElement | null>;
|
|
23
23
|
loader?: ReactNode;
|
|
24
24
|
onError?: ErrorCallback;
|
|
25
25
|
onLoad?: LoadCallback;
|
package/dist/index.js
CHANGED
|
@@ -57,7 +57,7 @@ function randomCharacter(character) {
|
|
|
57
57
|
return character[Math.floor(Math.random() * character.length)];
|
|
58
58
|
}
|
|
59
59
|
function canUseDOM() {
|
|
60
|
-
return !!window
|
|
60
|
+
return !!(typeof window !== "undefined" && window.document?.createElement);
|
|
61
61
|
}
|
|
62
62
|
function isSupportedEnvironment() {
|
|
63
63
|
return supportsInlineSVG() && typeof window !== "undefined" && window !== null;
|
|
@@ -129,9 +129,18 @@ var CacheStore = class {
|
|
|
129
129
|
this.cacheApi = cache;
|
|
130
130
|
}).catch((error) => {
|
|
131
131
|
console.error(`Failed to open cache: ${error.message}`);
|
|
132
|
+
this.cacheApi = void 0;
|
|
132
133
|
}).finally(() => {
|
|
133
134
|
this.isReady = true;
|
|
134
|
-
this.subscribers
|
|
135
|
+
const callbacks = [...this.subscribers];
|
|
136
|
+
this.subscribers.length = 0;
|
|
137
|
+
callbacks.forEach((callback) => {
|
|
138
|
+
try {
|
|
139
|
+
callback();
|
|
140
|
+
} catch (error) {
|
|
141
|
+
console.error(`Error in CacheStore subscriber callback: ${error.message}`);
|
|
142
|
+
}
|
|
143
|
+
});
|
|
135
144
|
});
|
|
136
145
|
} else {
|
|
137
146
|
this.isReady = true;
|
|
@@ -204,14 +213,13 @@ var CacheStore = class {
|
|
|
204
213
|
}
|
|
205
214
|
}
|
|
206
215
|
async handleLoading(url, callback) {
|
|
207
|
-
let retryCount = 0;
|
|
208
|
-
|
|
216
|
+
for (let retryCount = 0; retryCount < CACHE_MAX_RETRIES; retryCount++) {
|
|
217
|
+
if (this.cacheStore.get(url)?.status !== STATUS.LOADING) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
209
220
|
await sleep(0.1);
|
|
210
|
-
retryCount += 1;
|
|
211
|
-
}
|
|
212
|
-
if (retryCount >= CACHE_MAX_RETRIES) {
|
|
213
|
-
await callback();
|
|
214
221
|
}
|
|
222
|
+
await callback();
|
|
215
223
|
}
|
|
216
224
|
keys() {
|
|
217
225
|
return [...this.cacheStore.keys()];
|
|
@@ -228,9 +236,7 @@ var CacheStore = class {
|
|
|
228
236
|
async clear() {
|
|
229
237
|
if (this.cacheApi) {
|
|
230
238
|
const keys = await this.cacheApi.keys();
|
|
231
|
-
|
|
232
|
-
await this.cacheApi.delete(key);
|
|
233
|
-
}
|
|
239
|
+
await Promise.allSettled(keys.map((key) => this.cacheApi.delete(key)));
|
|
234
240
|
}
|
|
235
241
|
this.cacheStore.clear();
|
|
236
242
|
}
|
|
@@ -400,7 +406,7 @@ function ReactInlineSVG(props) {
|
|
|
400
406
|
status: STATUS.READY
|
|
401
407
|
});
|
|
402
408
|
} catch (error) {
|
|
403
|
-
handleError(
|
|
409
|
+
handleError(error);
|
|
404
410
|
}
|
|
405
411
|
}, [content, handleError, props]);
|
|
406
412
|
const getContent = (0, import_react2.useCallback)(async () => {
|
|
@@ -440,7 +446,7 @@ function ReactInlineSVG(props) {
|
|
|
440
446
|
() => {
|
|
441
447
|
isActive.current = true;
|
|
442
448
|
if (!canUseDOM() || isInitialized.current) {
|
|
443
|
-
return
|
|
449
|
+
return void 0;
|
|
444
450
|
}
|
|
445
451
|
try {
|
|
446
452
|
if (status === STATUS.IDLE) {
|
|
@@ -464,10 +470,7 @@ function ReactInlineSVG(props) {
|
|
|
464
470
|
[]
|
|
465
471
|
);
|
|
466
472
|
(0, import_react2.useEffect)(() => {
|
|
467
|
-
if (!canUseDOM()) {
|
|
468
|
-
return;
|
|
469
|
-
}
|
|
470
|
-
if (!previousProps) {
|
|
473
|
+
if (!canUseDOM() || !previousProps) {
|
|
471
474
|
return;
|
|
472
475
|
}
|
|
473
476
|
if (previousProps.src !== src) {
|
|
@@ -476,22 +479,44 @@ function ReactInlineSVG(props) {
|
|
|
476
479
|
return;
|
|
477
480
|
}
|
|
478
481
|
load();
|
|
479
|
-
}
|
|
482
|
+
}
|
|
483
|
+
}, [handleError, load, previousProps, src]);
|
|
484
|
+
(0, import_react2.useEffect)(() => {
|
|
485
|
+
if (status === STATUS.LOADED) {
|
|
480
486
|
getElement();
|
|
481
487
|
}
|
|
482
|
-
}, [
|
|
488
|
+
}, [status, getElement]);
|
|
483
489
|
(0, import_react2.useEffect)(() => {
|
|
484
|
-
if (!
|
|
490
|
+
if (!canUseDOM() || !previousProps || previousProps.src !== src) {
|
|
485
491
|
return;
|
|
486
492
|
}
|
|
487
|
-
if (
|
|
488
|
-
getContent();
|
|
489
|
-
}
|
|
490
|
-
if (previousState.status !== STATUS.LOADED && status === STATUS.LOADED) {
|
|
493
|
+
if (previousProps.title !== title || previousProps.description !== description) {
|
|
491
494
|
getElement();
|
|
492
495
|
}
|
|
493
|
-
|
|
494
|
-
|
|
496
|
+
}, [description, getElement, previousProps, src, title]);
|
|
497
|
+
(0, import_react2.useEffect)(() => {
|
|
498
|
+
if (!previousState) {
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
switch (status) {
|
|
502
|
+
case STATUS.LOADING: {
|
|
503
|
+
if (previousState.status !== STATUS.LOADING) {
|
|
504
|
+
getContent();
|
|
505
|
+
}
|
|
506
|
+
break;
|
|
507
|
+
}
|
|
508
|
+
case STATUS.LOADED: {
|
|
509
|
+
if (previousState.status !== STATUS.LOADED) {
|
|
510
|
+
getElement();
|
|
511
|
+
}
|
|
512
|
+
break;
|
|
513
|
+
}
|
|
514
|
+
case STATUS.READY: {
|
|
515
|
+
if (previousState.status !== STATUS.READY) {
|
|
516
|
+
onLoad?.(src, isCached);
|
|
517
|
+
}
|
|
518
|
+
break;
|
|
519
|
+
}
|
|
495
520
|
}
|
|
496
521
|
}, [getContent, getElement, isCached, onLoad, previousState, src, status]);
|
|
497
522
|
const elementProps = omit(
|
|
@@ -530,16 +555,15 @@ function InlineSVG(props) {
|
|
|
530
555
|
cacheStore = new CacheStore();
|
|
531
556
|
}
|
|
532
557
|
const { loader } = props;
|
|
533
|
-
const hasCallback = (0, import_react2.useRef)(false);
|
|
534
558
|
const [isReady, setReady] = (0, import_react2.useState)(cacheStore.isReady);
|
|
535
559
|
(0, import_react2.useEffect)(() => {
|
|
536
|
-
if (
|
|
537
|
-
|
|
538
|
-
setReady(true);
|
|
539
|
-
});
|
|
540
|
-
hasCallback.current = true;
|
|
560
|
+
if (isReady) {
|
|
561
|
+
return;
|
|
541
562
|
}
|
|
542
|
-
|
|
563
|
+
cacheStore.onReady(() => {
|
|
564
|
+
setReady(true);
|
|
565
|
+
});
|
|
566
|
+
}, [isReady]);
|
|
543
567
|
if (!isReady) {
|
|
544
568
|
return loader;
|
|
545
569
|
}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.tsx","../src/config.ts","../src/modules/helpers.ts","../src/modules/cache.ts","../src/modules/hooks.tsx","../src/modules/utils.ts"],"sourcesContent":["import React, {\n cloneElement,\n isValidElement,\n ReactElement,\n useCallback,\n useEffect,\n useReducer,\n useRef,\n useState,\n} from 'react';\nimport convert from 'react-from-dom';\n\nimport { STATUS } from './config';\nimport CacheStore from './modules/cache';\nimport { canUseDOM, isSupportedEnvironment, omit, randomString, request } from './modules/helpers';\nimport { usePrevious } from './modules/hooks';\nimport { getNode } from './modules/utils';\nimport { FetchError, Props, State, Status } from './types';\n\n// eslint-disable-next-line import/no-mutable-exports\nexport let cacheStore: CacheStore;\n\nfunction ReactInlineSVG(props: Props) {\n const {\n cacheRequests = true,\n children = null,\n description,\n fetchOptions,\n innerRef,\n loader = null,\n onError,\n onLoad,\n src,\n title,\n uniqueHash,\n } = props;\n const [state, setState] = useReducer(\n (previousState: State, nextState: Partial<State>) => ({\n ...previousState,\n ...nextState,\n }),\n {\n content: '',\n element: null,\n\n isCached: cacheRequests && cacheStore.isCached(props.src),\n status: STATUS.IDLE,\n },\n );\n const { content, element, isCached, status } = state;\n const previousProps = usePrevious(props);\n const previousState = usePrevious(state);\n\n const hash = useRef(uniqueHash ?? randomString(8));\n const isActive = useRef(false);\n const isInitialized = useRef(false);\n\n const handleError = useCallback(\n (error: Error | FetchError) => {\n if (isActive.current) {\n setState({\n status:\n error.message === 'Browser does not support SVG' ? STATUS.UNSUPPORTED : STATUS.FAILED,\n });\n\n onError?.(error);\n }\n },\n [onError],\n );\n\n const handleLoad = useCallback((loadedContent: string, hasCache = false) => {\n if (isActive.current) {\n setState({\n content: loadedContent,\n isCached: hasCache,\n status: STATUS.LOADED,\n });\n }\n }, []);\n\n const fetchContent = useCallback(async () => {\n const responseContent: string = await request(src, fetchOptions);\n\n handleLoad(responseContent);\n }, [fetchOptions, handleLoad, src]);\n\n const getElement = useCallback(() => {\n try {\n const node = getNode({ ...props, handleError, hash: hash.current, content }) as Node;\n const convertedElement = convert(node);\n\n if (!convertedElement || !isValidElement(convertedElement)) {\n throw new Error('Could not convert the src to a React element');\n }\n\n setState({\n element: convertedElement,\n status: STATUS.READY,\n });\n } catch (error: any) {\n handleError(new Error(error.message));\n }\n }, [content, handleError, props]);\n\n const getContent = useCallback(async () => {\n const dataURI = /^data:image\\/svg[^,]*?(;base64)?,(.*)/u.exec(src);\n let inlineSrc;\n\n if (dataURI) {\n inlineSrc = dataURI[1] ? window.atob(dataURI[2]) : decodeURIComponent(dataURI[2]);\n } else if (src.includes('<svg')) {\n inlineSrc = src;\n }\n\n if (inlineSrc) {\n handleLoad(inlineSrc);\n\n return;\n }\n\n try {\n if (cacheRequests) {\n const cachedContent = await cacheStore.get(src, fetchOptions);\n\n handleLoad(cachedContent, true);\n } else {\n await fetchContent();\n }\n } catch (error: any) {\n handleError(error);\n }\n }, [cacheRequests, fetchContent, fetchOptions, handleError, handleLoad, src]);\n\n const load = useCallback(async () => {\n if (isActive.current) {\n setState({\n content: '',\n element: null,\n isCached: false,\n status: STATUS.LOADING,\n });\n }\n }, []);\n\n // Run on mount\n useEffect(\n () => {\n isActive.current = true;\n\n if (!canUseDOM() || isInitialized.current) {\n return () => undefined;\n }\n\n try {\n if (status === STATUS.IDLE) {\n if (!isSupportedEnvironment()) {\n throw new Error('Browser does not support SVG');\n }\n\n if (!src) {\n throw new Error('Missing src');\n }\n\n load();\n }\n } catch (error: any) {\n handleError(error);\n }\n\n isInitialized.current = true;\n\n return () => {\n isActive.current = false;\n };\n },\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [],\n );\n\n // Handle prop changes\n useEffect(() => {\n if (!canUseDOM()) {\n return;\n }\n\n if (!previousProps) {\n return;\n }\n\n if (previousProps.src !== src) {\n if (!src) {\n handleError(new Error('Missing src'));\n\n return;\n }\n\n load();\n } else if (previousProps.title !== title || previousProps.description !== description) {\n getElement();\n }\n }, [description, getElement, handleError, load, previousProps, src, title]);\n\n // handle state\n useEffect(() => {\n if (!previousState) {\n return;\n }\n\n if (previousState.status !== STATUS.LOADING && status === STATUS.LOADING) {\n getContent();\n }\n\n if (previousState.status !== STATUS.LOADED && status === STATUS.LOADED) {\n getElement();\n }\n\n if (previousState.status !== STATUS.READY && status === STATUS.READY) {\n onLoad?.(src, isCached);\n }\n }, [getContent, getElement, isCached, onLoad, previousState, src, status]);\n\n const elementProps = omit(\n props,\n 'baseURL',\n 'cacheRequests',\n 'children',\n 'description',\n 'fetchOptions',\n 'innerRef',\n 'loader',\n 'onError',\n 'onLoad',\n 'preProcessor',\n 'src',\n 'title',\n 'uniqueHash',\n 'uniquifyIDs',\n );\n\n if (!canUseDOM()) {\n return loader;\n }\n\n if (element) {\n return cloneElement(element as ReactElement<any>, {\n ref: innerRef,\n ...elementProps,\n });\n }\n\n if (([STATUS.UNSUPPORTED, STATUS.FAILED] as Status[]).includes(status)) {\n return children;\n }\n\n return loader;\n}\n\nexport default function InlineSVG(props: Props) {\n if (!cacheStore) {\n cacheStore = new CacheStore();\n }\n\n const { loader } = props;\n const hasCallback = useRef(false);\n const [isReady, setReady] = useState(cacheStore.isReady);\n\n useEffect(() => {\n if (!hasCallback.current) {\n cacheStore.onReady(() => {\n setReady(true);\n });\n\n hasCallback.current = true;\n }\n }, []);\n\n if (!isReady) {\n return loader;\n }\n\n return <ReactInlineSVG {...props} />;\n}\n\nexport * from './types';\n","export const CACHE_NAME = 'react-inlinesvg';\nexport const CACHE_MAX_RETRIES = 10;\n\nexport const STATUS = {\n IDLE: 'idle',\n LOADING: 'loading',\n LOADED: 'loaded',\n FAILED: 'failed',\n READY: 'ready',\n UNSUPPORTED: 'unsupported',\n} as const;\n","import type { PlainObject } from '../types';\n\nfunction randomCharacter(character: string) {\n return character[Math.floor(Math.random() * character.length)];\n}\n\nexport function canUseDOM(): boolean {\n return !!window?.document?.createElement;\n}\n\nexport function isSupportedEnvironment(): boolean {\n return supportsInlineSVG() && typeof window !== 'undefined' && window !== null;\n}\n\n/**\n * Remove properties from an object\n */\nexport function omit<T extends PlainObject, K extends keyof T>(\n input: T,\n ...filter: K[]\n): Omit<T, K> {\n const output: any = {};\n\n for (const key in input) {\n if ({}.hasOwnProperty.call(input, key)) {\n if (!filter.includes(key as unknown as K)) {\n output[key] = input[key];\n }\n }\n }\n\n return output as Omit<T, K>;\n}\n\nexport function randomString(length: number): string {\n const letters = 'abcdefghijklmnopqrstuvwxyz';\n const numbers = '1234567890';\n const charset = `${letters}${letters.toUpperCase()}${numbers}`;\n\n let R = '';\n\n for (let index = 0; index < length; index++) {\n R += randomCharacter(charset);\n }\n\n return R;\n}\n\nexport async function request(url: string, options?: RequestInit) {\n const response = await fetch(url, options);\n const contentType = response.headers.get('content-type');\n const [fileType] = (contentType ?? '').split(/ ?; ?/);\n\n if (response.status > 299) {\n throw new Error('Not found');\n }\n\n if (!['image/svg+xml', 'text/plain'].some(d => fileType.includes(d))) {\n throw new Error(`Content type isn't valid: ${fileType}`);\n }\n\n return response.text();\n}\n\nexport function sleep(seconds = 1) {\n return new Promise(resolve => {\n setTimeout(resolve, seconds * 1000);\n });\n}\n\nexport function supportsInlineSVG(): boolean {\n /* c8 ignore next 3 */\n if (!document) {\n return false;\n }\n\n const div = document.createElement('div');\n\n div.innerHTML = '<svg />';\n const svg = div.firstChild as SVGSVGElement;\n\n return !!svg && svg.namespaceURI === 'http://www.w3.org/2000/svg';\n}\n","import { CACHE_MAX_RETRIES, CACHE_NAME, STATUS } from '../config';\nimport { StorageItem } from '../types';\n\nimport { canUseDOM, request, sleep } from './helpers';\n\nexport default class CacheStore {\n private cacheApi: Cache | undefined;\n private readonly cacheStore: Map<string, StorageItem>;\n private readonly subscribers: Array<() => void> = [];\n public isReady = false;\n\n constructor() {\n this.cacheStore = new Map<string, StorageItem>();\n\n let cacheName = CACHE_NAME;\n let usePersistentCache = false;\n\n if (canUseDOM()) {\n cacheName = window.REACT_INLINESVG_CACHE_NAME ?? CACHE_NAME;\n usePersistentCache = !!window.REACT_INLINESVG_PERSISTENT_CACHE && 'caches' in window;\n }\n\n if (usePersistentCache) {\n caches\n .open(cacheName)\n .then(cache => {\n this.cacheApi = cache;\n })\n .catch(error => {\n // eslint-disable-next-line no-console\n console.error(`Failed to open cache: ${error.message}`);\n })\n .finally(() => {\n this.isReady = true;\n this.subscribers.forEach(callback => callback());\n });\n } else {\n this.isReady = true;\n }\n }\n\n public onReady(callback: () => void) {\n if (this.isReady) {\n callback();\n } else {\n this.subscribers.push(callback);\n }\n }\n\n public async get(url: string, fetchOptions?: RequestInit) {\n await (this.cacheApi\n ? this.fetchAndAddToPersistentCache(url, fetchOptions)\n : this.fetchAndAddToInternalCache(url, fetchOptions));\n\n return this.cacheStore.get(url)?.content ?? '';\n }\n\n public set(url: string, data: StorageItem) {\n this.cacheStore.set(url, data);\n }\n\n public isCached(url: string) {\n return this.cacheStore.get(url)?.status === STATUS.LOADED;\n }\n\n private async fetchAndAddToInternalCache(url: string, fetchOptions?: RequestInit) {\n const cache = this.cacheStore.get(url);\n\n if (cache?.status === STATUS.LOADING) {\n await this.handleLoading(url, async () => {\n this.cacheStore.set(url, { content: '', status: STATUS.IDLE });\n await this.fetchAndAddToInternalCache(url, fetchOptions);\n });\n\n return;\n }\n\n if (!cache?.content) {\n this.cacheStore.set(url, { content: '', status: STATUS.LOADING });\n\n try {\n const content = await request(url, fetchOptions);\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n } catch (error: any) {\n this.cacheStore.set(url, { content: '', status: STATUS.FAILED });\n throw error;\n }\n }\n }\n\n private async fetchAndAddToPersistentCache(url: string, fetchOptions?: RequestInit) {\n const cache = this.cacheStore.get(url);\n\n if (cache?.status === STATUS.LOADED) {\n return;\n }\n\n if (cache?.status === STATUS.LOADING) {\n await this.handleLoading(url, async () => {\n this.cacheStore.set(url, { content: '', status: STATUS.IDLE });\n await this.fetchAndAddToPersistentCache(url, fetchOptions);\n });\n\n return;\n }\n\n this.cacheStore.set(url, { content: '', status: STATUS.LOADING });\n\n const data = await this.cacheApi?.match(url);\n\n if (data) {\n const content = await data.text();\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n\n return;\n }\n\n try {\n await this.cacheApi?.add(new Request(url, fetchOptions));\n\n const response = await this.cacheApi?.match(url);\n const content = (await response?.text()) ?? '';\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n } catch (error: any) {\n this.cacheStore.set(url, { content: '', status: STATUS.FAILED });\n throw error;\n }\n }\n\n private async handleLoading(url: string, callback: () => Promise<void>) {\n let retryCount = 0;\n\n while (this.cacheStore.get(url)?.status === STATUS.LOADING && retryCount < CACHE_MAX_RETRIES) {\n // eslint-disable-next-line no-await-in-loop\n await sleep(0.1);\n retryCount += 1;\n }\n\n if (retryCount >= CACHE_MAX_RETRIES) {\n await callback();\n }\n }\n\n public keys(): Array<string> {\n return [...this.cacheStore.keys()];\n }\n\n public data(): Array<Record<string, StorageItem>> {\n return [...this.cacheStore.entries()].map(([key, value]) => ({ [key]: value }));\n }\n\n public async delete(url: string) {\n if (this.cacheApi) {\n await this.cacheApi.delete(url);\n }\n\n this.cacheStore.delete(url);\n }\n\n public async clear() {\n if (this.cacheApi) {\n const keys = await this.cacheApi.keys();\n\n for (const key of keys) {\n // eslint-disable-next-line no-await-in-loop\n await this.cacheApi.delete(key);\n }\n }\n\n this.cacheStore.clear();\n }\n}\n","import { useEffect, useRef } from 'react';\n\nexport function usePrevious<T>(state: T): T | undefined {\n const ref = useRef<T>(undefined);\n\n useEffect(() => {\n ref.current = state;\n });\n\n return ref.current;\n}\n","import convert from 'react-from-dom';\n\nimport { Props, State } from '../types';\n\ninterface GetNodeOptions extends Props, Pick<State, 'content'> {\n handleError: (error: Error) => void;\n hash: string;\n}\n\ninterface UpdateSVGAttributesOptions extends Pick<Props, 'baseURL' | 'uniquifyIDs'> {\n hash: string;\n}\n\nexport function getNode(options: GetNodeOptions) {\n const {\n baseURL,\n content,\n description,\n handleError,\n hash,\n preProcessor,\n title,\n uniquifyIDs = false,\n } = options;\n\n try {\n const svgText = processSVG(content, preProcessor);\n const node = convert(svgText, { nodeOnly: true });\n\n if (!node || !(node instanceof SVGSVGElement)) {\n throw new Error('Could not convert the src to a DOM Node');\n }\n\n const svg = updateSVGAttributes(node, { baseURL, hash, uniquifyIDs });\n\n if (description) {\n const originalDesc = svg.querySelector('desc');\n\n if (originalDesc?.parentNode) {\n originalDesc.parentNode.removeChild(originalDesc);\n }\n\n const descElement = document.createElementNS('http://www.w3.org/2000/svg', 'desc');\n\n descElement.innerHTML = description;\n svg.prepend(descElement);\n }\n\n if (typeof title !== 'undefined') {\n const originalTitle = svg.querySelector('title');\n\n if (originalTitle?.parentNode) {\n originalTitle.parentNode.removeChild(originalTitle);\n }\n\n if (title) {\n const titleElement = document.createElementNS('http://www.w3.org/2000/svg', 'title');\n\n titleElement.innerHTML = title;\n svg.prepend(titleElement);\n }\n }\n\n return svg;\n } catch (error: any) {\n return handleError(error);\n }\n}\n\nexport function processSVG(content: string, preProcessor?: Props['preProcessor']) {\n if (preProcessor) {\n return preProcessor(content);\n }\n\n return content;\n}\n\nexport function updateSVGAttributes(\n node: SVGSVGElement,\n options: UpdateSVGAttributesOptions,\n): SVGSVGElement {\n const { baseURL = '', hash, uniquifyIDs } = options;\n const replaceableAttributes = ['id', 'href', 'xlink:href', 'xlink:role', 'xlink:arcrole'];\n const linkAttributes = ['href', 'xlink:href'];\n const isDataValue = (name: string, value: string) =>\n linkAttributes.includes(name) && (value ? !value.includes('#') : false);\n\n if (!uniquifyIDs) {\n return node;\n }\n\n [...node.children].forEach(d => {\n if (d.attributes?.length) {\n const attributes = Object.values(d.attributes).map(a => {\n const attribute = a;\n const match = /url\\((.*?)\\)/.exec(a.value);\n\n if (match?.[1]) {\n attribute.value = a.value.replace(match[0], `url(${baseURL}${match[1]}__${hash})`);\n }\n\n return attribute;\n });\n\n replaceableAttributes.forEach(r => {\n const attribute = attributes.find(a => a.name === r);\n\n if (attribute && !isDataValue(r, attribute.value)) {\n attribute.value = `${attribute.value}__${hash}`;\n }\n });\n }\n\n if (d.children.length) {\n return updateSVGAttributes(d as SVGSVGElement, options);\n }\n\n return d;\n });\n\n return node;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,gBASO;AACP,IAAAC,yBAAoB;;;ACVb,IAAM,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,kBAAgB,GAAG,OAAO;AACpC;","names":["import_react","import_react_from_dom","convert","previousState","convert","React"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.tsx","../src/config.ts","../src/modules/helpers.ts","../src/modules/cache.ts","../src/modules/hooks.tsx","../src/modules/utils.ts"],"sourcesContent":["import React, {\n cloneElement,\n isValidElement,\n ReactElement,\n useCallback,\n useEffect,\n useReducer,\n useRef,\n useState,\n} from 'react';\nimport convert from 'react-from-dom';\n\nimport { STATUS } from './config';\nimport CacheStore from './modules/cache';\nimport { canUseDOM, isSupportedEnvironment, omit, randomString, request } from './modules/helpers';\nimport { usePrevious } from './modules/hooks';\nimport { getNode } from './modules/utils';\nimport { FetchError, Props, State, Status } from './types';\n\n// eslint-disable-next-line import/no-mutable-exports\nexport let cacheStore: CacheStore;\n\nfunction ReactInlineSVG(props: Props) {\n const {\n cacheRequests = true,\n children = null,\n description,\n fetchOptions,\n innerRef,\n loader = null,\n onError,\n onLoad,\n src,\n title,\n uniqueHash,\n } = props;\n const [state, setState] = useReducer(\n (previousState: State, nextState: Partial<State>) => ({\n ...previousState,\n ...nextState,\n }),\n {\n content: '',\n element: null,\n\n isCached: cacheRequests && cacheStore.isCached(props.src),\n status: STATUS.IDLE,\n },\n );\n const { content, element, isCached, status } = state;\n const previousProps = usePrevious(props);\n const previousState = usePrevious(state);\n\n const hash = useRef(uniqueHash ?? randomString(8));\n const isActive = useRef(false);\n const isInitialized = useRef(false);\n\n const handleError = useCallback(\n (error: Error | FetchError) => {\n if (isActive.current) {\n setState({\n status:\n error.message === 'Browser does not support SVG' ? STATUS.UNSUPPORTED : STATUS.FAILED,\n });\n\n onError?.(error);\n }\n },\n [onError],\n );\n\n const handleLoad = useCallback((loadedContent: string, hasCache = false) => {\n if (isActive.current) {\n setState({\n content: loadedContent,\n isCached: hasCache,\n status: STATUS.LOADED,\n });\n }\n }, []);\n\n const fetchContent = useCallback(async () => {\n const responseContent: string = await request(src, fetchOptions);\n\n handleLoad(responseContent);\n }, [fetchOptions, handleLoad, src]);\n\n const getElement = useCallback(() => {\n try {\n const node = getNode({ ...props, handleError, hash: hash.current, content }) as Node;\n const convertedElement = convert(node);\n\n if (!convertedElement || !isValidElement(convertedElement)) {\n throw new Error('Could not convert the src to a React element');\n }\n\n setState({\n element: convertedElement,\n status: STATUS.READY,\n });\n } catch (error: any) {\n handleError(error);\n }\n }, [content, handleError, props]);\n\n const getContent = useCallback(async () => {\n const dataURI = /^data:image\\/svg[^,]*?(;base64)?,(.*)/u.exec(src);\n let inlineSrc;\n\n if (dataURI) {\n inlineSrc = dataURI[1] ? window.atob(dataURI[2]) : decodeURIComponent(dataURI[2]);\n } else if (src.includes('<svg')) {\n inlineSrc = src;\n }\n\n if (inlineSrc) {\n handleLoad(inlineSrc);\n\n return;\n }\n\n try {\n if (cacheRequests) {\n const cachedContent = await cacheStore.get(src, fetchOptions);\n\n handleLoad(cachedContent, true);\n } else {\n await fetchContent();\n }\n } catch (error: any) {\n handleError(error);\n }\n }, [cacheRequests, fetchContent, fetchOptions, handleError, handleLoad, src]);\n\n const load = useCallback(async () => {\n if (isActive.current) {\n setState({\n content: '',\n element: null,\n isCached: false,\n status: STATUS.LOADING,\n });\n }\n }, []);\n\n // Run on mount\n useEffect(\n () => {\n isActive.current = true;\n\n if (!canUseDOM() || isInitialized.current) {\n return undefined;\n }\n\n try {\n if (status === STATUS.IDLE) {\n if (!isSupportedEnvironment()) {\n throw new Error('Browser does not support SVG');\n }\n\n if (!src) {\n throw new Error('Missing src');\n }\n\n load();\n }\n } catch (error: any) {\n handleError(error);\n }\n\n isInitialized.current = true;\n\n return () => {\n isActive.current = false;\n };\n },\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [],\n );\n\n // Handles `src` changes\n useEffect(() => {\n if (!canUseDOM() || !previousProps) {\n return;\n }\n\n if (previousProps.src !== src) {\n if (!src) {\n handleError(new Error('Missing src'));\n\n return;\n }\n\n load();\n }\n }, [handleError, load, previousProps, src]);\n\n // Handles content loading\n useEffect(() => {\n if (status === STATUS.LOADED) {\n getElement();\n }\n }, [status, getElement]);\n\n // Handles `title` and `description` changes\n useEffect(() => {\n if (!canUseDOM() || !previousProps || previousProps.src !== src) {\n return;\n }\n\n if (previousProps.title !== title || previousProps.description !== description) {\n getElement();\n }\n }, [description, getElement, previousProps, src, title]);\n\n // handle state\n useEffect(() => {\n if (!previousState) {\n return;\n }\n\n switch (status) {\n case STATUS.LOADING: {\n if (previousState.status !== STATUS.LOADING) {\n getContent();\n }\n\n break;\n }\n case STATUS.LOADED: {\n if (previousState.status !== STATUS.LOADED) {\n getElement();\n }\n\n break;\n }\n case STATUS.READY: {\n if (previousState.status !== STATUS.READY) {\n onLoad?.(src, isCached);\n }\n\n break;\n }\n }\n }, [getContent, getElement, isCached, onLoad, previousState, src, status]);\n\n const elementProps = omit(\n props,\n 'baseURL',\n 'cacheRequests',\n 'children',\n 'description',\n 'fetchOptions',\n 'innerRef',\n 'loader',\n 'onError',\n 'onLoad',\n 'preProcessor',\n 'src',\n 'title',\n 'uniqueHash',\n 'uniquifyIDs',\n );\n\n if (!canUseDOM()) {\n return loader;\n }\n\n if (element) {\n return cloneElement(element as ReactElement, {\n ref: innerRef,\n ...elementProps,\n });\n }\n\n if (([STATUS.UNSUPPORTED, STATUS.FAILED] as Status[]).includes(status)) {\n return children;\n }\n\n return loader;\n}\n\nexport default function InlineSVG(props: Props) {\n if (!cacheStore) {\n cacheStore = new CacheStore();\n }\n\n const { loader } = props;\n const [isReady, setReady] = useState(cacheStore.isReady);\n\n useEffect(() => {\n if (isReady) {\n return;\n }\n\n cacheStore.onReady(() => {\n setReady(true);\n });\n }, [isReady]);\n\n if (!isReady) {\n return loader;\n }\n\n return <ReactInlineSVG {...props} />;\n}\n\nexport * from './types';\n","export const CACHE_NAME = 'react-inlinesvg';\nexport const CACHE_MAX_RETRIES = 10;\n\nexport const STATUS = {\n IDLE: 'idle',\n LOADING: 'loading',\n LOADED: 'loaded',\n FAILED: 'failed',\n READY: 'ready',\n UNSUPPORTED: 'unsupported',\n} as const;\n","import type { PlainObject } from '../types';\n\nfunction randomCharacter(character: string) {\n return character[Math.floor(Math.random() * character.length)];\n}\n\nexport function canUseDOM(): boolean {\n return !!(typeof window !== 'undefined' && window.document?.createElement);\n}\n\nexport function isSupportedEnvironment(): boolean {\n return supportsInlineSVG() && typeof window !== 'undefined' && window !== null;\n}\n\n/**\n * Remove properties from an object\n */\nexport function omit<T extends PlainObject, K extends keyof T>(\n input: T,\n ...filter: K[]\n): Omit<T, K> {\n const output: any = {};\n\n for (const key in input) {\n if ({}.hasOwnProperty.call(input, key)) {\n if (!filter.includes(key as unknown as K)) {\n output[key] = input[key];\n }\n }\n }\n\n return output as Omit<T, K>;\n}\n\nexport function randomString(length: number): string {\n const letters = 'abcdefghijklmnopqrstuvwxyz';\n const numbers = '1234567890';\n const charset = `${letters}${letters.toUpperCase()}${numbers}`;\n\n let R = '';\n\n for (let index = 0; index < length; index++) {\n R += randomCharacter(charset);\n }\n\n return R;\n}\n\nexport async function request(url: string, options?: RequestInit) {\n const response = await fetch(url, options);\n const contentType = response.headers.get('content-type');\n const [fileType] = (contentType ?? '').split(/ ?; ?/);\n\n if (response.status > 299) {\n throw new Error('Not found');\n }\n\n if (!['image/svg+xml', 'text/plain'].some(d => fileType.includes(d))) {\n throw new Error(`Content type isn't valid: ${fileType}`);\n }\n\n return response.text();\n}\n\nexport function sleep(seconds = 1) {\n return new Promise(resolve => {\n setTimeout(resolve, seconds * 1000);\n });\n}\n\nexport function supportsInlineSVG(): boolean {\n /* c8 ignore next 3 */\n if (!document) {\n return false;\n }\n\n const div = document.createElement('div');\n\n div.innerHTML = '<svg />';\n const svg = div.firstChild as SVGSVGElement;\n\n return !!svg && svg.namespaceURI === 'http://www.w3.org/2000/svg';\n}\n","import { CACHE_MAX_RETRIES, CACHE_NAME, STATUS } from '../config';\nimport { StorageItem } from '../types';\n\nimport { canUseDOM, request, sleep } from './helpers';\n\nexport default class CacheStore {\n private cacheApi: Cache | undefined;\n private readonly cacheStore: Map<string, StorageItem>;\n private readonly subscribers: Array<() => void> = [];\n public isReady = false;\n\n constructor() {\n this.cacheStore = new Map<string, StorageItem>();\n\n let cacheName = CACHE_NAME;\n let usePersistentCache = false;\n\n if (canUseDOM()) {\n cacheName = window.REACT_INLINESVG_CACHE_NAME ?? CACHE_NAME;\n usePersistentCache = !!window.REACT_INLINESVG_PERSISTENT_CACHE && 'caches' in window;\n }\n\n if (usePersistentCache) {\n caches\n .open(cacheName)\n .then(cache => {\n this.cacheApi = cache;\n })\n .catch(error => {\n // eslint-disable-next-line no-console\n console.error(`Failed to open cache: ${error.message}`);\n this.cacheApi = undefined;\n })\n .finally(() => {\n this.isReady = true;\n // Copy to avoid mutation issues\n const callbacks = [...this.subscribers];\n\n // Clear array efficiently\n this.subscribers.length = 0;\n\n callbacks.forEach(callback => {\n try {\n callback();\n } catch (error: any) {\n // eslint-disable-next-line no-console\n console.error(`Error in CacheStore subscriber callback: ${error.message}`);\n }\n });\n });\n } else {\n this.isReady = true;\n }\n }\n\n public onReady(callback: () => void) {\n if (this.isReady) {\n callback();\n } else {\n this.subscribers.push(callback);\n }\n }\n\n public async get(url: string, fetchOptions?: RequestInit) {\n await (this.cacheApi\n ? this.fetchAndAddToPersistentCache(url, fetchOptions)\n : this.fetchAndAddToInternalCache(url, fetchOptions));\n\n return this.cacheStore.get(url)?.content ?? '';\n }\n\n public set(url: string, data: StorageItem) {\n this.cacheStore.set(url, data);\n }\n\n public isCached(url: string) {\n return this.cacheStore.get(url)?.status === STATUS.LOADED;\n }\n\n private async fetchAndAddToInternalCache(url: string, fetchOptions?: RequestInit) {\n const cache = this.cacheStore.get(url);\n\n if (cache?.status === STATUS.LOADING) {\n await this.handleLoading(url, async () => {\n this.cacheStore.set(url, { content: '', status: STATUS.IDLE });\n await this.fetchAndAddToInternalCache(url, fetchOptions);\n });\n\n return;\n }\n\n if (!cache?.content) {\n this.cacheStore.set(url, { content: '', status: STATUS.LOADING });\n\n try {\n const content = await request(url, fetchOptions);\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n } catch (error: any) {\n this.cacheStore.set(url, { content: '', status: STATUS.FAILED });\n throw error;\n }\n }\n }\n\n private async fetchAndAddToPersistentCache(url: string, fetchOptions?: RequestInit) {\n const cache = this.cacheStore.get(url);\n\n if (cache?.status === STATUS.LOADED) {\n return;\n }\n\n if (cache?.status === STATUS.LOADING) {\n await this.handleLoading(url, async () => {\n this.cacheStore.set(url, { content: '', status: STATUS.IDLE });\n await this.fetchAndAddToPersistentCache(url, fetchOptions);\n });\n\n return;\n }\n\n this.cacheStore.set(url, { content: '', status: STATUS.LOADING });\n\n const data = await this.cacheApi?.match(url);\n\n if (data) {\n const content = await data.text();\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n\n return;\n }\n\n try {\n await this.cacheApi?.add(new Request(url, fetchOptions));\n\n const response = await this.cacheApi?.match(url);\n const content = (await response?.text()) ?? '';\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n } catch (error: any) {\n this.cacheStore.set(url, { content: '', status: STATUS.FAILED });\n throw error;\n }\n }\n\n private async handleLoading(url: string, callback: () => Promise<void>) {\n for (let retryCount = 0; retryCount < CACHE_MAX_RETRIES; retryCount++) {\n if (this.cacheStore.get(url)?.status !== STATUS.LOADING) {\n return;\n }\n\n // eslint-disable-next-line no-await-in-loop\n await sleep(0.1);\n }\n\n await callback();\n }\n\n public keys(): Array<string> {\n return [...this.cacheStore.keys()];\n }\n\n public data(): Array<Record<string, StorageItem>> {\n return [...this.cacheStore.entries()].map(([key, value]) => ({ [key]: value }));\n }\n\n public async delete(url: string) {\n if (this.cacheApi) {\n await this.cacheApi.delete(url);\n }\n\n this.cacheStore.delete(url);\n }\n\n public async clear() {\n if (this.cacheApi) {\n const keys = await this.cacheApi.keys();\n\n await Promise.allSettled(keys.map(key => this.cacheApi!.delete(key)));\n }\n\n this.cacheStore.clear();\n }\n}\n","import { useEffect, useRef } from 'react';\n\nexport function usePrevious<T>(state: T): T | undefined {\n const ref = useRef<T>(undefined);\n\n useEffect(() => {\n ref.current = state;\n });\n\n return ref.current;\n}\n","import convert from 'react-from-dom';\n\nimport { Props, State } from '../types';\n\ninterface GetNodeOptions extends Props, Pick<State, 'content'> {\n handleError: (error: Error) => void;\n hash: string;\n}\n\ninterface UpdateSVGAttributesOptions extends Pick<Props, 'baseURL' | 'uniquifyIDs'> {\n hash: string;\n}\n\nexport function getNode(options: GetNodeOptions) {\n const {\n baseURL,\n content,\n description,\n handleError,\n hash,\n preProcessor,\n title,\n uniquifyIDs = false,\n } = options;\n\n try {\n const svgText = processSVG(content, preProcessor);\n const node = convert(svgText, { nodeOnly: true });\n\n if (!node || !(node instanceof SVGSVGElement)) {\n throw new Error('Could not convert the src to a DOM Node');\n }\n\n const svg = updateSVGAttributes(node, { baseURL, hash, uniquifyIDs });\n\n if (description) {\n const originalDesc = svg.querySelector('desc');\n\n if (originalDesc?.parentNode) {\n originalDesc.parentNode.removeChild(originalDesc);\n }\n\n const descElement = document.createElementNS('http://www.w3.org/2000/svg', 'desc');\n\n descElement.innerHTML = description;\n svg.prepend(descElement);\n }\n\n if (typeof title !== 'undefined') {\n const originalTitle = svg.querySelector('title');\n\n if (originalTitle?.parentNode) {\n originalTitle.parentNode.removeChild(originalTitle);\n }\n\n if (title) {\n const titleElement = document.createElementNS('http://www.w3.org/2000/svg', 'title');\n\n titleElement.innerHTML = title;\n svg.prepend(titleElement);\n }\n }\n\n return svg;\n } catch (error: any) {\n return handleError(error);\n }\n}\n\nexport function processSVG(content: string, preProcessor?: Props['preProcessor']) {\n if (preProcessor) {\n return preProcessor(content);\n }\n\n return content;\n}\n\nexport function updateSVGAttributes(\n node: SVGSVGElement,\n options: UpdateSVGAttributesOptions,\n): SVGSVGElement {\n const { baseURL = '', hash, uniquifyIDs } = options;\n const replaceableAttributes = ['id', 'href', 'xlink:href', 'xlink:role', 'xlink:arcrole'];\n const linkAttributes = ['href', 'xlink:href'];\n const isDataValue = (name: string, value: string) =>\n linkAttributes.includes(name) && (value ? !value.includes('#') : false);\n\n if (!uniquifyIDs) {\n return node;\n }\n\n [...node.children].forEach(d => {\n if (d.attributes?.length) {\n const attributes = Object.values(d.attributes).map(a => {\n const attribute = a;\n const match = /url\\((.*?)\\)/.exec(a.value);\n\n if (match?.[1]) {\n attribute.value = a.value.replace(match[0], `url(${baseURL}${match[1]}__${hash})`);\n }\n\n return attribute;\n });\n\n replaceableAttributes.forEach(r => {\n const attribute = attributes.find(a => a.name === r);\n\n if (attribute && !isDataValue(r, attribute.value)) {\n attribute.value = `${attribute.value}__${hash}`;\n }\n });\n }\n\n if (d.children.length) {\n return updateSVGAttributes(d as SVGSVGElement, options);\n }\n\n return d;\n });\n\n return node;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,gBASO;AACP,IAAAC,yBAAoB;;;ACVb,IAAM,aAAa;AACnB,IAAM,oBAAoB;AAE1B,IAAM,SAAS;AAAA,EACpB,MAAM;AAAA,EACN,SAAS;AAAA,EACT,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,OAAO;AAAA,EACP,aAAa;AACf;;;ACRA,SAAS,gBAAgB,WAAmB;AAC1C,SAAO,UAAU,KAAK,MAAM,KAAK,OAAO,IAAI,UAAU,MAAM,CAAC;AAC/D;AAEO,SAAS,YAAqB;AACnC,SAAO,CAAC,EAAE,OAAO,WAAW,eAAe,OAAO,UAAU;AAC9D;AAEO,SAAS,yBAAkC;AAChD,SAAO,kBAAkB,KAAK,OAAO,WAAW,eAAe,WAAW;AAC5E;AAKO,SAAS,KACd,UACG,QACS;AACZ,QAAM,SAAc,CAAC;AAErB,aAAW,OAAO,OAAO;AACvB,QAAI,CAAC,EAAE,eAAe,KAAK,OAAO,GAAG,GAAG;AACtC,UAAI,CAAC,OAAO,SAAS,GAAmB,GAAG;AACzC,eAAO,GAAG,IAAI,MAAM,GAAG;A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AAA,MACF;AAAA,MACA,KAAK,OAAO,OAAO;AACjB,YAAI,cAAc,WAAW,OAAO,OAAO;AACzC,mBAAS,KAAK,QAAQ;AAAA,QACxB;AAEA;AAAA,MACF;AAAA,IACF;AAAA,EACF,GAAG,CAAC,YAAY,YAAY,UAAU,QAAQ,eAAe,KAAK,MAAM,CAAC;AAEzE,QAAM,eAAe;AAAA,IACnB;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AAEA,MAAI,CAAC,UAAU,GAAG;AAChB,WAAO;AAAA,EACT;AAEA,MAAI,SAAS;AACX,eAAO,4BAAa,SAAyB;AAAA,MAC3C,KAAK;AAAA,MACL,GAAG;AAAA,IACL,CAAC;AAAA,EACH;AAEA,MAAK,CAAC,OAAO,aAAa,OAAO,MAAM,EAAe,SAAS,MAAM,GAAG;AACtE,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAEe,SAAR,UAA2B,OAAc;AAC9C,MAAI,CAAC,YAAY;AACf,iBAAa,IAAI,WAAW;AAAA,EAC9B;AAEA,QAAM,EAAE,OAAO,IAAI;AACnB,QAAM,CAAC,SAAS,QAAQ,QAAI,wBAAS,WAAW,OAAO;AAEvD,+BAAU,MAAM;AACd,QAAI,SAAS;AACX;AAAA,IACF;AAEA,eAAW,QAAQ,MAAM;AACvB,eAAS,IAAI;AAAA,IACf,CAAC;AAAA,EACH,GAAG,CAAC,OAAO,CAAC;AAEZ,MAAI,CAAC,SAAS;AACZ,WAAO;AAAA,EACT;AAEA,SAAO,8BAAAC,QAAA,cAAC,kBAAgB,GAAG,OAAO;AACpC;","names":["import_react","import_react_from_dom","convert","previousState","convert","React"]}
|
package/dist/index.mjs
CHANGED
|
@@ -32,7 +32,7 @@ function randomCharacter(character) {
|
|
|
32
32
|
return character[Math.floor(Math.random() * character.length)];
|
|
33
33
|
}
|
|
34
34
|
function canUseDOM() {
|
|
35
|
-
return !!window
|
|
35
|
+
return !!(typeof window !== "undefined" && window.document?.createElement);
|
|
36
36
|
}
|
|
37
37
|
function isSupportedEnvironment() {
|
|
38
38
|
return supportsInlineSVG() && typeof window !== "undefined" && window !== null;
|
|
@@ -104,9 +104,18 @@ var CacheStore = class {
|
|
|
104
104
|
this.cacheApi = cache;
|
|
105
105
|
}).catch((error) => {
|
|
106
106
|
console.error(`Failed to open cache: ${error.message}`);
|
|
107
|
+
this.cacheApi = void 0;
|
|
107
108
|
}).finally(() => {
|
|
108
109
|
this.isReady = true;
|
|
109
|
-
this.subscribers
|
|
110
|
+
const callbacks = [...this.subscribers];
|
|
111
|
+
this.subscribers.length = 0;
|
|
112
|
+
callbacks.forEach((callback) => {
|
|
113
|
+
try {
|
|
114
|
+
callback();
|
|
115
|
+
} catch (error) {
|
|
116
|
+
console.error(`Error in CacheStore subscriber callback: ${error.message}`);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
110
119
|
});
|
|
111
120
|
} else {
|
|
112
121
|
this.isReady = true;
|
|
@@ -179,14 +188,13 @@ var CacheStore = class {
|
|
|
179
188
|
}
|
|
180
189
|
}
|
|
181
190
|
async handleLoading(url, callback) {
|
|
182
|
-
let retryCount = 0;
|
|
183
|
-
|
|
191
|
+
for (let retryCount = 0; retryCount < CACHE_MAX_RETRIES; retryCount++) {
|
|
192
|
+
if (this.cacheStore.get(url)?.status !== STATUS.LOADING) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
184
195
|
await sleep(0.1);
|
|
185
|
-
retryCount += 1;
|
|
186
|
-
}
|
|
187
|
-
if (retryCount >= CACHE_MAX_RETRIES) {
|
|
188
|
-
await callback();
|
|
189
196
|
}
|
|
197
|
+
await callback();
|
|
190
198
|
}
|
|
191
199
|
keys() {
|
|
192
200
|
return [...this.cacheStore.keys()];
|
|
@@ -203,9 +211,7 @@ var CacheStore = class {
|
|
|
203
211
|
async clear() {
|
|
204
212
|
if (this.cacheApi) {
|
|
205
213
|
const keys = await this.cacheApi.keys();
|
|
206
|
-
|
|
207
|
-
await this.cacheApi.delete(key);
|
|
208
|
-
}
|
|
214
|
+
await Promise.allSettled(keys.map((key) => this.cacheApi.delete(key)));
|
|
209
215
|
}
|
|
210
216
|
this.cacheStore.clear();
|
|
211
217
|
}
|
|
@@ -375,7 +381,7 @@ function ReactInlineSVG(props) {
|
|
|
375
381
|
status: STATUS.READY
|
|
376
382
|
});
|
|
377
383
|
} catch (error) {
|
|
378
|
-
handleError(
|
|
384
|
+
handleError(error);
|
|
379
385
|
}
|
|
380
386
|
}, [content, handleError, props]);
|
|
381
387
|
const getContent = useCallback(async () => {
|
|
@@ -415,7 +421,7 @@ function ReactInlineSVG(props) {
|
|
|
415
421
|
() => {
|
|
416
422
|
isActive.current = true;
|
|
417
423
|
if (!canUseDOM() || isInitialized.current) {
|
|
418
|
-
return
|
|
424
|
+
return void 0;
|
|
419
425
|
}
|
|
420
426
|
try {
|
|
421
427
|
if (status === STATUS.IDLE) {
|
|
@@ -439,10 +445,7 @@ function ReactInlineSVG(props) {
|
|
|
439
445
|
[]
|
|
440
446
|
);
|
|
441
447
|
useEffect2(() => {
|
|
442
|
-
if (!canUseDOM()) {
|
|
443
|
-
return;
|
|
444
|
-
}
|
|
445
|
-
if (!previousProps) {
|
|
448
|
+
if (!canUseDOM() || !previousProps) {
|
|
446
449
|
return;
|
|
447
450
|
}
|
|
448
451
|
if (previousProps.src !== src) {
|
|
@@ -451,22 +454,44 @@ function ReactInlineSVG(props) {
|
|
|
451
454
|
return;
|
|
452
455
|
}
|
|
453
456
|
load();
|
|
454
|
-
}
|
|
457
|
+
}
|
|
458
|
+
}, [handleError, load, previousProps, src]);
|
|
459
|
+
useEffect2(() => {
|
|
460
|
+
if (status === STATUS.LOADED) {
|
|
455
461
|
getElement();
|
|
456
462
|
}
|
|
457
|
-
}, [
|
|
463
|
+
}, [status, getElement]);
|
|
458
464
|
useEffect2(() => {
|
|
459
|
-
if (!
|
|
465
|
+
if (!canUseDOM() || !previousProps || previousProps.src !== src) {
|
|
460
466
|
return;
|
|
461
467
|
}
|
|
462
|
-
if (
|
|
463
|
-
getContent();
|
|
464
|
-
}
|
|
465
|
-
if (previousState.status !== STATUS.LOADED && status === STATUS.LOADED) {
|
|
468
|
+
if (previousProps.title !== title || previousProps.description !== description) {
|
|
466
469
|
getElement();
|
|
467
470
|
}
|
|
468
|
-
|
|
469
|
-
|
|
471
|
+
}, [description, getElement, previousProps, src, title]);
|
|
472
|
+
useEffect2(() => {
|
|
473
|
+
if (!previousState) {
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
switch (status) {
|
|
477
|
+
case STATUS.LOADING: {
|
|
478
|
+
if (previousState.status !== STATUS.LOADING) {
|
|
479
|
+
getContent();
|
|
480
|
+
}
|
|
481
|
+
break;
|
|
482
|
+
}
|
|
483
|
+
case STATUS.LOADED: {
|
|
484
|
+
if (previousState.status !== STATUS.LOADED) {
|
|
485
|
+
getElement();
|
|
486
|
+
}
|
|
487
|
+
break;
|
|
488
|
+
}
|
|
489
|
+
case STATUS.READY: {
|
|
490
|
+
if (previousState.status !== STATUS.READY) {
|
|
491
|
+
onLoad?.(src, isCached);
|
|
492
|
+
}
|
|
493
|
+
break;
|
|
494
|
+
}
|
|
470
495
|
}
|
|
471
496
|
}, [getContent, getElement, isCached, onLoad, previousState, src, status]);
|
|
472
497
|
const elementProps = omit(
|
|
@@ -505,16 +530,15 @@ function InlineSVG(props) {
|
|
|
505
530
|
cacheStore = new CacheStore();
|
|
506
531
|
}
|
|
507
532
|
const { loader } = props;
|
|
508
|
-
const hasCallback = useRef2(false);
|
|
509
533
|
const [isReady, setReady] = useState(cacheStore.isReady);
|
|
510
534
|
useEffect2(() => {
|
|
511
|
-
if (
|
|
512
|
-
|
|
513
|
-
setReady(true);
|
|
514
|
-
});
|
|
515
|
-
hasCallback.current = true;
|
|
535
|
+
if (isReady) {
|
|
536
|
+
return;
|
|
516
537
|
}
|
|
517
|
-
|
|
538
|
+
cacheStore.onReady(() => {
|
|
539
|
+
setReady(true);
|
|
540
|
+
});
|
|
541
|
+
}, [isReady]);
|
|
518
542
|
if (!isReady) {
|
|
519
543
|
return loader;
|
|
520
544
|
}
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.tsx","../src/config.ts","../src/modules/helpers.ts","../src/modules/cache.ts","../src/modules/hooks.tsx","../src/modules/utils.ts"],"sourcesContent":["import React, {\n cloneElement,\n isValidElement,\n ReactElement,\n useCallback,\n useEffect,\n useReducer,\n useRef,\n useState,\n} from 'react';\nimport convert from 'react-from-dom';\n\nimport { STATUS } from './config';\nimport CacheStore from './modules/cache';\nimport { canUseDOM, isSupportedEnvironment, omit, randomString, request } from './modules/helpers';\nimport { usePrevious } from './modules/hooks';\nimport { getNode } from './modules/utils';\nimport { FetchError, Props, State, Status } from './types';\n\n// eslint-disable-next-line import/no-mutable-exports\nexport let cacheStore: CacheStore;\n\nfunction ReactInlineSVG(props: Props) {\n const {\n cacheRequests = true,\n children = null,\n description,\n fetchOptions,\n innerRef,\n loader = null,\n onError,\n onLoad,\n src,\n title,\n uniqueHash,\n } = props;\n const [state, setState] = useReducer(\n (previousState: State, nextState: Partial<State>) => ({\n ...previousState,\n ...nextState,\n }),\n {\n content: '',\n element: null,\n\n isCached: cacheRequests && cacheStore.isCached(props.src),\n status: STATUS.IDLE,\n },\n );\n const { content, element, isCached, status } = state;\n const previousProps = usePrevious(props);\n const previousState = usePrevious(state);\n\n const hash = useRef(uniqueHash ?? randomString(8));\n const isActive = useRef(false);\n const isInitialized = useRef(false);\n\n const handleError = useCallback(\n (error: Error | FetchError) => {\n if (isActive.current) {\n setState({\n status:\n error.message === 'Browser does not support SVG' ? STATUS.UNSUPPORTED : STATUS.FAILED,\n });\n\n onError?.(error);\n }\n },\n [onError],\n );\n\n const handleLoad = useCallback((loadedContent: string, hasCache = false) => {\n if (isActive.current) {\n setState({\n content: loadedContent,\n isCached: hasCache,\n status: STATUS.LOADED,\n });\n }\n }, []);\n\n const fetchContent = useCallback(async () => {\n const responseContent: string = await request(src, fetchOptions);\n\n handleLoad(responseContent);\n }, [fetchOptions, handleLoad, src]);\n\n const getElement = useCallback(() => {\n try {\n const node = getNode({ ...props, handleError, hash: hash.current, content }) as Node;\n const convertedElement = convert(node);\n\n if (!convertedElement || !isValidElement(convertedElement)) {\n throw new Error('Could not convert the src to a React element');\n }\n\n setState({\n element: convertedElement,\n status: STATUS.READY,\n });\n } catch (error: any) {\n handleError(new Error(error.message));\n }\n }, [content, handleError, props]);\n\n const getContent = useCallback(async () => {\n const dataURI = /^data:image\\/svg[^,]*?(;base64)?,(.*)/u.exec(src);\n let inlineSrc;\n\n if (dataURI) {\n inlineSrc = dataURI[1] ? window.atob(dataURI[2]) : decodeURIComponent(dataURI[2]);\n } else if (src.includes('<svg')) {\n inlineSrc = src;\n }\n\n if (inlineSrc) {\n handleLoad(inlineSrc);\n\n return;\n }\n\n try {\n if (cacheRequests) {\n const cachedContent = await cacheStore.get(src, fetchOptions);\n\n handleLoad(cachedContent, true);\n } else {\n await fetchContent();\n }\n } catch (error: any) {\n handleError(error);\n }\n }, [cacheRequests, fetchContent, fetchOptions, handleError, handleLoad, src]);\n\n const load = useCallback(async () => {\n if (isActive.current) {\n setState({\n content: '',\n element: null,\n isCached: false,\n status: STATUS.LOADING,\n });\n }\n }, []);\n\n // Run on mount\n useEffect(\n () => {\n isActive.current = true;\n\n if (!canUseDOM() || isInitialized.current) {\n return () => undefined;\n }\n\n try {\n if (status === STATUS.IDLE) {\n if (!isSupportedEnvironment()) {\n throw new Error('Browser does not support SVG');\n }\n\n if (!src) {\n throw new Error('Missing src');\n }\n\n load();\n }\n } catch (error: any) {\n handleError(error);\n }\n\n isInitialized.current = true;\n\n return () => {\n isActive.current = false;\n };\n },\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [],\n );\n\n // Handle prop changes\n useEffect(() => {\n if (!canUseDOM()) {\n return;\n }\n\n if (!previousProps) {\n return;\n }\n\n if (previousProps.src !== src) {\n if (!src) {\n handleError(new Error('Missing src'));\n\n return;\n }\n\n load();\n } else if (previousProps.title !== title || previousProps.description !== description) {\n getElement();\n }\n }, [description, getElement, handleError, load, previousProps, src, title]);\n\n // handle state\n useEffect(() => {\n if (!previousState) {\n return;\n }\n\n if (previousState.status !== STATUS.LOADING && status === STATUS.LOADING) {\n getContent();\n }\n\n if (previousState.status !== STATUS.LOADED && status === STATUS.LOADED) {\n getElement();\n }\n\n if (previousState.status !== STATUS.READY && status === STATUS.READY) {\n onLoad?.(src, isCached);\n }\n }, [getContent, getElement, isCached, onLoad, previousState, src, status]);\n\n const elementProps = omit(\n props,\n 'baseURL',\n 'cacheRequests',\n 'children',\n 'description',\n 'fetchOptions',\n 'innerRef',\n 'loader',\n 'onError',\n 'onLoad',\n 'preProcessor',\n 'src',\n 'title',\n 'uniqueHash',\n 'uniquifyIDs',\n );\n\n if (!canUseDOM()) {\n return loader;\n }\n\n if (element) {\n return cloneElement(element as ReactElement<any>, {\n ref: innerRef,\n ...elementProps,\n });\n }\n\n if (([STATUS.UNSUPPORTED, STATUS.FAILED] as Status[]).includes(status)) {\n return children;\n }\n\n return loader;\n}\n\nexport default function InlineSVG(props: Props) {\n if (!cacheStore) {\n cacheStore = new CacheStore();\n }\n\n const { loader } = props;\n const hasCallback = useRef(false);\n const [isReady, setReady] = useState(cacheStore.isReady);\n\n useEffect(() => {\n if (!hasCallback.current) {\n cacheStore.onReady(() => {\n setReady(true);\n });\n\n hasCallback.current = true;\n }\n }, []);\n\n if (!isReady) {\n return loader;\n }\n\n return <ReactInlineSVG {...props} />;\n}\n\nexport * from './types';\n","export const CACHE_NAME = 'react-inlinesvg';\nexport const CACHE_MAX_RETRIES = 10;\n\nexport const STATUS = {\n IDLE: 'idle',\n LOADING: 'loading',\n LOADED: 'loaded',\n FAILED: 'failed',\n READY: 'ready',\n UNSUPPORTED: 'unsupported',\n} as const;\n","import type { PlainObject } from '../types';\n\nfunction randomCharacter(character: string) {\n return character[Math.floor(Math.random() * character.length)];\n}\n\nexport function canUseDOM(): boolean {\n return !!window?.document?.createElement;\n}\n\nexport function isSupportedEnvironment(): boolean {\n return supportsInlineSVG() && typeof window !== 'undefined' && window !== null;\n}\n\n/**\n * Remove properties from an object\n */\nexport function omit<T extends PlainObject, K extends keyof T>(\n input: T,\n ...filter: K[]\n): Omit<T, K> {\n const output: any = {};\n\n for (const key in input) {\n if ({}.hasOwnProperty.call(input, key)) {\n if (!filter.includes(key as unknown as K)) {\n output[key] = input[key];\n }\n }\n }\n\n return output as Omit<T, K>;\n}\n\nexport function randomString(length: number): string {\n const letters = 'abcdefghijklmnopqrstuvwxyz';\n const numbers = '1234567890';\n const charset = `${letters}${letters.toUpperCase()}${numbers}`;\n\n let R = '';\n\n for (let index = 0; index < length; index++) {\n R += randomCharacter(charset);\n }\n\n return R;\n}\n\nexport async function request(url: string, options?: RequestInit) {\n const response = await fetch(url, options);\n const contentType = response.headers.get('content-type');\n const [fileType] = (contentType ?? '').split(/ ?; ?/);\n\n if (response.status > 299) {\n throw new Error('Not found');\n }\n\n if (!['image/svg+xml', 'text/plain'].some(d => fileType.includes(d))) {\n throw new Error(`Content type isn't valid: ${fileType}`);\n }\n\n return response.text();\n}\n\nexport function sleep(seconds = 1) {\n return new Promise(resolve => {\n setTimeout(resolve, seconds * 1000);\n });\n}\n\nexport function supportsInlineSVG(): boolean {\n /* c8 ignore next 3 */\n if (!document) {\n return false;\n }\n\n const div = document.createElement('div');\n\n div.innerHTML = '<svg />';\n const svg = div.firstChild as SVGSVGElement;\n\n return !!svg && svg.namespaceURI === 'http://www.w3.org/2000/svg';\n}\n","import { CACHE_MAX_RETRIES, CACHE_NAME, STATUS } from '../config';\nimport { StorageItem } from '../types';\n\nimport { canUseDOM, request, sleep } from './helpers';\n\nexport default class CacheStore {\n private cacheApi: Cache | undefined;\n private readonly cacheStore: Map<string, StorageItem>;\n private readonly subscribers: Array<() => void> = [];\n public isReady = false;\n\n constructor() {\n this.cacheStore = new Map<string, StorageItem>();\n\n let cacheName = CACHE_NAME;\n let usePersistentCache = false;\n\n if (canUseDOM()) {\n cacheName = window.REACT_INLINESVG_CACHE_NAME ?? CACHE_NAME;\n usePersistentCache = !!window.REACT_INLINESVG_PERSISTENT_CACHE && 'caches' in window;\n }\n\n if (usePersistentCache) {\n caches\n .open(cacheName)\n .then(cache => {\n this.cacheApi = cache;\n })\n .catch(error => {\n // eslint-disable-next-line no-console\n console.error(`Failed to open cache: ${error.message}`);\n })\n .finally(() => {\n this.isReady = true;\n this.subscribers.forEach(callback => callback());\n });\n } else {\n this.isReady = true;\n }\n }\n\n public onReady(callback: () => void) {\n if (this.isReady) {\n callback();\n } else {\n this.subscribers.push(callback);\n }\n }\n\n public async get(url: string, fetchOptions?: RequestInit) {\n await (this.cacheApi\n ? this.fetchAndAddToPersistentCache(url, fetchOptions)\n : this.fetchAndAddToInternalCache(url, fetchOptions));\n\n return this.cacheStore.get(url)?.content ?? '';\n }\n\n public set(url: string, data: StorageItem) {\n this.cacheStore.set(url, data);\n }\n\n public isCached(url: string) {\n return this.cacheStore.get(url)?.status === STATUS.LOADED;\n }\n\n private async fetchAndAddToInternalCache(url: string, fetchOptions?: RequestInit) {\n const cache = this.cacheStore.get(url);\n\n if (cache?.status === STATUS.LOADING) {\n await this.handleLoading(url, async () => {\n this.cacheStore.set(url, { content: '', status: STATUS.IDLE });\n await this.fetchAndAddToInternalCache(url, fetchOptions);\n });\n\n return;\n }\n\n if (!cache?.content) {\n this.cacheStore.set(url, { content: '', status: STATUS.LOADING });\n\n try {\n const content = await request(url, fetchOptions);\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n } catch (error: any) {\n this.cacheStore.set(url, { content: '', status: STATUS.FAILED });\n throw error;\n }\n }\n }\n\n private async fetchAndAddToPersistentCache(url: string, fetchOptions?: RequestInit) {\n const cache = this.cacheStore.get(url);\n\n if (cache?.status === STATUS.LOADED) {\n return;\n }\n\n if (cache?.status === STATUS.LOADING) {\n await this.handleLoading(url, async () => {\n this.cacheStore.set(url, { content: '', status: STATUS.IDLE });\n await this.fetchAndAddToPersistentCache(url, fetchOptions);\n });\n\n return;\n }\n\n this.cacheStore.set(url, { content: '', status: STATUS.LOADING });\n\n const data = await this.cacheApi?.match(url);\n\n if (data) {\n const content = await data.text();\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n\n return;\n }\n\n try {\n await this.cacheApi?.add(new Request(url, fetchOptions));\n\n const response = await this.cacheApi?.match(url);\n const content = (await response?.text()) ?? '';\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n } catch (error: any) {\n this.cacheStore.set(url, { content: '', status: STATUS.FAILED });\n throw error;\n }\n }\n\n private async handleLoading(url: string, callback: () => Promise<void>) {\n let retryCount = 0;\n\n while (this.cacheStore.get(url)?.status === STATUS.LOADING && retryCount < CACHE_MAX_RETRIES) {\n // eslint-disable-next-line no-await-in-loop\n await sleep(0.1);\n retryCount += 1;\n }\n\n if (retryCount >= CACHE_MAX_RETRIES) {\n await callback();\n }\n }\n\n public keys(): Array<string> {\n return [...this.cacheStore.keys()];\n }\n\n public data(): Array<Record<string, StorageItem>> {\n return [...this.cacheStore.entries()].map(([key, value]) => ({ [key]: value }));\n }\n\n public async delete(url: string) {\n if (this.cacheApi) {\n await this.cacheApi.delete(url);\n }\n\n this.cacheStore.delete(url);\n }\n\n public async clear() {\n if (this.cacheApi) {\n const keys = await this.cacheApi.keys();\n\n for (const key of keys) {\n // eslint-disable-next-line no-await-in-loop\n await this.cacheApi.delete(key);\n }\n }\n\n this.cacheStore.clear();\n }\n}\n","import { useEffect, useRef } from 'react';\n\nexport function usePrevious<T>(state: T): T | undefined {\n const ref = useRef<T>(undefined);\n\n useEffect(() => {\n ref.current = state;\n });\n\n return ref.current;\n}\n","import convert from 'react-from-dom';\n\nimport { Props, State } from '../types';\n\ninterface GetNodeOptions extends Props, Pick<State, 'content'> {\n handleError: (error: Error) => void;\n hash: string;\n}\n\ninterface UpdateSVGAttributesOptions extends Pick<Props, 'baseURL' | 'uniquifyIDs'> {\n hash: string;\n}\n\nexport function getNode(options: GetNodeOptions) {\n const {\n baseURL,\n content,\n description,\n handleError,\n hash,\n preProcessor,\n title,\n uniquifyIDs = false,\n } = options;\n\n try {\n const svgText = processSVG(content, preProcessor);\n const node = convert(svgText, { nodeOnly: true });\n\n if (!node || !(node instanceof SVGSVGElement)) {\n throw new Error('Could not convert the src to a DOM Node');\n }\n\n const svg = updateSVGAttributes(node, { baseURL, hash, uniquifyIDs });\n\n if (description) {\n const originalDesc = svg.querySelector('desc');\n\n if (originalDesc?.parentNode) {\n originalDesc.parentNode.removeChild(originalDesc);\n }\n\n const descElement = document.createElementNS('http://www.w3.org/2000/svg', 'desc');\n\n descElement.innerHTML = description;\n svg.prepend(descElement);\n }\n\n if (typeof title !== 'undefined') {\n const originalTitle = svg.querySelector('title');\n\n if (originalTitle?.parentNode) {\n originalTitle.parentNode.removeChild(originalTitle);\n }\n\n if (title) {\n const titleElement = document.createElementNS('http://www.w3.org/2000/svg', 'title');\n\n titleElement.innerHTML = title;\n svg.prepend(titleElement);\n }\n }\n\n return svg;\n } catch (error: any) {\n return handleError(error);\n }\n}\n\nexport function processSVG(content: string, preProcessor?: Props['preProcessor']) {\n if (preProcessor) {\n return preProcessor(content);\n }\n\n return content;\n}\n\nexport function updateSVGAttributes(\n node: SVGSVGElement,\n options: UpdateSVGAttributesOptions,\n): SVGSVGElement {\n const { baseURL = '', hash, uniquifyIDs } = options;\n const replaceableAttributes = ['id', 'href', 'xlink:href', 'xlink:role', 'xlink:arcrole'];\n const linkAttributes = ['href', 'xlink:href'];\n const isDataValue = (name: string, value: string) =>\n linkAttributes.includes(name) && (value ? !value.includes('#') : false);\n\n if (!uniquifyIDs) {\n return node;\n }\n\n [...node.children].forEach(d => {\n if (d.attributes?.length) {\n const attributes = Object.values(d.attributes).map(a => {\n const attribute = a;\n const match = /url\\((.*?)\\)/.exec(a.value);\n\n if (match?.[1]) {\n attribute.value = a.value.replace(match[0], `url(${baseURL}${match[1]}__${hash})`);\n }\n\n return attribute;\n });\n\n replaceableAttributes.forEach(r => {\n const attribute = attributes.find(a => a.name === r);\n\n if (attribute && !isDataValue(r, attribute.value)) {\n attribute.value = `${attribute.value}__${hash}`;\n }\n });\n }\n\n if (d.children.length) {\n return updateSVGAttributes(d as SVGSVGElement, options);\n }\n\n return d;\n });\n\n return node;\n}\n"],"mappings":";;;;;;AAAA,OAAO;AAAA,EACL;AAAA,EACA;AAAA,EAEA;AAAA,EACA,aAAAA;AAAA,EACA;AAAA,EACA,UAAAC;AAAA,EACA;AAAA,OACK;AACP,OAAOC,cAAa;;;ACVb,IAAM,aAAa;AACnB,IAAM,oBAAoB;AAE1B,IAAM,SAAS;AAAA,EACpB,MAAM;AAAA,EACN,SAAS;AAAA,EACT,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,OAAO;AAAA,EACP,aAAa;AACf;;;ACRA,SAAS,gBAAgB,WAAmB;AAC1C,SAAO,UAAU,KAAK,MAAM,KAAK,OAAO,IAAI,UAAU,MAAM,CAAC;AAC/D;AAEO,SAAS,YAAqB;AACnC,SAAO,CAAC,CAAC,QAAQ,UAAU;AAC7B;AAEO,SAAS,yBAAkC;AAChD,SAAO,kBAAkB,KAAK,OAAO,WAAW,eAAe,WAAW;AAC5E;AAKO,SAAS,KACd,UACG,QACS;AACZ,QAAM,SAAc,CAAC;AAErB,aAAW,OAAO,OAAO;AACvB,QAAI,CAAC,EAAE,eAAe,KAAK,OAAO,GAAG,GAAG;AACtC,UAAI,CAAC,OAAO,SAAS,GAAmB,GAAG;AACzC,eAAO,GAAG,IAAI,MAAM,GAAG;AAAA,MACzB;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;AAEO,SAAS,aAAa,QAAwB;AACnD,QAAM,UAAU;AAChB,QAAM,UAAU;AAChB,QAAM,UAAU,GAAG,OA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AAAA,EACT;AAEA,SAAO;AACT;AAEe,SAAR,UAA2B,OAAc;AAC9C,MAAI,CAAC,YAAY;AACf,iBAAa,IAAI,WAAW;AAAA,EAC9B;AAEA,QAAM,EAAE,OAAO,IAAI;AACnB,QAAM,cAAcF,QAAO,KAAK;AAChC,QAAM,CAAC,SAAS,QAAQ,IAAI,SAAS,WAAW,OAAO;AAEvD,EAAAE,WAAU,MAAM;AACd,QAAI,CAAC,YAAY,SAAS;AACxB,iBAAW,QAAQ,MAAM;AACvB,iBAAS,IAAI;AAAA,MACf,CAAC;AAED,kBAAY,UAAU;AAAA,IACxB;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,MAAI,CAAC,SAAS;AACZ,WAAO;AAAA,EACT;AAEA,SAAO,oCAAC,kBAAgB,GAAG,OAAO;AACpC;","names":["useEffect","useRef","convert","previousState","useRef","convert","useEffect"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.tsx","../src/config.ts","../src/modules/helpers.ts","../src/modules/cache.ts","../src/modules/hooks.tsx","../src/modules/utils.ts"],"sourcesContent":["import React, {\n cloneElement,\n isValidElement,\n ReactElement,\n useCallback,\n useEffect,\n useReducer,\n useRef,\n useState,\n} from 'react';\nimport convert from 'react-from-dom';\n\nimport { STATUS } from './config';\nimport CacheStore from './modules/cache';\nimport { canUseDOM, isSupportedEnvironment, omit, randomString, request } from './modules/helpers';\nimport { usePrevious } from './modules/hooks';\nimport { getNode } from './modules/utils';\nimport { FetchError, Props, State, Status } from './types';\n\n// eslint-disable-next-line import/no-mutable-exports\nexport let cacheStore: CacheStore;\n\nfunction ReactInlineSVG(props: Props) {\n const {\n cacheRequests = true,\n children = null,\n description,\n fetchOptions,\n innerRef,\n loader = null,\n onError,\n onLoad,\n src,\n title,\n uniqueHash,\n } = props;\n const [state, setState] = useReducer(\n (previousState: State, nextState: Partial<State>) => ({\n ...previousState,\n ...nextState,\n }),\n {\n content: '',\n element: null,\n\n isCached: cacheRequests && cacheStore.isCached(props.src),\n status: STATUS.IDLE,\n },\n );\n const { content, element, isCached, status } = state;\n const previousProps = usePrevious(props);\n const previousState = usePrevious(state);\n\n const hash = useRef(uniqueHash ?? randomString(8));\n const isActive = useRef(false);\n const isInitialized = useRef(false);\n\n const handleError = useCallback(\n (error: Error | FetchError) => {\n if (isActive.current) {\n setState({\n status:\n error.message === 'Browser does not support SVG' ? STATUS.UNSUPPORTED : STATUS.FAILED,\n });\n\n onError?.(error);\n }\n },\n [onError],\n );\n\n const handleLoad = useCallback((loadedContent: string, hasCache = false) => {\n if (isActive.current) {\n setState({\n content: loadedContent,\n isCached: hasCache,\n status: STATUS.LOADED,\n });\n }\n }, []);\n\n const fetchContent = useCallback(async () => {\n const responseContent: string = await request(src, fetchOptions);\n\n handleLoad(responseContent);\n }, [fetchOptions, handleLoad, src]);\n\n const getElement = useCallback(() => {\n try {\n const node = getNode({ ...props, handleError, hash: hash.current, content }) as Node;\n const convertedElement = convert(node);\n\n if (!convertedElement || !isValidElement(convertedElement)) {\n throw new Error('Could not convert the src to a React element');\n }\n\n setState({\n element: convertedElement,\n status: STATUS.READY,\n });\n } catch (error: any) {\n handleError(error);\n }\n }, [content, handleError, props]);\n\n const getContent = useCallback(async () => {\n const dataURI = /^data:image\\/svg[^,]*?(;base64)?,(.*)/u.exec(src);\n let inlineSrc;\n\n if (dataURI) {\n inlineSrc = dataURI[1] ? window.atob(dataURI[2]) : decodeURIComponent(dataURI[2]);\n } else if (src.includes('<svg')) {\n inlineSrc = src;\n }\n\n if (inlineSrc) {\n handleLoad(inlineSrc);\n\n return;\n }\n\n try {\n if (cacheRequests) {\n const cachedContent = await cacheStore.get(src, fetchOptions);\n\n handleLoad(cachedContent, true);\n } else {\n await fetchContent();\n }\n } catch (error: any) {\n handleError(error);\n }\n }, [cacheRequests, fetchContent, fetchOptions, handleError, handleLoad, src]);\n\n const load = useCallback(async () => {\n if (isActive.current) {\n setState({\n content: '',\n element: null,\n isCached: false,\n status: STATUS.LOADING,\n });\n }\n }, []);\n\n // Run on mount\n useEffect(\n () => {\n isActive.current = true;\n\n if (!canUseDOM() || isInitialized.current) {\n return undefined;\n }\n\n try {\n if (status === STATUS.IDLE) {\n if (!isSupportedEnvironment()) {\n throw new Error('Browser does not support SVG');\n }\n\n if (!src) {\n throw new Error('Missing src');\n }\n\n load();\n }\n } catch (error: any) {\n handleError(error);\n }\n\n isInitialized.current = true;\n\n return () => {\n isActive.current = false;\n };\n },\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [],\n );\n\n // Handles `src` changes\n useEffect(() => {\n if (!canUseDOM() || !previousProps) {\n return;\n }\n\n if (previousProps.src !== src) {\n if (!src) {\n handleError(new Error('Missing src'));\n\n return;\n }\n\n load();\n }\n }, [handleError, load, previousProps, src]);\n\n // Handles content loading\n useEffect(() => {\n if (status === STATUS.LOADED) {\n getElement();\n }\n }, [status, getElement]);\n\n // Handles `title` and `description` changes\n useEffect(() => {\n if (!canUseDOM() || !previousProps || previousProps.src !== src) {\n return;\n }\n\n if (previousProps.title !== title || previousProps.description !== description) {\n getElement();\n }\n }, [description, getElement, previousProps, src, title]);\n\n // handle state\n useEffect(() => {\n if (!previousState) {\n return;\n }\n\n switch (status) {\n case STATUS.LOADING: {\n if (previousState.status !== STATUS.LOADING) {\n getContent();\n }\n\n break;\n }\n case STATUS.LOADED: {\n if (previousState.status !== STATUS.LOADED) {\n getElement();\n }\n\n break;\n }\n case STATUS.READY: {\n if (previousState.status !== STATUS.READY) {\n onLoad?.(src, isCached);\n }\n\n break;\n }\n }\n }, [getContent, getElement, isCached, onLoad, previousState, src, status]);\n\n const elementProps = omit(\n props,\n 'baseURL',\n 'cacheRequests',\n 'children',\n 'description',\n 'fetchOptions',\n 'innerRef',\n 'loader',\n 'onError',\n 'onLoad',\n 'preProcessor',\n 'src',\n 'title',\n 'uniqueHash',\n 'uniquifyIDs',\n );\n\n if (!canUseDOM()) {\n return loader;\n }\n\n if (element) {\n return cloneElement(element as ReactElement, {\n ref: innerRef,\n ...elementProps,\n });\n }\n\n if (([STATUS.UNSUPPORTED, STATUS.FAILED] as Status[]).includes(status)) {\n return children;\n }\n\n return loader;\n}\n\nexport default function InlineSVG(props: Props) {\n if (!cacheStore) {\n cacheStore = new CacheStore();\n }\n\n const { loader } = props;\n const [isReady, setReady] = useState(cacheStore.isReady);\n\n useEffect(() => {\n if (isReady) {\n return;\n }\n\n cacheStore.onReady(() => {\n setReady(true);\n });\n }, [isReady]);\n\n if (!isReady) {\n return loader;\n }\n\n return <ReactInlineSVG {...props} />;\n}\n\nexport * from './types';\n","export const CACHE_NAME = 'react-inlinesvg';\nexport const CACHE_MAX_RETRIES = 10;\n\nexport const STATUS = {\n IDLE: 'idle',\n LOADING: 'loading',\n LOADED: 'loaded',\n FAILED: 'failed',\n READY: 'ready',\n UNSUPPORTED: 'unsupported',\n} as const;\n","import type { PlainObject } from '../types';\n\nfunction randomCharacter(character: string) {\n return character[Math.floor(Math.random() * character.length)];\n}\n\nexport function canUseDOM(): boolean {\n return !!(typeof window !== 'undefined' && window.document?.createElement);\n}\n\nexport function isSupportedEnvironment(): boolean {\n return supportsInlineSVG() && typeof window !== 'undefined' && window !== null;\n}\n\n/**\n * Remove properties from an object\n */\nexport function omit<T extends PlainObject, K extends keyof T>(\n input: T,\n ...filter: K[]\n): Omit<T, K> {\n const output: any = {};\n\n for (const key in input) {\n if ({}.hasOwnProperty.call(input, key)) {\n if (!filter.includes(key as unknown as K)) {\n output[key] = input[key];\n }\n }\n }\n\n return output as Omit<T, K>;\n}\n\nexport function randomString(length: number): string {\n const letters = 'abcdefghijklmnopqrstuvwxyz';\n const numbers = '1234567890';\n const charset = `${letters}${letters.toUpperCase()}${numbers}`;\n\n let R = '';\n\n for (let index = 0; index < length; index++) {\n R += randomCharacter(charset);\n }\n\n return R;\n}\n\nexport async function request(url: string, options?: RequestInit) {\n const response = await fetch(url, options);\n const contentType = response.headers.get('content-type');\n const [fileType] = (contentType ?? '').split(/ ?; ?/);\n\n if (response.status > 299) {\n throw new Error('Not found');\n }\n\n if (!['image/svg+xml', 'text/plain'].some(d => fileType.includes(d))) {\n throw new Error(`Content type isn't valid: ${fileType}`);\n }\n\n return response.text();\n}\n\nexport function sleep(seconds = 1) {\n return new Promise(resolve => {\n setTimeout(resolve, seconds * 1000);\n });\n}\n\nexport function supportsInlineSVG(): boolean {\n /* c8 ignore next 3 */\n if (!document) {\n return false;\n }\n\n const div = document.createElement('div');\n\n div.innerHTML = '<svg />';\n const svg = div.firstChild as SVGSVGElement;\n\n return !!svg && svg.namespaceURI === 'http://www.w3.org/2000/svg';\n}\n","import { CACHE_MAX_RETRIES, CACHE_NAME, STATUS } from '../config';\nimport { StorageItem } from '../types';\n\nimport { canUseDOM, request, sleep } from './helpers';\n\nexport default class CacheStore {\n private cacheApi: Cache | undefined;\n private readonly cacheStore: Map<string, StorageItem>;\n private readonly subscribers: Array<() => void> = [];\n public isReady = false;\n\n constructor() {\n this.cacheStore = new Map<string, StorageItem>();\n\n let cacheName = CACHE_NAME;\n let usePersistentCache = false;\n\n if (canUseDOM()) {\n cacheName = window.REACT_INLINESVG_CACHE_NAME ?? CACHE_NAME;\n usePersistentCache = !!window.REACT_INLINESVG_PERSISTENT_CACHE && 'caches' in window;\n }\n\n if (usePersistentCache) {\n caches\n .open(cacheName)\n .then(cache => {\n this.cacheApi = cache;\n })\n .catch(error => {\n // eslint-disable-next-line no-console\n console.error(`Failed to open cache: ${error.message}`);\n this.cacheApi = undefined;\n })\n .finally(() => {\n this.isReady = true;\n // Copy to avoid mutation issues\n const callbacks = [...this.subscribers];\n\n // Clear array efficiently\n this.subscribers.length = 0;\n\n callbacks.forEach(callback => {\n try {\n callback();\n } catch (error: any) {\n // eslint-disable-next-line no-console\n console.error(`Error in CacheStore subscriber callback: ${error.message}`);\n }\n });\n });\n } else {\n this.isReady = true;\n }\n }\n\n public onReady(callback: () => void) {\n if (this.isReady) {\n callback();\n } else {\n this.subscribers.push(callback);\n }\n }\n\n public async get(url: string, fetchOptions?: RequestInit) {\n await (this.cacheApi\n ? this.fetchAndAddToPersistentCache(url, fetchOptions)\n : this.fetchAndAddToInternalCache(url, fetchOptions));\n\n return this.cacheStore.get(url)?.content ?? '';\n }\n\n public set(url: string, data: StorageItem) {\n this.cacheStore.set(url, data);\n }\n\n public isCached(url: string) {\n return this.cacheStore.get(url)?.status === STATUS.LOADED;\n }\n\n private async fetchAndAddToInternalCache(url: string, fetchOptions?: RequestInit) {\n const cache = this.cacheStore.get(url);\n\n if (cache?.status === STATUS.LOADING) {\n await this.handleLoading(url, async () => {\n this.cacheStore.set(url, { content: '', status: STATUS.IDLE });\n await this.fetchAndAddToInternalCache(url, fetchOptions);\n });\n\n return;\n }\n\n if (!cache?.content) {\n this.cacheStore.set(url, { content: '', status: STATUS.LOADING });\n\n try {\n const content = await request(url, fetchOptions);\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n } catch (error: any) {\n this.cacheStore.set(url, { content: '', status: STATUS.FAILED });\n throw error;\n }\n }\n }\n\n private async fetchAndAddToPersistentCache(url: string, fetchOptions?: RequestInit) {\n const cache = this.cacheStore.get(url);\n\n if (cache?.status === STATUS.LOADED) {\n return;\n }\n\n if (cache?.status === STATUS.LOADING) {\n await this.handleLoading(url, async () => {\n this.cacheStore.set(url, { content: '', status: STATUS.IDLE });\n await this.fetchAndAddToPersistentCache(url, fetchOptions);\n });\n\n return;\n }\n\n this.cacheStore.set(url, { content: '', status: STATUS.LOADING });\n\n const data = await this.cacheApi?.match(url);\n\n if (data) {\n const content = await data.text();\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n\n return;\n }\n\n try {\n await this.cacheApi?.add(new Request(url, fetchOptions));\n\n const response = await this.cacheApi?.match(url);\n const content = (await response?.text()) ?? '';\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n } catch (error: any) {\n this.cacheStore.set(url, { content: '', status: STATUS.FAILED });\n throw error;\n }\n }\n\n private async handleLoading(url: string, callback: () => Promise<void>) {\n for (let retryCount = 0; retryCount < CACHE_MAX_RETRIES; retryCount++) {\n if (this.cacheStore.get(url)?.status !== STATUS.LOADING) {\n return;\n }\n\n // eslint-disable-next-line no-await-in-loop\n await sleep(0.1);\n }\n\n await callback();\n }\n\n public keys(): Array<string> {\n return [...this.cacheStore.keys()];\n }\n\n public data(): Array<Record<string, StorageItem>> {\n return [...this.cacheStore.entries()].map(([key, value]) => ({ [key]: value }));\n }\n\n public async delete(url: string) {\n if (this.cacheApi) {\n await this.cacheApi.delete(url);\n }\n\n this.cacheStore.delete(url);\n }\n\n public async clear() {\n if (this.cacheApi) {\n const keys = await this.cacheApi.keys();\n\n await Promise.allSettled(keys.map(key => this.cacheApi!.delete(key)));\n }\n\n this.cacheStore.clear();\n }\n}\n","import { useEffect, useRef } from 'react';\n\nexport function usePrevious<T>(state: T): T | undefined {\n const ref = useRef<T>(undefined);\n\n useEffect(() => {\n ref.current = state;\n });\n\n return ref.current;\n}\n","import convert from 'react-from-dom';\n\nimport { Props, State } from '../types';\n\ninterface GetNodeOptions extends Props, Pick<State, 'content'> {\n handleError: (error: Error) => void;\n hash: string;\n}\n\ninterface UpdateSVGAttributesOptions extends Pick<Props, 'baseURL' | 'uniquifyIDs'> {\n hash: string;\n}\n\nexport function getNode(options: GetNodeOptions) {\n const {\n baseURL,\n content,\n description,\n handleError,\n hash,\n preProcessor,\n title,\n uniquifyIDs = false,\n } = options;\n\n try {\n const svgText = processSVG(content, preProcessor);\n const node = convert(svgText, { nodeOnly: true });\n\n if (!node || !(node instanceof SVGSVGElement)) {\n throw new Error('Could not convert the src to a DOM Node');\n }\n\n const svg = updateSVGAttributes(node, { baseURL, hash, uniquifyIDs });\n\n if (description) {\n const originalDesc = svg.querySelector('desc');\n\n if (originalDesc?.parentNode) {\n originalDesc.parentNode.removeChild(originalDesc);\n }\n\n const descElement = document.createElementNS('http://www.w3.org/2000/svg', 'desc');\n\n descElement.innerHTML = description;\n svg.prepend(descElement);\n }\n\n if (typeof title !== 'undefined') {\n const originalTitle = svg.querySelector('title');\n\n if (originalTitle?.parentNode) {\n originalTitle.parentNode.removeChild(originalTitle);\n }\n\n if (title) {\n const titleElement = document.createElementNS('http://www.w3.org/2000/svg', 'title');\n\n titleElement.innerHTML = title;\n svg.prepend(titleElement);\n }\n }\n\n return svg;\n } catch (error: any) {\n return handleError(error);\n }\n}\n\nexport function processSVG(content: string, preProcessor?: Props['preProcessor']) {\n if (preProcessor) {\n return preProcessor(content);\n }\n\n return content;\n}\n\nexport function updateSVGAttributes(\n node: SVGSVGElement,\n options: UpdateSVGAttributesOptions,\n): SVGSVGElement {\n const { baseURL = '', hash, uniquifyIDs } = options;\n const replaceableAttributes = ['id', 'href', 'xlink:href', 'xlink:role', 'xlink:arcrole'];\n const linkAttributes = ['href', 'xlink:href'];\n const isDataValue = (name: string, value: string) =>\n linkAttributes.includes(name) && (value ? !value.includes('#') : false);\n\n if (!uniquifyIDs) {\n return node;\n }\n\n [...node.children].forEach(d => {\n if (d.attributes?.length) {\n const attributes = Object.values(d.attributes).map(a => {\n const attribute = a;\n const match = /url\\((.*?)\\)/.exec(a.value);\n\n if (match?.[1]) {\n attribute.value = a.value.replace(match[0], `url(${baseURL}${match[1]}__${hash})`);\n }\n\n return attribute;\n });\n\n replaceableAttributes.forEach(r => {\n const attribute = attributes.find(a => a.name === r);\n\n if (attribute && !isDataValue(r, attribute.value)) {\n attribute.value = `${attribute.value}__${hash}`;\n }\n });\n }\n\n if (d.children.length) {\n return updateSVGAttributes(d as SVGSVGElement, options);\n }\n\n return d;\n });\n\n return node;\n}\n"],"mappings":";;;;;;AAAA,OAAO;AAAA,EACL;AAAA,EACA;AAAA,EAEA;AAAA,EACA,aAAAA;AAAA,EACA;AAAA,EACA,UAAAC;AAAA,EACA;AAAA,OACK;AACP,OAAOC,cAAa;;;ACVb,IAAM,aAAa;AACnB,IAAM,oBAAoB;AAE1B,IAAM,SAAS;AAAA,EACpB,MAAM;AAAA,EACN,SAAS;AAAA,EACT,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR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AAY;AACf,iBAAa,IAAI,WAAW;AAAA,EAC9B;AAEA,QAAM,EAAE,OAAO,IAAI;AACnB,QAAM,CAAC,SAAS,QAAQ,IAAI,SAAS,WAAW,OAAO;AAEvD,EAAAA,WAAU,MAAM;AACd,QAAI,SAAS;AACX;AAAA,IACF;AAEA,eAAW,QAAQ,MAAM;AACvB,eAAS,IAAI;AAAA,IACf,CAAC;AAAA,EACH,GAAG,CAAC,OAAO,CAAC;AAEZ,MAAI,CAAC,SAAS;AACZ,WAAO;AAAA,EACT;AAEA,SAAO,oCAAC,kBAAgB,GAAG,OAAO;AACpC;","names":["useEffect","useRef","convert","previousState","useRef","convert","useEffect"]}
|
package/dist/provider.js
CHANGED
|
@@ -27,7 +27,7 @@ module.exports = __toCommonJS(provider_exports);
|
|
|
27
27
|
|
|
28
28
|
// src/modules/helpers.ts
|
|
29
29
|
function canUseDOM() {
|
|
30
|
-
return !!window
|
|
30
|
+
return !!(typeof window !== "undefined" && window.document?.createElement);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
// src/provider.tsx
|
package/dist/provider.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/provider.tsx","../src/modules/helpers.ts"],"sourcesContent":["import { ReactNode } from 'react';\n\nimport { canUseDOM } from './modules/helpers';\n\ninterface Props {\n children: ReactNode;\n name?: string;\n}\n\nexport default function CacheProvider({ children, name }: Props) {\n if (canUseDOM()) {\n window.REACT_INLINESVG_CACHE_NAME = name;\n window.REACT_INLINESVG_PERSISTENT_CACHE = true;\n }\n\n return children;\n}\n","import type { PlainObject } from '../types';\n\nfunction randomCharacter(character: string) {\n return character[Math.floor(Math.random() * character.length)];\n}\n\nexport function canUseDOM(): boolean {\n return !!window
|
|
1
|
+
{"version":3,"sources":["../src/provider.tsx","../src/modules/helpers.ts"],"sourcesContent":["import { ReactNode } from 'react';\n\nimport { canUseDOM } from './modules/helpers';\n\ninterface Props {\n children: ReactNode;\n name?: string;\n}\n\nexport default function CacheProvider({ children, name }: Props) {\n if (canUseDOM()) {\n window.REACT_INLINESVG_CACHE_NAME = name;\n window.REACT_INLINESVG_PERSISTENT_CACHE = true;\n }\n\n return children;\n}\n","import type { PlainObject } from '../types';\n\nfunction randomCharacter(character: string) {\n return character[Math.floor(Math.random() * character.length)];\n}\n\nexport function canUseDOM(): boolean {\n return !!(typeof window !== 'undefined' && window.document?.createElement);\n}\n\nexport function isSupportedEnvironment(): boolean {\n return supportsInlineSVG() && typeof window !== 'undefined' && window !== null;\n}\n\n/**\n * Remove properties from an object\n */\nexport function omit<T extends PlainObject, K extends keyof T>(\n input: T,\n ...filter: K[]\n): Omit<T, K> {\n const output: any = {};\n\n for (const key in input) {\n if ({}.hasOwnProperty.call(input, key)) {\n if (!filter.includes(key as unknown as K)) {\n output[key] = input[key];\n }\n }\n }\n\n return output as Omit<T, K>;\n}\n\nexport function randomString(length: number): string {\n const letters = 'abcdefghijklmnopqrstuvwxyz';\n const numbers = '1234567890';\n const charset = `${letters}${letters.toUpperCase()}${numbers}`;\n\n let R = '';\n\n for (let index = 0; index < length; index++) {\n R += randomCharacter(charset);\n }\n\n return R;\n}\n\nexport async function request(url: string, options?: RequestInit) {\n const response = await fetch(url, options);\n const contentType = response.headers.get('content-type');\n const [fileType] = (contentType ?? '').split(/ ?; ?/);\n\n if (response.status > 299) {\n throw new Error('Not found');\n }\n\n if (!['image/svg+xml', 'text/plain'].some(d => fileType.includes(d))) {\n throw new Error(`Content type isn't valid: ${fileType}`);\n }\n\n return response.text();\n}\n\nexport function sleep(seconds = 1) {\n return new Promise(resolve => {\n setTimeout(resolve, seconds * 1000);\n });\n}\n\nexport function supportsInlineSVG(): boolean {\n /* c8 ignore next 3 */\n if (!document) {\n return false;\n }\n\n const div = document.createElement('div');\n\n div.innerHTML = '<svg />';\n const svg = div.firstChild as SVGSVGElement;\n\n return !!svg && svg.namespaceURI === 'http://www.w3.org/2000/svg';\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACMO,SAAS,YAAqB;AACnC,SAAO,CAAC,EAAE,OAAO,WAAW,eAAe,OAAO,UAAU;AAC9D;;;ADCe,SAAR,cAA+B,EAAE,UAAU,KAAK,GAAU;AAC/D,MAAI,UAAU,GAAG;AACf,WAAO,6BAA6B;AACpC,WAAO,mCAAmC;AAAA,EAC5C;AAEA,SAAO;AACT;","names":[]}
|
package/dist/provider.mjs
CHANGED
package/dist/provider.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/modules/helpers.ts","../src/provider.tsx"],"sourcesContent":["import type { PlainObject } from '../types';\n\nfunction randomCharacter(character: string) {\n return character[Math.floor(Math.random() * character.length)];\n}\n\nexport function canUseDOM(): boolean {\n return !!window
|
|
1
|
+
{"version":3,"sources":["../src/modules/helpers.ts","../src/provider.tsx"],"sourcesContent":["import type { PlainObject } from '../types';\n\nfunction randomCharacter(character: string) {\n return character[Math.floor(Math.random() * character.length)];\n}\n\nexport function canUseDOM(): boolean {\n return !!(typeof window !== 'undefined' && window.document?.createElement);\n}\n\nexport function isSupportedEnvironment(): boolean {\n return supportsInlineSVG() && typeof window !== 'undefined' && window !== null;\n}\n\n/**\n * Remove properties from an object\n */\nexport function omit<T extends PlainObject, K extends keyof T>(\n input: T,\n ...filter: K[]\n): Omit<T, K> {\n const output: any = {};\n\n for (const key in input) {\n if ({}.hasOwnProperty.call(input, key)) {\n if (!filter.includes(key as unknown as K)) {\n output[key] = input[key];\n }\n }\n }\n\n return output as Omit<T, K>;\n}\n\nexport function randomString(length: number): string {\n const letters = 'abcdefghijklmnopqrstuvwxyz';\n const numbers = '1234567890';\n const charset = `${letters}${letters.toUpperCase()}${numbers}`;\n\n let R = '';\n\n for (let index = 0; index < length; index++) {\n R += randomCharacter(charset);\n }\n\n return R;\n}\n\nexport async function request(url: string, options?: RequestInit) {\n const response = await fetch(url, options);\n const contentType = response.headers.get('content-type');\n const [fileType] = (contentType ?? '').split(/ ?; ?/);\n\n if (response.status > 299) {\n throw new Error('Not found');\n }\n\n if (!['image/svg+xml', 'text/plain'].some(d => fileType.includes(d))) {\n throw new Error(`Content type isn't valid: ${fileType}`);\n }\n\n return response.text();\n}\n\nexport function sleep(seconds = 1) {\n return new Promise(resolve => {\n setTimeout(resolve, seconds * 1000);\n });\n}\n\nexport function supportsInlineSVG(): boolean {\n /* c8 ignore next 3 */\n if (!document) {\n return false;\n }\n\n const div = document.createElement('div');\n\n div.innerHTML = '<svg />';\n const svg = div.firstChild as SVGSVGElement;\n\n return !!svg && svg.namespaceURI === 'http://www.w3.org/2000/svg';\n}\n","import { ReactNode } from 'react';\n\nimport { canUseDOM } from './modules/helpers';\n\ninterface Props {\n children: ReactNode;\n name?: string;\n}\n\nexport default function CacheProvider({ children, name }: Props) {\n if (canUseDOM()) {\n window.REACT_INLINESVG_CACHE_NAME = name;\n window.REACT_INLINESVG_PERSISTENT_CACHE = true;\n }\n\n return children;\n}\n"],"mappings":";;;AAMO,SAAS,YAAqB;AACnC,SAAO,CAAC,EAAE,OAAO,WAAW,eAAe,OAAO,UAAU;AAC9D;;;ACCe,SAAR,cAA+B,EAAE,UAAU,KAAK,GAAU;AAC/D,MAAI,UAAU,GAAG;AACf,WAAO,6BAA6B;AACpC,WAAO,mCAAmC;AAAA,EAC5C;AAEA,SAAO;AACT;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-inlinesvg",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"description": "An SVG loader for React",
|
|
5
5
|
"author": "Gil Barbara <gilbarbara@gmail.com>",
|
|
6
6
|
"contributors": [
|
|
@@ -60,12 +60,12 @@
|
|
|
60
60
|
"@gilbarbara/tsconfig": "^0.2.3",
|
|
61
61
|
"@size-limit/preset-small-lib": "^11.1.6",
|
|
62
62
|
"@testing-library/jest-dom": "^6.6.3",
|
|
63
|
-
"@testing-library/react": "^16.
|
|
64
|
-
"@types/node": "^22.
|
|
63
|
+
"@testing-library/react": "^16.2.0",
|
|
64
|
+
"@types/node": "^22.13.4",
|
|
65
65
|
"@types/react": "^18.3.18",
|
|
66
66
|
"@types/react-dom": "^18.3.5",
|
|
67
67
|
"@vitejs/plugin-react": "^4.3.4",
|
|
68
|
-
"@vitest/coverage-v8": "^
|
|
68
|
+
"@vitest/coverage-v8": "^3.0.6",
|
|
69
69
|
"browser-cache-mock": "^0.1.7",
|
|
70
70
|
"del-cli": "^6.0.0",
|
|
71
71
|
"fix-tsup-cjs": "^1.2.0",
|
|
@@ -79,9 +79,9 @@
|
|
|
79
79
|
"size-limit": "^11.1.6",
|
|
80
80
|
"start-server-and-test": "^2.0.10",
|
|
81
81
|
"ts-node": "^10.9.2",
|
|
82
|
-
"tsup": "^8.3.
|
|
82
|
+
"tsup": "^8.3.6",
|
|
83
83
|
"typescript": "^5.7.3",
|
|
84
|
-
"vitest": "^
|
|
84
|
+
"vitest": "^3.0.6",
|
|
85
85
|
"vitest-fetch-mock": "^0.4.3"
|
|
86
86
|
},
|
|
87
87
|
"scripts": {
|
package/src/index.tsx
CHANGED
|
@@ -99,7 +99,7 @@ function ReactInlineSVG(props: Props) {
|
|
|
99
99
|
status: STATUS.READY,
|
|
100
100
|
});
|
|
101
101
|
} catch (error: any) {
|
|
102
|
-
handleError(
|
|
102
|
+
handleError(error);
|
|
103
103
|
}
|
|
104
104
|
}, [content, handleError, props]);
|
|
105
105
|
|
|
@@ -149,7 +149,7 @@ function ReactInlineSVG(props: Props) {
|
|
|
149
149
|
isActive.current = true;
|
|
150
150
|
|
|
151
151
|
if (!canUseDOM() || isInitialized.current) {
|
|
152
|
-
return
|
|
152
|
+
return undefined;
|
|
153
153
|
}
|
|
154
154
|
|
|
155
155
|
try {
|
|
@@ -178,13 +178,9 @@ function ReactInlineSVG(props: Props) {
|
|
|
178
178
|
[],
|
|
179
179
|
);
|
|
180
180
|
|
|
181
|
-
//
|
|
181
|
+
// Handles `src` changes
|
|
182
182
|
useEffect(() => {
|
|
183
|
-
if (!canUseDOM()) {
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
if (!previousProps) {
|
|
183
|
+
if (!canUseDOM() || !previousProps) {
|
|
188
184
|
return;
|
|
189
185
|
}
|
|
190
186
|
|
|
@@ -196,27 +192,55 @@ function ReactInlineSVG(props: Props) {
|
|
|
196
192
|
}
|
|
197
193
|
|
|
198
194
|
load();
|
|
199
|
-
}
|
|
195
|
+
}
|
|
196
|
+
}, [handleError, load, previousProps, src]);
|
|
197
|
+
|
|
198
|
+
// Handles content loading
|
|
199
|
+
useEffect(() => {
|
|
200
|
+
if (status === STATUS.LOADED) {
|
|
200
201
|
getElement();
|
|
201
202
|
}
|
|
202
|
-
}, [
|
|
203
|
+
}, [status, getElement]);
|
|
203
204
|
|
|
204
|
-
//
|
|
205
|
+
// Handles `title` and `description` changes
|
|
205
206
|
useEffect(() => {
|
|
206
|
-
if (!
|
|
207
|
+
if (!canUseDOM() || !previousProps || previousProps.src !== src) {
|
|
207
208
|
return;
|
|
208
209
|
}
|
|
209
210
|
|
|
210
|
-
if (
|
|
211
|
-
|
|
211
|
+
if (previousProps.title !== title || previousProps.description !== description) {
|
|
212
|
+
getElement();
|
|
212
213
|
}
|
|
214
|
+
}, [description, getElement, previousProps, src, title]);
|
|
213
215
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
+
// handle state
|
|
217
|
+
useEffect(() => {
|
|
218
|
+
if (!previousState) {
|
|
219
|
+
return;
|
|
216
220
|
}
|
|
217
221
|
|
|
218
|
-
|
|
219
|
-
|
|
222
|
+
switch (status) {
|
|
223
|
+
case STATUS.LOADING: {
|
|
224
|
+
if (previousState.status !== STATUS.LOADING) {
|
|
225
|
+
getContent();
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
case STATUS.LOADED: {
|
|
231
|
+
if (previousState.status !== STATUS.LOADED) {
|
|
232
|
+
getElement();
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
case STATUS.READY: {
|
|
238
|
+
if (previousState.status !== STATUS.READY) {
|
|
239
|
+
onLoad?.(src, isCached);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
220
244
|
}
|
|
221
245
|
}, [getContent, getElement, isCached, onLoad, previousState, src, status]);
|
|
222
246
|
|
|
@@ -243,7 +267,7 @@ function ReactInlineSVG(props: Props) {
|
|
|
243
267
|
}
|
|
244
268
|
|
|
245
269
|
if (element) {
|
|
246
|
-
return cloneElement(element as ReactElement
|
|
270
|
+
return cloneElement(element as ReactElement, {
|
|
247
271
|
ref: innerRef,
|
|
248
272
|
...elementProps,
|
|
249
273
|
});
|
|
@@ -262,18 +286,17 @@ export default function InlineSVG(props: Props) {
|
|
|
262
286
|
}
|
|
263
287
|
|
|
264
288
|
const { loader } = props;
|
|
265
|
-
const hasCallback = useRef(false);
|
|
266
289
|
const [isReady, setReady] = useState(cacheStore.isReady);
|
|
267
290
|
|
|
268
291
|
useEffect(() => {
|
|
269
|
-
if (
|
|
270
|
-
|
|
271
|
-
setReady(true);
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
hasCallback.current = true;
|
|
292
|
+
if (isReady) {
|
|
293
|
+
return;
|
|
275
294
|
}
|
|
276
|
-
|
|
295
|
+
|
|
296
|
+
cacheStore.onReady(() => {
|
|
297
|
+
setReady(true);
|
|
298
|
+
});
|
|
299
|
+
}, [isReady]);
|
|
277
300
|
|
|
278
301
|
if (!isReady) {
|
|
279
302
|
return loader;
|
package/src/modules/cache.ts
CHANGED
|
@@ -29,10 +29,24 @@ export default class CacheStore {
|
|
|
29
29
|
.catch(error => {
|
|
30
30
|
// eslint-disable-next-line no-console
|
|
31
31
|
console.error(`Failed to open cache: ${error.message}`);
|
|
32
|
+
this.cacheApi = undefined;
|
|
32
33
|
})
|
|
33
34
|
.finally(() => {
|
|
34
35
|
this.isReady = true;
|
|
35
|
-
|
|
36
|
+
// Copy to avoid mutation issues
|
|
37
|
+
const callbacks = [...this.subscribers];
|
|
38
|
+
|
|
39
|
+
// Clear array efficiently
|
|
40
|
+
this.subscribers.length = 0;
|
|
41
|
+
|
|
42
|
+
callbacks.forEach(callback => {
|
|
43
|
+
try {
|
|
44
|
+
callback();
|
|
45
|
+
} catch (error: any) {
|
|
46
|
+
// eslint-disable-next-line no-console
|
|
47
|
+
console.error(`Error in CacheStore subscriber callback: ${error.message}`);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
36
50
|
});
|
|
37
51
|
} else {
|
|
38
52
|
this.isReady = true;
|
|
@@ -131,17 +145,16 @@ export default class CacheStore {
|
|
|
131
145
|
}
|
|
132
146
|
|
|
133
147
|
private async handleLoading(url: string, callback: () => Promise<void>) {
|
|
134
|
-
let retryCount = 0;
|
|
148
|
+
for (let retryCount = 0; retryCount < CACHE_MAX_RETRIES; retryCount++) {
|
|
149
|
+
if (this.cacheStore.get(url)?.status !== STATUS.LOADING) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
135
152
|
|
|
136
|
-
while (this.cacheStore.get(url)?.status === STATUS.LOADING && retryCount < CACHE_MAX_RETRIES) {
|
|
137
153
|
// eslint-disable-next-line no-await-in-loop
|
|
138
154
|
await sleep(0.1);
|
|
139
|
-
retryCount += 1;
|
|
140
155
|
}
|
|
141
156
|
|
|
142
|
-
|
|
143
|
-
await callback();
|
|
144
|
-
}
|
|
157
|
+
await callback();
|
|
145
158
|
}
|
|
146
159
|
|
|
147
160
|
public keys(): Array<string> {
|
|
@@ -164,10 +177,7 @@ export default class CacheStore {
|
|
|
164
177
|
if (this.cacheApi) {
|
|
165
178
|
const keys = await this.cacheApi.keys();
|
|
166
179
|
|
|
167
|
-
|
|
168
|
-
// eslint-disable-next-line no-await-in-loop
|
|
169
|
-
await this.cacheApi.delete(key);
|
|
170
|
-
}
|
|
180
|
+
await Promise.allSettled(keys.map(key => this.cacheApi!.delete(key)));
|
|
171
181
|
}
|
|
172
182
|
|
|
173
183
|
this.cacheStore.clear();
|
package/src/modules/helpers.ts
CHANGED
|
@@ -5,7 +5,7 @@ function randomCharacter(character: string) {
|
|
|
5
5
|
}
|
|
6
6
|
|
|
7
7
|
export function canUseDOM(): boolean {
|
|
8
|
-
return !!window
|
|
8
|
+
return !!(typeof window !== 'undefined' && window.document?.createElement);
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
export function isSupportedEnvironment(): boolean {
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ReactNode,
|
|
1
|
+
import { ReactNode, Ref, SVGProps } from 'react';
|
|
2
2
|
|
|
3
3
|
import { STATUS } from './config';
|
|
4
4
|
|
|
@@ -14,7 +14,7 @@ export type Props = Simplify<
|
|
|
14
14
|
children?: ReactNode;
|
|
15
15
|
description?: string;
|
|
16
16
|
fetchOptions?: RequestInit;
|
|
17
|
-
innerRef?:
|
|
17
|
+
innerRef?: Ref<SVGElement | null>;
|
|
18
18
|
loader?: ReactNode;
|
|
19
19
|
onError?: ErrorCallback;
|
|
20
20
|
onLoad?: LoadCallback;
|