Disable image cache in AngularJS

All we need is an easy explanation of the problem, so here it is.

I’ve written a directive that allows the user to upload photos then displays them. the user can then click on one of those displayed images and crop it using Jcrop. After the user is done cropping the image, the information is sent to the server, is parsed, then the image is cropped and put back in the db. this new cropped image has the same reference as it did before; the original image is overwritten. This is all done on a spring server. I need to figure out how to get the image to refresh and display the new cropped image. The main issue is that the image and the request (I think) are both cached. I’m fairly new to angular and don’t know a lot about the caching factory process.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You can add a random argument to image URL – this will prevent caching in most browsers. This technique is called cache busting. You will have to change the value of this argument each time the image changed for this to work.

Update

Save timestamp in some variable and change it only when image has changed.

Eg. This will call the getTimeStamp() function which will generate the current timestamp and act as a cache buster.

<img data-ng-src='api/image/{{image.id}}?{{getTimeStamp()}}' data-ng-click='editImage($index);'/> 

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply