Skip to content

Commit 23ca4ee

Browse files
committed
added parameters for cache key generation
1 parent be6245c commit 23ca4ee

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import CacheableImage from 'react-native-cacheable-image'
3333

3434
* `activityIndicatorProps` - pass this property to alter the ActivityIndicator
3535
* `defaultSource` - pass this property to provide a default source to fallback on (the defaultSource is attached to another CacheableImage component)
36-
36+
* `useQueryParamsInCacheKey` - Defaults to false for backwards compatibility. Set to true to include query parameters in cache key generation. Set to an array of parameters to only include specific parameters in cache key generation.
3737

3838
## Example
3939

image.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,21 @@ class CacheableImage extends React.Component {
128128
&& source.hasOwnProperty('uri'))
129129
{ // remote
130130
const url = new URL(source.uri, null, true);
131+
132+
// handle query params for cache key
131133
let cacheable = url.pathname;
132-
if (this.props.useQueryParamsInCacheKey) {
134+
if (Array.isArray(this.props.useQueryParamsInCacheKey)) {
135+
this.props.useQueryParamsInCacheKey.forEach(function(k) {
136+
if (url.query.hasOwnProperty(k)) {
137+
cacheable = cacheable.concat(url.query[k]);
138+
}
139+
});
140+
}
141+
else if (this.props.useQueryParamsInCacheKey) {
133142
cacheable = cacheable.concat(url.query);
134143
}
144+
145+
// ignore extension
135146
// const type = url.pathname.replace(/.*\.(.*)/, '$1');
136147
const cacheKey = SHA1(cacheable); // +'.'+type;
137148

@@ -212,7 +223,10 @@ class CacheableImage extends React.Component {
212223
CacheableImage.propTypes = {
213224
activityIndicatorProps: React.PropTypes.object,
214225
defaultSource: React.PropTypes.object,
215-
useQueryParamsInCacheKey: React.PropTypes.bool
226+
useQueryParamsInCacheKey: React.PropTypes.oneOfType([
227+
React.PropTypes.bool,
228+
React.PropTypes.array
229+
])
216230
};
217231

218232

0 commit comments

Comments
 (0)