-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Does <SvgUri /> component support cache? #1328
Comments
It uses fetch provided by react-native, so it respects the http protocol and the http cache related headers as is. If you want something more than that, I'd recommend copying the code from SvgUri and modify it to your needs. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You may also mark this issue as a "discussion" and I will leave this open. |
I also needed caching and solved it like this: // CachedSVGUri.js
import React, {Component} from 'react';
import {SvgFromXml} from 'react-native-svg';
import SVGCacheService from '../services/storage/SVGCacheService';
export async function fetchCached(uri) {
const cached = await SVGCacheService.getSvg(uri);
if (cached) {
return cached;
} else {
const response = await fetch(uri);
const svg = await response.text();
SVGCacheService.setSvg(uri, svg);
return svg;
}
}
export default class CachedSvgUri extends Component {
state = {xml: null};
componentDidMount() {
this.fetch(this.props.uri);
}
componentDidUpdate(prevProps) {
const {uri} = this.props;
if (uri !== prevProps.uri) {
this.fetch(uri);
}
}
async fetch(uri) {
try {
this.setState({xml: uri ? await fetchCached(uri) : null});
} catch (e) {
console.error(e);
}
}
render() {
const {
props,
state: {xml},
} = this;
return <SvgFromXml xml={xml} override={props} />;
}
} // SVGCacheService.js
import AsyncStorage from '@react-native-community/async-storage';
const ASYNC_STORAGE_KEY = 'svg-cache';
let data = null;
const loadData = async () => {
const defaultData = {
svgs: {},
};
const result = await AsyncStorage.getItem(ASYNC_STORAGE_KEY);
data = result ? await JSON.parse(result) : defaultData;
data = {...defaultData, ...data};
};
export default class SVGCacheService {
static async setSvg(uri, svg) {
const oldData = data || {};
const newData = {
...oldData,
svgs: {
...oldData.svgs,
[uri]: svg,
},
};
data = newData;
await AsyncStorage.setItem(ASYNC_STORAGE_KEY, JSON.stringify(newData));
}
static async getSvg(uri) {
if (data === null) await loadData();
return data.svgs[uri];
}
} |
@creat-or nice solution |
@danyasnov hey there! |
Since the solution provided by @27leaves seems like a good option, I believe we can close this issue. Feel free to comment here if something is wrong and we can always reopen it then. |
Same problem here, maybe @27leaves's solution works, but as @danyasnov said, it is not an optimal solution. @WoLewicki as you closed the issue, if for you the @27leaves's solution is fine, maybe you can implement him code in the library? Thanks After some hours, I found this package which works very well: https://docs.expo.dev/versions/latest/sdk/image By default, it includes a cache and store it in the "disk", you can change if you want with: https://docs.expo.dev/versions/latest/sdk/image/#cachepolicy A simple change will optimize your code and application, like: <SvgUri width="50" height="50" uri="url here" /> to <Image
style={{
width: 50,
height: 50,
}}
source="url here"
cachePolicy="memory-disk"
/> And off course npm remove react-native-svg |
|
Does
<SvgUri />
component support cache like react native's<Image />
component?For example I'm using cache for my remote png files like
The text was updated successfully, but these errors were encountered: