react-native icon indicating copy to clipboard operation
react-native copied to clipboard

Image never reloads with same uri

Open RyanMitchellWilson opened this issue 8 years ago • 86 comments

I am using an Image component to display a profile picture but no matter what I do to the image component it will not reload the image. I have to use the same url for the profile pictures so I tried to give the Image a unique key prop to try and get it to reload the image but it still just uses the image from cache. I have changed the image on the server countless times but the Image component only shows the original image it got for the url.

It would be great if there was a prop to pass into Image that forces it to refresh the image every time it is rendered.

In a perfect world it would display the image from cache while fetching the new image, compare the two then decide if the image has changed.

  • React Native version: 0.41.0
  • Platform: Both

RyanMitchellWilson avatar Feb 27 '17 20:02 RyanMitchellWilson

@RyanMitchellWilson What I do as a workaround is add a meaningless query parameter to the url. Now whenever I want the image to reload, I just change that param and that invalidates the cache.

ramilushev avatar Feb 28 '17 08:02 ramilushev

So it turns out I was actually hitting the wrong dev environment for the picture so it just looked as though it wasn't changing when it actually was.

RyanMitchellWilson avatar Feb 28 '17 17:02 RyanMitchellWilson

Well it seems I was actually right the first time so I'm going to reopen this issue. I even gave the image a key of Math.random() and it still keeps loading from cache when the image on the server has changed multiple times

RyanMitchellWilson avatar Mar 07 '17 22:03 RyanMitchellWilson

Using @ramilushev suggestion worked but it feels hacky and probably shouldn't be needed.

RyanMitchellWilson avatar Mar 07 '17 22:03 RyanMitchellWilson

@RyanMitchellWilson have you found a better solution to this ever since then? I do feel that this is an issue that the developers in FB have to look into. its been re-raised and no good solution has been given till now

LEEY19 avatar Apr 13 '17 16:04 LEEY19

Same issue here (only this time with FB account images, LOL). On iOS the image seems to refresh okey-ish after the image view is unmounted, and it also has a cache props for that. On android the image is cached for ever unless do something like @ramilushev suggested, although it is only a workaround and not without side effects (e.g. no cache to use after path change, producing too many junk copies in the cache for the same image). The issue probably boils down to the Fresco library (the Image component uses internally), which currently doesn't seem to have a good knowledge about when to invalidate its cache, and there is no current way of forcing it to invalidate either (or if there is one please let us know).

huangmr avatar Jul 18 '17 15:07 huangmr

ios I can set <Image key ={this.count++} style={{width:SCALE(200),height:SCALE(70)}} source={{uri:ver,cache: 'reload'}}/> and is ok. but android stil bad. please let me know if it be solve.

wuyunqiang avatar Aug 22 '17 09:08 wuyunqiang

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions.

stale[bot] avatar Oct 21 '17 09:10 stale[bot]

This is still a problem

RyanMitchellWilson avatar Oct 23 '17 15:10 RyanMitchellWilson

@RyanMitchellWilson
Did you solve the problem?

dbyilmaz avatar Oct 30 '17 06:10 dbyilmaz

@dbyilmaz Not exactly I'm still using the work around stated above adding a Math.random() to the query string in a way that doesn't affect the url. This causes the url to be different every time so it will actually fetch the image instead of just grabbing it from the cache.

RyanMitchellWilson avatar Oct 30 '17 15:10 RyanMitchellWilson

@RyanMitchellWilson I am using your workaround. I think it is not good. I guess facebook don't think to solve this problem.

dbyilmaz avatar Dec 01 '17 13:12 dbyilmaz

@RyanMitchellWilson @dbyilmaz you can fix this problem by adding new Date() value in key. like

<Image
  key={new Date()}
  style={styles.drawerAccountImg}
  source={uri: this.state.photoURL}
/>

However, I've not tested in android yet.

hyochan avatar Jan 10 '18 10:01 hyochan

Hi

This worked for me on Android emulator:

<Image key={(new Date()).getTime()} source={{ uri: global.SERVER_ROOT + item['LOGO_IMG']+'?time'+(new Date()).getTime(), headers: {Pragma: 'no-cache' } }} style={{ width: width / 5, height: 100 }} />

Hope it hepls Malina

malinamircu avatar Feb 03 '18 11:02 malinamircu

add a query string after url, works for both platform <Image style={styles.drawerAccountImg} source={uri: this.state.photoURL + '?' + new Date()} />

crazyfree avatar Feb 06 '18 11:02 crazyfree

Thankz bro...

aswin2772 avatar Feb 15 '18 12:02 aswin2772

Adding a query param to the source works. But it's all too hacky.

dbarros avatar Mar 26 '18 08:03 dbarros

i'm still cant believe its really works

Padillahirpan avatar Apr 03 '18 07:04 Padillahirpan

add a 'time=' param along with image URL, works as well.

<Image source={ { uri : this.props.userData.profileImgURL + '?time=' new Date() }} />

indrajeet1629 avatar Apr 13 '18 08:04 indrajeet1629

Still an issue - cache: 'reload' doesn't work on Android. Only hacky solutions work but they put a lot of junk images in the cache which is bad...

Dror-Bar avatar May 30 '18 09:05 Dror-Bar

Still an issue, wish to resolve : )

tokinonagare avatar Jul 25 '18 07:07 tokinonagare

+1

Faolain avatar Sep 16 '18 03:09 Faolain

<Image source={{uri: <your_image_uri>, CACHE: 'reload'}} />

CACHE prop worked for me.

I-O-O-I avatar Sep 20 '18 12:09 I-O-O-I

@I-O-O-I As mentioned in the thread, that is for IOS only, it does not work for Android.

Dror-Bar avatar Sep 20 '18 12:09 Dror-Bar

+1

ahpollen avatar Sep 20 '18 18:09 ahpollen

<Image style={{flex:3,width:'100%'}} source={{uri: image.thumbnailUrl+'?'+new Date().getDate()}} key={image${i}}/> This worked for me, currently react native does not have support for android, so we need some kind of hack

singhal-akash67 avatar Dec 28 '18 04:12 singhal-akash67

hi, i am using this property for android:-

<Image
source={uri: this.state.uri + '?' + new Date()}
/>

For iOS:- <Image source={{uri: this.state.uri, CACHE: 'reload'}} />

But any changes doing in that particular screen then image is disappearing like below mentioned gif file :- ezgif com-video-to-gif Please give any suggestion for this issue.

harikanammi avatar Jan 05 '19 13:01 harikanammi

same issue @harikanammi

habibiazmi123 avatar Jan 16 '19 08:01 habibiazmi123

I haven't tried this, but in Vue the same happened to me. It didn't work with Date() but it worked with Math.random(). It would be something like this:

<Image source= { uri: this.state.uri + '?rnd=' + Math.random() } />

zoix avatar Jan 16 '19 18:01 zoix

hi @agamadev, thank you for your reply. <Image source= { uri: this.state.uri + '?rnd=' + Math.random() } /> this is not working for me. Image disappearing issue is not fixed.

harikanammi avatar Mar 18 '19 11:03 harikanammi