react-inlinesvg 3.1.0-1 → 4.0.0-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 +7 -5
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +5 -3
- package/dist/index.mjs.map +1 -1
- package/dist/provider.js +1 -0
- package/dist/provider.js.map +1 -1
- package/dist/provider.mjs +1 -0
- package/dist/provider.mjs.map +1 -1
- package/package.json +1 -1
- package/src/index.tsx +6 -3
- package/src/provider.tsx +2 -0
package/README.md
CHANGED
|
@@ -143,7 +143,7 @@ Since version 4.x, the `cacheStore` is an instance of the [Cache](src/cache.ts)
|
|
|
143
143
|
|
|
144
144
|
### Persistent cache
|
|
145
145
|
|
|
146
|
-
Starting with version 4.x you can use browser's [cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache) to store the SVGs
|
|
146
|
+
Starting with version 4.x, you can use the browser's [cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache) to store the SVGs.
|
|
147
147
|
To set it up, you need to wrap your app with the provider:
|
|
148
148
|
|
|
149
149
|
```typescript
|
|
@@ -158,15 +158,17 @@ createRoot(document.getElementById('root')!).render(
|
|
|
158
158
|
);
|
|
159
159
|
```
|
|
160
160
|
|
|
161
|
+
> Be aware of the limitations of the [Cache API](https://developer.mozilla.org/en-US/docs/Web/API/Cache).
|
|
162
|
+
|
|
161
163
|
## Browser Support
|
|
162
164
|
|
|
163
165
|
Any browsers that support inlining [SVGs](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg) and [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) will work.
|
|
164
166
|
|
|
165
|
-
If you need to support legacy browsers
|
|
167
|
+
If you need to support legacy browsers, include a polyfill for `fetch` and `Number.isNaN` in your app. Take a look at [react-app-polyfill](https://www.npmjs.com/package/react-app-polyfill) or [polyfill.io](https://polyfill.io/v3/).
|
|
166
168
|
|
|
167
169
|
## CORS
|
|
168
170
|
|
|
169
|
-
If you are loading remote SVGs, you
|
|
171
|
+
If you are loading remote SVGs, you must ensure it has [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) support.
|
|
170
172
|
|
|
171
173
|
## Why do you need this package?
|
|
172
174
|
|
|
@@ -196,5 +198,5 @@ The SVG [`<use>`](http://css-tricks.com/svg-use-external-source) element can be
|
|
|
196
198
|
|
|
197
199
|
## Credits
|
|
198
200
|
|
|
199
|
-
Thanks to [@matthewwithanm](https://github.com/matthewwithanm) for creating this component and so kindly
|
|
200
|
-
I'll definitely keep the good work! ❤️
|
|
201
|
+
Thanks to [@matthewwithanm](https://github.com/matthewwithanm) for creating this component and so kindly transferring it to me.
|
|
202
|
+
I'll definitely keep up the good work! ❤️
|
package/dist/index.d.mts
CHANGED
|
@@ -69,6 +69,6 @@ declare class CacheStore {
|
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
declare let cacheStore: CacheStore;
|
|
72
|
-
declare function InlineSVG(props: Props): string | number | boolean | Iterable<React.ReactNode> | react_jsx_runtime.JSX.Element | null;
|
|
72
|
+
declare function InlineSVG(props: Props): string | number | boolean | Iterable<React.ReactNode> | react_jsx_runtime.JSX.Element | null | undefined;
|
|
73
73
|
|
|
74
74
|
export { ErrorCallback, FetchError, LoadCallback, PlainObject, PreProcessorCallback, Props, State, Status, StorageItem, cacheStore, InlineSVG as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -69,6 +69,6 @@ declare class CacheStore {
|
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
declare let cacheStore: CacheStore;
|
|
72
|
-
declare function InlineSVG(props: Props): string | number | boolean | Iterable<React.ReactNode> | react_jsx_runtime.JSX.Element | null;
|
|
72
|
+
declare function InlineSVG(props: Props): string | number | boolean | Iterable<React.ReactNode> | react_jsx_runtime.JSX.Element | null | undefined;
|
|
73
73
|
|
|
74
74
|
export { ErrorCallback, FetchError, LoadCallback, PlainObject, PreProcessorCallback, Props, State, Status, StorageItem, cacheStore, InlineSVG as default };
|
package/dist/index.js
CHANGED
|
@@ -1299,18 +1299,18 @@ function InlineSVG(props) {
|
|
|
1299
1299
|
cacheStore = new CacheStore();
|
|
1300
1300
|
}
|
|
1301
1301
|
var loader = props.loader;
|
|
1302
|
+
var hasCallback = React.useRef(false);
|
|
1302
1303
|
var _React_useState = _sliced_to_array(React.useState(cacheStore.isReady), 2), isReady = _React_useState[0], setReady = _React_useState[1];
|
|
1303
1304
|
React.useEffect(function() {
|
|
1304
|
-
if (!
|
|
1305
|
+
if (!hasCallback.current) {
|
|
1305
1306
|
cacheStore.onReady(function() {
|
|
1306
1307
|
setReady(true);
|
|
1307
1308
|
});
|
|
1309
|
+
hasCallback.current = true;
|
|
1308
1310
|
}
|
|
1309
|
-
}, [
|
|
1310
|
-
isReady
|
|
1311
|
-
]);
|
|
1311
|
+
}, []);
|
|
1312
1312
|
if (!isReady) {
|
|
1313
|
-
return loader
|
|
1313
|
+
return loader;
|
|
1314
1314
|
}
|
|
1315
1315
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ReactInlineSVG, _object_spread({}, props));
|
|
1316
1316
|
}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.tsx","../src/config.ts","../src/helpers.ts","../src/cache.ts"],"names":["canUseDOMFlag","convert"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,YAAuB;AACvB,4BAAoB;;;ACDb,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;;;ACVA,mBAA2C;AAIpC,SAAS,YAAqB;AACnC,SAAO,aAAAA;AACT;AAEO,SA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AAS;AACZ,iBAAW,QAAQ,MAAM;AACvB,iBAAS,IAAI;AAAA,MACf,CAAC;AAAA,IACH;AAAA,EACF,GAAG,CAAC,OAAO,CAAC;AAEZ,MAAI,CAAC,SAAS;AACZ,WAAO,UAAU;AAAA,EACnB;AAEA,SAAO,4CAAC,kBAAgB,GAAG,OAAO;AACpC","sourcesContent":["import * as React from 'react';\nimport convert from 'react-from-dom';\n\nimport CacheStore from './cache';\nimport { STATUS } from './config';\nimport { canUseDOM, isSupportedEnvironment, omit, randomString, request } from './helpers';\nimport { FetchError, Props, State, Status } from './types';\n\n// eslint-disable-next-line import/no-mutable-exports\nexport let cacheStore: CacheStore;\n\nclass ReactInlineSVG extends React.PureComponent<Props, State> {\n private readonly hash: string;\n private isActive = false;\n private isInitialized = false;\n\n public static defaultProps = {\n cacheRequests: true,\n uniquifyIDs: false,\n };\n\n constructor(props: Props) {\n super(props);\n\n this.state = {\n content: '',\n element: null,\n isCached: !!props.cacheRequests && cacheStore.isCached(props.src),\n status: STATUS.IDLE,\n };\n\n this.hash = props.uniqueHash || randomString(8);\n }\n\n public componentDidMount(): void {\n this.isActive = true;\n\n if (!canUseDOM() || this.isInitialized) {\n return;\n }\n\n const { status } = this.state;\n const { src } = this.props;\n\n try {\n /* istanbul ignore else */\n if (status === STATUS.IDLE) {\n /* istanbul ignore else */\n if (!isSupportedEnvironment()) {\n throw new Error('Browser does not support SVG');\n }\n\n /* istanbul ignore else */\n if (!src) {\n throw new Error('Missing src');\n }\n\n this.load();\n }\n } catch (error: any) {\n this.handleError(error);\n }\n\n this.isInitialized = true;\n }\n\n public componentDidUpdate(previousProps: Props, previousState: State): void {\n if (!canUseDOM()) {\n return;\n }\n\n const { isCached, status } = this.state;\n const { onLoad, src } = this.props;\n\n if (previousState.status !== STATUS.READY && status === STATUS.READY) {\n /* istanbul ignore else */\n if (onLoad) {\n onLoad(src, isCached);\n }\n }\n\n if (previousProps.src !== src) {\n if (!src) {\n this.handleError(new Error('Missing src'));\n\n return;\n }\n\n this.load();\n }\n }\n\n public componentWillUnmount(): void {\n this.isActive = false;\n }\n\n private fetchContent = async () => {\n const { fetchOptions, src } = this.props;\n\n const content: string = await request(src, fetchOptions);\n\n this.handleLoad(content);\n };\n\n private getElement() {\n try {\n const node = this.getNode() as Node;\n const element = convert(node);\n\n if (!element || !React.isValidElement(element)) {\n throw new Error('Could not convert the src to a React element');\n }\n\n this.setState({\n element,\n status: STATUS.READY,\n });\n } catch (error: any) {\n this.handleError(new Error(error.message));\n }\n }\n\n private getNode() {\n const { description, title } = this.props;\n\n try {\n const svgText = this.processSVG();\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 = this.updateSVGAttributes(node);\n\n if (description) {\n const originalDesc = svg.querySelector('desc');\n\n if (originalDesc && 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 && 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 this.handleError(error);\n }\n }\n\n private handleError = (error: Error | FetchError) => {\n const { onError } = this.props;\n const status =\n error.message === 'Browser does not support SVG' ? STATUS.UNSUPPORTED : STATUS.FAILED;\n\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState({ status }, () => {\n /* istanbul ignore else */\n if (typeof onError === 'function') {\n onError(error);\n }\n });\n }\n };\n\n private handleLoad = (content: string, hasCache = false) => {\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState(\n {\n content,\n isCached: hasCache,\n status: STATUS.LOADED,\n },\n this.getElement,\n );\n }\n };\n\n private load() {\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState(\n {\n content: '',\n element: null,\n isCached: false,\n status: STATUS.LOADING,\n },\n async () => {\n const { cacheRequests, fetchOptions, src } = this.props;\n\n const dataURI = src.match(/^data:image\\/svg[^,]*?(;base64)?,(.*)/u);\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 this.handleLoad(inlineSrc);\n\n return;\n }\n\n try {\n if (cacheRequests) {\n const content = await cacheStore.get(src, fetchOptions);\n\n this.handleLoad(content, true);\n } else {\n await this.fetchContent();\n }\n } catch (error: any) {\n this.handleError(error);\n }\n },\n );\n }\n }\n\n private processSVG() {\n const { content } = this.state;\n const { preProcessor } = this.props;\n\n if (preProcessor) {\n return preProcessor(content);\n }\n\n return content;\n }\n\n private updateSVGAttributes(node: SVGSVGElement): SVGSVGElement {\n const { baseURL = '', uniquifyIDs } = this.props;\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 && d.attributes.length) {\n const attributes = Object.values(d.attributes).map(a => {\n const attribute = a;\n const match = a.value.match(/url\\((.*?)\\)/);\n\n if (match && match[1]) {\n attribute.value = a.value.replace(match[0], `url(${baseURL}${match[1]}__${this.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}__${this.hash}`;\n }\n });\n }\n\n if (d.children.length) {\n return this.updateSVGAttributes(d as SVGSVGElement);\n }\n\n return d;\n });\n\n return node;\n }\n\n public render(): React.ReactNode {\n const { element, status } = this.state;\n const { children = null, innerRef, loader = null } = this.props;\n const elementProps = omit(\n this.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 React.cloneElement(element as React.ReactElement, { ref: innerRef, ...elementProps });\n }\n\n if (([STATUS.UNSUPPORTED, STATUS.FAILED] as Status[]).includes(status)) {\n return children;\n }\n\n return loader;\n }\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] = React.useState(cacheStore.isReady);\n\n React.useEffect(() => {\n if (!isReady) {\n cacheStore.onReady(() => {\n setReady(true);\n });\n }\n }, [isReady]);\n\n if (!isReady) {\n return loader ?? null;\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 { canUseDOM as canUseDOMFlag } from 'exenv';\n\nimport type { PlainObject } from './types';\n\nexport function canUseDOM(): boolean {\n return canUseDOMFlag;\n}\n\nexport function isSupportedEnvironment(): boolean {\n return supportsInlineSVG() && typeof window !== 'undefined' && window !== null;\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 /* istanbul ignore next */\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\nfunction randomCharacter(character: string) {\n return character[Math.floor(Math.random() * character.length)];\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\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 /* istanbul ignore else */\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","import { CACHE_MAX_RETRIES, CACHE_NAME, STATUS } from './config';\nimport { request, sleep } from './helpers';\nimport { StorageItem } from './types';\n\nexport default class CacheStore {\n private cacheApi: Cache | undefined;\n private readonly cacheStore: Map<string, StorageItem>;\n private readonly subscribers: Array<() => void> = [];\n private readonly usePersistentCache: boolean;\n public isReady = false;\n\n constructor() {\n this.cacheStore = new Map<string, StorageItem>();\n\n this.usePersistentCache =\n 'REACT_INLINESVG_PERSISTENT_CACHE' in window && !!window.REACT_INLINESVG_PERSISTENT_CACHE;\n\n if (this.usePersistentCache) {\n caches.open(CACHE_NAME).then(cache => {\n this.cacheApi = cache;\n this.isReady = true;\n\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.usePersistentCache\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 // eslint-disable-next-line no-await-in-loop\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"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.tsx","../src/config.ts","../src/helpers.ts","../src/cache.ts"],"names":["canUseDOMFlag","convert"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,YAAuB;AACvB,4BAAoB;;;ACDb,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;;;ACVA,mBAA2C;AAIpC,SAAS,YAAqB;AACnC,SAAO,aAAAA;AACT;AAEO,SAAS,yBAAkC;AAChD,SAAO,kBAAkB,KAAK,OAAO,WAAW,eAAe,WAAW;AAC5E;AAEA,eAAsB,QAAQ,KAAa,SAAuB;AAChE,QAAM,WAAW,MAAM,MAAM,KAAK,OAAO;AACzC,QAAM,cAAc,SAAS,QAAQ,IAAI,cAAc;AACvD,QAAM,CAAC,QAA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AAEA,SAAO,4CAAC,kBAAgB,GAAG,OAAO;AACpC","sourcesContent":["import * as React from 'react';\nimport convert from 'react-from-dom';\n\nimport CacheStore from './cache';\nimport { STATUS } from './config';\nimport { canUseDOM, isSupportedEnvironment, omit, randomString, request } from './helpers';\nimport { FetchError, Props, State, Status } from './types';\n\n// eslint-disable-next-line import/no-mutable-exports\nexport let cacheStore: CacheStore;\n\nclass ReactInlineSVG extends React.PureComponent<Props, State> {\n private readonly hash: string;\n private isActive = false;\n private isInitialized = false;\n\n public static defaultProps = {\n cacheRequests: true,\n uniquifyIDs: false,\n };\n\n constructor(props: Props) {\n super(props);\n\n this.state = {\n content: '',\n element: null,\n isCached: !!props.cacheRequests && cacheStore.isCached(props.src),\n status: STATUS.IDLE,\n };\n\n this.hash = props.uniqueHash || randomString(8);\n }\n\n public componentDidMount(): void {\n this.isActive = true;\n\n if (!canUseDOM() || this.isInitialized) {\n return;\n }\n\n const { status } = this.state;\n const { src } = this.props;\n\n try {\n /* istanbul ignore else */\n if (status === STATUS.IDLE) {\n /* istanbul ignore else */\n if (!isSupportedEnvironment()) {\n throw new Error('Browser does not support SVG');\n }\n\n /* istanbul ignore else */\n if (!src) {\n throw new Error('Missing src');\n }\n\n this.load();\n }\n } catch (error: any) {\n this.handleError(error);\n }\n\n this.isInitialized = true;\n }\n\n public componentDidUpdate(previousProps: Props, previousState: State): void {\n if (!canUseDOM()) {\n return;\n }\n\n const { isCached, status } = this.state;\n const { onLoad, src } = this.props;\n\n if (previousState.status !== STATUS.READY && status === STATUS.READY) {\n /* istanbul ignore else */\n if (onLoad) {\n onLoad(src, isCached);\n }\n }\n\n if (previousProps.src !== src) {\n if (!src) {\n this.handleError(new Error('Missing src'));\n\n return;\n }\n\n this.load();\n }\n }\n\n public componentWillUnmount(): void {\n this.isActive = false;\n }\n\n private fetchContent = async () => {\n const { fetchOptions, src } = this.props;\n\n const content: string = await request(src, fetchOptions);\n\n this.handleLoad(content);\n };\n\n private getElement() {\n try {\n const node = this.getNode() as Node;\n const element = convert(node);\n\n if (!element || !React.isValidElement(element)) {\n throw new Error('Could not convert the src to a React element');\n }\n\n this.setState({\n element,\n status: STATUS.READY,\n });\n } catch (error: any) {\n this.handleError(new Error(error.message));\n }\n }\n\n private getNode() {\n const { description, title } = this.props;\n\n try {\n const svgText = this.processSVG();\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 = this.updateSVGAttributes(node);\n\n if (description) {\n const originalDesc = svg.querySelector('desc');\n\n if (originalDesc && 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 && 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 this.handleError(error);\n }\n }\n\n private handleError = (error: Error | FetchError) => {\n const { onError } = this.props;\n const status =\n error.message === 'Browser does not support SVG' ? STATUS.UNSUPPORTED : STATUS.FAILED;\n\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState({ status }, () => {\n /* istanbul ignore else */\n if (typeof onError === 'function') {\n onError(error);\n }\n });\n }\n };\n\n private handleLoad = (content: string, hasCache = false) => {\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState(\n {\n content,\n isCached: hasCache,\n status: STATUS.LOADED,\n },\n this.getElement,\n );\n }\n };\n\n private load() {\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState(\n {\n content: '',\n element: null,\n isCached: false,\n status: STATUS.LOADING,\n },\n async () => {\n const { cacheRequests, fetchOptions, src } = this.props;\n\n const dataURI = src.match(/^data:image\\/svg[^,]*?(;base64)?,(.*)/u);\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 this.handleLoad(inlineSrc);\n\n return;\n }\n\n try {\n if (cacheRequests) {\n const content = await cacheStore.get(src, fetchOptions);\n\n this.handleLoad(content, true);\n } else {\n await this.fetchContent();\n }\n } catch (error: any) {\n this.handleError(error);\n }\n },\n );\n }\n }\n\n private processSVG() {\n const { content } = this.state;\n const { preProcessor } = this.props;\n\n if (preProcessor) {\n return preProcessor(content);\n }\n\n return content;\n }\n\n private updateSVGAttributes(node: SVGSVGElement): SVGSVGElement {\n const { baseURL = '', uniquifyIDs } = this.props;\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 && d.attributes.length) {\n const attributes = Object.values(d.attributes).map(a => {\n const attribute = a;\n const match = a.value.match(/url\\((.*?)\\)/);\n\n if (match && match[1]) {\n attribute.value = a.value.replace(match[0], `url(${baseURL}${match[1]}__${this.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}__${this.hash}`;\n }\n });\n }\n\n if (d.children.length) {\n return this.updateSVGAttributes(d as SVGSVGElement);\n }\n\n return d;\n });\n\n return node;\n }\n\n public render(): React.ReactNode {\n const { element, status } = this.state;\n const { children = null, innerRef, loader = null } = this.props;\n const elementProps = omit(\n this.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 React.cloneElement(element as React.ReactElement, { ref: innerRef, ...elementProps });\n }\n\n if (([STATUS.UNSUPPORTED, STATUS.FAILED] as Status[]).includes(status)) {\n return children;\n }\n\n return loader;\n }\n}\n\nexport default function InlineSVG(props: Props) {\n if (!cacheStore) {\n cacheStore = new CacheStore();\n }\n\n const { loader } = props;\n const hasCallback = React.useRef(false);\n const [isReady, setReady] = React.useState(cacheStore.isReady);\n\n React.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 { canUseDOM as canUseDOMFlag } from 'exenv';\n\nimport type { PlainObject } from './types';\n\nexport function canUseDOM(): boolean {\n return canUseDOMFlag;\n}\n\nexport function isSupportedEnvironment(): boolean {\n return supportsInlineSVG() && typeof window !== 'undefined' && window !== null;\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 /* istanbul ignore next */\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\nfunction randomCharacter(character: string) {\n return character[Math.floor(Math.random() * character.length)];\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\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 /* istanbul ignore else */\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","import { CACHE_MAX_RETRIES, CACHE_NAME, STATUS } from './config';\nimport { request, sleep } from './helpers';\nimport { StorageItem } from './types';\n\nexport default class CacheStore {\n private cacheApi: Cache | undefined;\n private readonly cacheStore: Map<string, StorageItem>;\n private readonly subscribers: Array<() => void> = [];\n private readonly usePersistentCache: boolean;\n public isReady = false;\n\n constructor() {\n this.cacheStore = new Map<string, StorageItem>();\n\n this.usePersistentCache =\n 'REACT_INLINESVG_PERSISTENT_CACHE' in window && !!window.REACT_INLINESVG_PERSISTENT_CACHE;\n\n if (this.usePersistentCache) {\n caches.open(CACHE_NAME).then(cache => {\n this.cacheApi = cache;\n this.isReady = true;\n\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.usePersistentCache\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 // eslint-disable-next-line no-await-in-loop\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"]}
|
package/dist/index.mjs
CHANGED
|
@@ -454,16 +454,18 @@ function InlineSVG(props) {
|
|
|
454
454
|
cacheStore = new CacheStore();
|
|
455
455
|
}
|
|
456
456
|
const { loader } = props;
|
|
457
|
+
const hasCallback = React.useRef(false);
|
|
457
458
|
const [isReady, setReady] = React.useState(cacheStore.isReady);
|
|
458
459
|
React.useEffect(() => {
|
|
459
|
-
if (!
|
|
460
|
+
if (!hasCallback.current) {
|
|
460
461
|
cacheStore.onReady(() => {
|
|
461
462
|
setReady(true);
|
|
462
463
|
});
|
|
464
|
+
hasCallback.current = true;
|
|
463
465
|
}
|
|
464
|
-
}, [
|
|
466
|
+
}, []);
|
|
465
467
|
if (!isReady) {
|
|
466
|
-
return loader
|
|
468
|
+
return loader;
|
|
467
469
|
}
|
|
468
470
|
return /* @__PURE__ */ jsx(ReactInlineSVG, { ...props });
|
|
469
471
|
}
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.tsx","../src/config.ts","../src/helpers.ts","../src/cache.ts"],"sourcesContent":["import * as React from 'react';\nimport convert from 'react-from-dom';\n\nimport CacheStore from './cache';\nimport { STATUS } from './config';\nimport { canUseDOM, isSupportedEnvironment, omit, randomString, request } from './helpers';\nimport { FetchError, Props, State, Status } from './types';\n\n// eslint-disable-next-line import/no-mutable-exports\nexport let cacheStore: CacheStore;\n\nclass ReactInlineSVG extends React.PureComponent<Props, State> {\n private readonly hash: string;\n private isActive = false;\n private isInitialized = false;\n\n public static defaultProps = {\n cacheRequests: true,\n uniquifyIDs: false,\n };\n\n constructor(props: Props) {\n super(props);\n\n this.state = {\n content: '',\n element: null,\n isCached: !!props.cacheRequests && cacheStore.isCached(props.src),\n status: STATUS.IDLE,\n };\n\n this.hash = props.uniqueHash || randomString(8);\n }\n\n public componentDidMount(): void {\n this.isActive = true;\n\n if (!canUseDOM() || this.isInitialized) {\n return;\n }\n\n const { status } = this.state;\n const { src } = this.props;\n\n try {\n /* istanbul ignore else */\n if (status === STATUS.IDLE) {\n /* istanbul ignore else */\n if (!isSupportedEnvironment()) {\n throw new Error('Browser does not support SVG');\n }\n\n /* istanbul ignore else */\n if (!src) {\n throw new Error('Missing src');\n }\n\n this.load();\n }\n } catch (error: any) {\n this.handleError(error);\n }\n\n this.isInitialized = true;\n }\n\n public componentDidUpdate(previousProps: Props, previousState: State): void {\n if (!canUseDOM()) {\n return;\n }\n\n const { isCached, status } = this.state;\n const { onLoad, src } = this.props;\n\n if (previousState.status !== STATUS.READY && status === STATUS.READY) {\n /* istanbul ignore else */\n if (onLoad) {\n onLoad(src, isCached);\n }\n }\n\n if (previousProps.src !== src) {\n if (!src) {\n this.handleError(new Error('Missing src'));\n\n return;\n }\n\n this.load();\n }\n }\n\n public componentWillUnmount(): void {\n this.isActive = false;\n }\n\n private fetchContent = async () => {\n const { fetchOptions, src } = this.props;\n\n const content: string = await request(src, fetchOptions);\n\n this.handleLoad(content);\n };\n\n private getElement() {\n try {\n const node = this.getNode() as Node;\n const element = convert(node);\n\n if (!element || !React.isValidElement(element)) {\n throw new Error('Could not convert the src to a React element');\n }\n\n this.setState({\n element,\n status: STATUS.READY,\n });\n } catch (error: any) {\n this.handleError(new Error(error.message));\n }\n }\n\n private getNode() {\n const { description, title } = this.props;\n\n try {\n const svgText = this.processSVG();\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 = this.updateSVGAttributes(node);\n\n if (description) {\n const originalDesc = svg.querySelector('desc');\n\n if (originalDesc && 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 && 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 this.handleError(error);\n }\n }\n\n private handleError = (error: Error | FetchError) => {\n const { onError } = this.props;\n const status =\n error.message === 'Browser does not support SVG' ? STATUS.UNSUPPORTED : STATUS.FAILED;\n\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState({ status }, () => {\n /* istanbul ignore else */\n if (typeof onError === 'function') {\n onError(error);\n }\n });\n }\n };\n\n private handleLoad = (content: string, hasCache = false) => {\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState(\n {\n content,\n isCached: hasCache,\n status: STATUS.LOADED,\n },\n this.getElement,\n );\n }\n };\n\n private load() {\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState(\n {\n content: '',\n element: null,\n isCached: false,\n status: STATUS.LOADING,\n },\n async () => {\n const { cacheRequests, fetchOptions, src } = this.props;\n\n const dataURI = src.match(/^data:image\\/svg[^,]*?(;base64)?,(.*)/u);\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 this.handleLoad(inlineSrc);\n\n return;\n }\n\n try {\n if (cacheRequests) {\n const content = await cacheStore.get(src, fetchOptions);\n\n this.handleLoad(content, true);\n } else {\n await this.fetchContent();\n }\n } catch (error: any) {\n this.handleError(error);\n }\n },\n );\n }\n }\n\n private processSVG() {\n const { content } = this.state;\n const { preProcessor } = this.props;\n\n if (preProcessor) {\n return preProcessor(content);\n }\n\n return content;\n }\n\n private updateSVGAttributes(node: SVGSVGElement): SVGSVGElement {\n const { baseURL = '', uniquifyIDs } = this.props;\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 && d.attributes.length) {\n const attributes = Object.values(d.attributes).map(a => {\n const attribute = a;\n const match = a.value.match(/url\\((.*?)\\)/);\n\n if (match && match[1]) {\n attribute.value = a.value.replace(match[0], `url(${baseURL}${match[1]}__${this.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}__${this.hash}`;\n }\n });\n }\n\n if (d.children.length) {\n return this.updateSVGAttributes(d as SVGSVGElement);\n }\n\n return d;\n });\n\n return node;\n }\n\n public render(): React.ReactNode {\n const { element, status } = this.state;\n const { children = null, innerRef, loader = null } = this.props;\n const elementProps = omit(\n this.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 React.cloneElement(element as React.ReactElement, { ref: innerRef, ...elementProps });\n }\n\n if (([STATUS.UNSUPPORTED, STATUS.FAILED] as Status[]).includes(status)) {\n return children;\n }\n\n return loader;\n }\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] = React.useState(cacheStore.isReady);\n\n React.useEffect(() => {\n if (!isReady) {\n cacheStore.onReady(() => {\n setReady(true);\n });\n }\n }, [isReady]);\n\n if (!isReady) {\n return loader ?? null;\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 { canUseDOM as canUseDOMFlag } from 'exenv';\n\nimport type { PlainObject } from './types';\n\nexport function canUseDOM(): boolean {\n return canUseDOMFlag;\n}\n\nexport function isSupportedEnvironment(): boolean {\n return supportsInlineSVG() && typeof window !== 'undefined' && window !== null;\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 /* istanbul ignore next */\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\nfunction randomCharacter(character: string) {\n return character[Math.floor(Math.random() * character.length)];\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\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 /* istanbul ignore else */\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","import { CACHE_MAX_RETRIES, CACHE_NAME, STATUS } from './config';\nimport { request, sleep } from './helpers';\nimport { StorageItem } from './types';\n\nexport default class CacheStore {\n private cacheApi: Cache | undefined;\n private readonly cacheStore: Map<string, StorageItem>;\n private readonly subscribers: Array<() => void> = [];\n private readonly usePersistentCache: boolean;\n public isReady = false;\n\n constructor() {\n this.cacheStore = new Map<string, StorageItem>();\n\n this.usePersistentCache =\n 'REACT_INLINESVG_PERSISTENT_CACHE' in window && !!window.REACT_INLINESVG_PERSISTENT_CACHE;\n\n if (this.usePersistentCache) {\n caches.open(CACHE_NAME).then(cache => {\n this.cacheApi = cache;\n this.isReady = true;\n\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.usePersistentCache\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 // eslint-disable-next-line no-await-in-loop\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"],"mappings":";;;;;;;;AAAA,YAAY,WAAW;AACvB,OAAO,aAAa;;;ACDb,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;;;ACVA,SAAS,aAAa,qBAAqB;AAIpC,SAAS,YAAqB;AACnC,SAAO;AACT;AAEO,SAAS,yBAAkC;AAChD,SAAO,kBAAkB,KAAK,OAAO,WAAW,eAAe,WAAW;AAC5E;AAEA,eAAsB,QAAQ,KAAa,SAAuB;AAChE,QAAM,WAAW,MAAM,MAAM,KAAK,OAAO;AACzC,QAAM,cAAc,SAAS,QAAQ,IAAI,cAAc;AACvD,QAAM,CAAC,QAAQ,KAAK,eAAe,IAAI,MAAM,OAAO;AAEpD,MAAI,SAAS,SAAS,KAAK;AACzB,UAAM,IAAI,MAAM,WAAW;AAAA,EAC7B;AAEA,MAAI,CAAC,CAAC,iBAAiB,YAAY,EAAE,KAAK,OAAK,SAAS,SAAS,CAAC,CAAC,GAAG;AACpE,UAAM,IAAI,MAAM,6BAA6B,QAAQ,EAAE;AAAA,EACzD;AAEA,SAAO,SAAS,KAAK;AACvB;AAEO,SAAS,MAAM,UAAU,GAAG;AACjC,SAAO,IAAI,QAAQ,aAAW;AAC5B,eAAW,SAAS,UAAU,GAAI;AAAA,EACpC,CAAC;AACH;AAEO,SAAS,oBAA6B;AAE3C,MAAI,CAAC,UAAU;AACb,WAAO;AAAA,EACT;AAEA,QAAM,MAAM,SAAS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,aAAO;AAAA,IACT;AAEA,QAAI,SAAS;AACX,aAAa,mBAAa,SAA+B,EAAE,KAAK,UAAU,GAAG,aAAa,CAAC;AAAA,IAC7F;AAEA,QAAK,CAAC,OAAO,aAAa,OAAO,MAAM,EAAe,SAAS,MAAM,GAAG;AACtE,aAAO;AAAA,IACT;AAEA,WAAO;AAAA,EACT;AACF;AA5TE,cALI,gBAKU,gBAAe;AAAA,EAC3B,eAAe;AAAA,EACf,aAAa;AACf;AA2Ta,SAAR,UAA2B,OAAc;AAC9C,MAAI,CAAC,YAAY;AACf,iBAAa,IAAI,WAAW;AAAA,EAC9B;AAEA,QAAM,EAAE,OAAO,IAAI;AACnB,QAAM,CAAC,SAAS,QAAQ,IAAU,eAAS,WAAW,OAAO;AAE7D,EAAM,gBAAU,MAAM;AACpB,QAAI,CAAC,SAAS;AACZ,iBAAW,QAAQ,MAAM;AACvB,iBAAS,IAAI;AAAA,MACf,CAAC;AAAA,IACH;AAAA,EACF,GAAG,CAAC,OAAO,CAAC;AAEZ,MAAI,CAAC,SAAS;AACZ,WAAO,UAAU;AAAA,EACnB;AAEA,SAAO,oBAAC,kBAAgB,GAAG,OAAO;AACpC;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.tsx","../src/config.ts","../src/helpers.ts","../src/cache.ts"],"sourcesContent":["import * as React from 'react';\nimport convert from 'react-from-dom';\n\nimport CacheStore from './cache';\nimport { STATUS } from './config';\nimport { canUseDOM, isSupportedEnvironment, omit, randomString, request } from './helpers';\nimport { FetchError, Props, State, Status } from './types';\n\n// eslint-disable-next-line import/no-mutable-exports\nexport let cacheStore: CacheStore;\n\nclass ReactInlineSVG extends React.PureComponent<Props, State> {\n private readonly hash: string;\n private isActive = false;\n private isInitialized = false;\n\n public static defaultProps = {\n cacheRequests: true,\n uniquifyIDs: false,\n };\n\n constructor(props: Props) {\n super(props);\n\n this.state = {\n content: '',\n element: null,\n isCached: !!props.cacheRequests && cacheStore.isCached(props.src),\n status: STATUS.IDLE,\n };\n\n this.hash = props.uniqueHash || randomString(8);\n }\n\n public componentDidMount(): void {\n this.isActive = true;\n\n if (!canUseDOM() || this.isInitialized) {\n return;\n }\n\n const { status } = this.state;\n const { src } = this.props;\n\n try {\n /* istanbul ignore else */\n if (status === STATUS.IDLE) {\n /* istanbul ignore else */\n if (!isSupportedEnvironment()) {\n throw new Error('Browser does not support SVG');\n }\n\n /* istanbul ignore else */\n if (!src) {\n throw new Error('Missing src');\n }\n\n this.load();\n }\n } catch (error: any) {\n this.handleError(error);\n }\n\n this.isInitialized = true;\n }\n\n public componentDidUpdate(previousProps: Props, previousState: State): void {\n if (!canUseDOM()) {\n return;\n }\n\n const { isCached, status } = this.state;\n const { onLoad, src } = this.props;\n\n if (previousState.status !== STATUS.READY && status === STATUS.READY) {\n /* istanbul ignore else */\n if (onLoad) {\n onLoad(src, isCached);\n }\n }\n\n if (previousProps.src !== src) {\n if (!src) {\n this.handleError(new Error('Missing src'));\n\n return;\n }\n\n this.load();\n }\n }\n\n public componentWillUnmount(): void {\n this.isActive = false;\n }\n\n private fetchContent = async () => {\n const { fetchOptions, src } = this.props;\n\n const content: string = await request(src, fetchOptions);\n\n this.handleLoad(content);\n };\n\n private getElement() {\n try {\n const node = this.getNode() as Node;\n const element = convert(node);\n\n if (!element || !React.isValidElement(element)) {\n throw new Error('Could not convert the src to a React element');\n }\n\n this.setState({\n element,\n status: STATUS.READY,\n });\n } catch (error: any) {\n this.handleError(new Error(error.message));\n }\n }\n\n private getNode() {\n const { description, title } = this.props;\n\n try {\n const svgText = this.processSVG();\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 = this.updateSVGAttributes(node);\n\n if (description) {\n const originalDesc = svg.querySelector('desc');\n\n if (originalDesc && 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 && 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 this.handleError(error);\n }\n }\n\n private handleError = (error: Error | FetchError) => {\n const { onError } = this.props;\n const status =\n error.message === 'Browser does not support SVG' ? STATUS.UNSUPPORTED : STATUS.FAILED;\n\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState({ status }, () => {\n /* istanbul ignore else */\n if (typeof onError === 'function') {\n onError(error);\n }\n });\n }\n };\n\n private handleLoad = (content: string, hasCache = false) => {\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState(\n {\n content,\n isCached: hasCache,\n status: STATUS.LOADED,\n },\n this.getElement,\n );\n }\n };\n\n private load() {\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState(\n {\n content: '',\n element: null,\n isCached: false,\n status: STATUS.LOADING,\n },\n async () => {\n const { cacheRequests, fetchOptions, src } = this.props;\n\n const dataURI = src.match(/^data:image\\/svg[^,]*?(;base64)?,(.*)/u);\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 this.handleLoad(inlineSrc);\n\n return;\n }\n\n try {\n if (cacheRequests) {\n const content = await cacheStore.get(src, fetchOptions);\n\n this.handleLoad(content, true);\n } else {\n await this.fetchContent();\n }\n } catch (error: any) {\n this.handleError(error);\n }\n },\n );\n }\n }\n\n private processSVG() {\n const { content } = this.state;\n const { preProcessor } = this.props;\n\n if (preProcessor) {\n return preProcessor(content);\n }\n\n return content;\n }\n\n private updateSVGAttributes(node: SVGSVGElement): SVGSVGElement {\n const { baseURL = '', uniquifyIDs } = this.props;\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 && d.attributes.length) {\n const attributes = Object.values(d.attributes).map(a => {\n const attribute = a;\n const match = a.value.match(/url\\((.*?)\\)/);\n\n if (match && match[1]) {\n attribute.value = a.value.replace(match[0], `url(${baseURL}${match[1]}__${this.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}__${this.hash}`;\n }\n });\n }\n\n if (d.children.length) {\n return this.updateSVGAttributes(d as SVGSVGElement);\n }\n\n return d;\n });\n\n return node;\n }\n\n public render(): React.ReactNode {\n const { element, status } = this.state;\n const { children = null, innerRef, loader = null } = this.props;\n const elementProps = omit(\n this.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 React.cloneElement(element as React.ReactElement, { ref: innerRef, ...elementProps });\n }\n\n if (([STATUS.UNSUPPORTED, STATUS.FAILED] as Status[]).includes(status)) {\n return children;\n }\n\n return loader;\n }\n}\n\nexport default function InlineSVG(props: Props) {\n if (!cacheStore) {\n cacheStore = new CacheStore();\n }\n\n const { loader } = props;\n const hasCallback = React.useRef(false);\n const [isReady, setReady] = React.useState(cacheStore.isReady);\n\n React.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 { canUseDOM as canUseDOMFlag } from 'exenv';\n\nimport type { PlainObject } from './types';\n\nexport function canUseDOM(): boolean {\n return canUseDOMFlag;\n}\n\nexport function isSupportedEnvironment(): boolean {\n return supportsInlineSVG() && typeof window !== 'undefined' && window !== null;\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 /* istanbul ignore next */\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\nfunction randomCharacter(character: string) {\n return character[Math.floor(Math.random() * character.length)];\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\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 /* istanbul ignore else */\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","import { CACHE_MAX_RETRIES, CACHE_NAME, STATUS } from './config';\nimport { request, sleep } from './helpers';\nimport { StorageItem } from './types';\n\nexport default class CacheStore {\n private cacheApi: Cache | undefined;\n private readonly cacheStore: Map<string, StorageItem>;\n private readonly subscribers: Array<() => void> = [];\n private readonly usePersistentCache: boolean;\n public isReady = false;\n\n constructor() {\n this.cacheStore = new Map<string, StorageItem>();\n\n this.usePersistentCache =\n 'REACT_INLINESVG_PERSISTENT_CACHE' in window && !!window.REACT_INLINESVG_PERSISTENT_CACHE;\n\n if (this.usePersistentCache) {\n caches.open(CACHE_NAME).then(cache => {\n this.cacheApi = cache;\n this.isReady = true;\n\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.usePersistentCache\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 // eslint-disable-next-line no-await-in-loop\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"],"mappings":";;;;;;;;AAAA,YAAY,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B,eAAe;AAAA,EACf,aAAa;AACf;AA2Ta,SAAR,UAA2B,OAAc;AAC9C,MAAI,CAAC,YAAY;AACf,iBAAa,IAAI,WAAW;AAAA,EAC9B;AAEA,QAAM,EAAE,OAAO,IAAI;AACnB,QAAM,cAAoB,aAAO,KAAK;AACtC,QAAM,CAAC,SAAS,QAAQ,IAAU,eAAS,WAAW,OAAO;AAE7D,EAAM,gBAAU,MAAM;AACpB,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,oBAAC,kBAAgB,GAAG,OAAO;AACpC;","names":[]}
|
package/dist/provider.js
CHANGED
|
@@ -56,6 +56,7 @@ module.exports = __toCommonJS(provider_exports);
|
|
|
56
56
|
var import_react = require("react");
|
|
57
57
|
function CacheProvider(param) {
|
|
58
58
|
var children = param.children;
|
|
59
|
+
window.REACT_INLINESVG_PERSISTENT_CACHE = true;
|
|
59
60
|
(0, import_react.useEffect)(function() {
|
|
60
61
|
window.REACT_INLINESVG_PERSISTENT_CACHE = true;
|
|
61
62
|
return function() {
|
package/dist/provider.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/provider.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAqC;AAYtB,SAAR,cAA+B,EAAE,SAAS,GAAU;AACzD,8BAAU,MAAM;AACd,WAAO,mCAAmC;AAE1C,WAAO,MAAM;AACX,aAAO,OAAO;AAAA,IAChB;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,SAAO;AACT","sourcesContent":["import { ReactNode, useEffect } from 'react';\n\ninterface Props {\n children: ReactNode;\n}\n\ndeclare global {\n interface Window {\n REACT_INLINESVG_PERSISTENT_CACHE?: boolean;\n }\n}\n\nexport default function CacheProvider({ children }: Props) {\n useEffect(() => {\n window.REACT_INLINESVG_PERSISTENT_CACHE = true;\n\n return () => {\n delete window.REACT_INLINESVG_PERSISTENT_CACHE;\n };\n }, []);\n\n return children;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/provider.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAqC;AAYtB,SAAR,cAA+B,EAAE,SAAS,GAAU;AACzD,SAAO,mCAAmC;AAE1C,8BAAU,MAAM;AACd,WAAO,mCAAmC;AAE1C,WAAO,MAAM;AACX,aAAO,OAAO;AAAA,IAChB;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,SAAO;AACT","sourcesContent":["import { ReactNode, useEffect } from 'react';\n\ninterface Props {\n children: ReactNode;\n}\n\ndeclare global {\n interface Window {\n REACT_INLINESVG_PERSISTENT_CACHE?: boolean;\n }\n}\n\nexport default function CacheProvider({ children }: Props) {\n window.REACT_INLINESVG_PERSISTENT_CACHE = true;\n\n useEffect(() => {\n window.REACT_INLINESVG_PERSISTENT_CACHE = true;\n\n return () => {\n delete window.REACT_INLINESVG_PERSISTENT_CACHE;\n };\n }, []);\n\n return children;\n}\n"]}
|
package/dist/provider.mjs
CHANGED
package/dist/provider.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/provider.tsx"],"sourcesContent":["import { ReactNode, useEffect } from 'react';\n\ninterface Props {\n children: ReactNode;\n}\n\ndeclare global {\n interface Window {\n REACT_INLINESVG_PERSISTENT_CACHE?: boolean;\n }\n}\n\nexport default function CacheProvider({ children }: Props) {\n useEffect(() => {\n window.REACT_INLINESVG_PERSISTENT_CACHE = true;\n\n return () => {\n delete window.REACT_INLINESVG_PERSISTENT_CACHE;\n };\n }, []);\n\n return children;\n}\n"],"mappings":";AAAA,SAAoB,iBAAiB;AAYtB,SAAR,cAA+B,EAAE,SAAS,GAAU;AACzD,YAAU,MAAM;AACd,WAAO,mCAAmC;AAE1C,WAAO,MAAM;AACX,aAAO,OAAO;AAAA,IAChB;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,SAAO;AACT;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/provider.tsx"],"sourcesContent":["import { ReactNode, useEffect } from 'react';\n\ninterface Props {\n children: ReactNode;\n}\n\ndeclare global {\n interface Window {\n REACT_INLINESVG_PERSISTENT_CACHE?: boolean;\n }\n}\n\nexport default function CacheProvider({ children }: Props) {\n window.REACT_INLINESVG_PERSISTENT_CACHE = true;\n\n useEffect(() => {\n window.REACT_INLINESVG_PERSISTENT_CACHE = true;\n\n return () => {\n delete window.REACT_INLINESVG_PERSISTENT_CACHE;\n };\n }, []);\n\n return children;\n}\n"],"mappings":";AAAA,SAAoB,iBAAiB;AAYtB,SAAR,cAA+B,EAAE,SAAS,GAAU;AACzD,SAAO,mCAAmC;AAE1C,YAAU,MAAM;AACd,WAAO,mCAAmC;AAE1C,WAAO,MAAM;AACX,aAAO,OAAO;AAAA,IAChB;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,SAAO;AACT;","names":[]}
|
package/package.json
CHANGED
package/src/index.tsx
CHANGED
|
@@ -338,18 +338,21 @@ export default function InlineSVG(props: Props) {
|
|
|
338
338
|
}
|
|
339
339
|
|
|
340
340
|
const { loader } = props;
|
|
341
|
+
const hasCallback = React.useRef(false);
|
|
341
342
|
const [isReady, setReady] = React.useState(cacheStore.isReady);
|
|
342
343
|
|
|
343
344
|
React.useEffect(() => {
|
|
344
|
-
if (!
|
|
345
|
+
if (!hasCallback.current) {
|
|
345
346
|
cacheStore.onReady(() => {
|
|
346
347
|
setReady(true);
|
|
347
348
|
});
|
|
349
|
+
|
|
350
|
+
hasCallback.current = true;
|
|
348
351
|
}
|
|
349
|
-
}, [
|
|
352
|
+
}, []);
|
|
350
353
|
|
|
351
354
|
if (!isReady) {
|
|
352
|
-
return loader
|
|
355
|
+
return loader;
|
|
353
356
|
}
|
|
354
357
|
|
|
355
358
|
return <ReactInlineSVG {...props} />;
|