Disable cache for some images
I generate some images using a PHP lib. Sometimes the browser does not load the new generated file. How can I disable cache just for images created dynamically by me? Note: I have to use same name for the created images over time.
13 Answers 13
A common and simple solution to this problem that feels like a hack but is fairly portable is to add a randomly generated query string to each request for the dynamic image.
From the point of view of the web-server the same file is accessed, but from the point of view of the browser no caching can be performed.
The random number generation can happen either on the server when serving the page (just make sure the page itself isn’t cached. ), or on the client (using JavaScript).
You will need to verify whether your web-server can cope with this trick.
Instead of random numbers, use the timestamp that the data changed or a version number of the reflected data.
Please note: You do not actually prevent the browser from caching the image, you only prevent looking at the cached image. Applying proper headers to your image is the best way imho (see the solution of lhunath below). Since this way you also fill the cache unnecessarily with images you do not want to cache with the cost of causing less cache space for things you actually do want to be cached.
this does not really works, the image needs to be flushed another way (usually on image cropping, the image remains the same)
Browser caching strategies can be controlled by HTTP headers. Remember that they are just a hint, really. Since browsers are terribly inconsistent in this (and any other) field, you’ll need several headers to get the desired effect on a range of browsers.
header ("Pragma-directive: no-cache"); header ("Cache-directive: no-cache"); header ("Cache-control: no-cache"); header ("Pragma: no-cache"); header ("Expires: 0");
this will applay to the whole page. I can’t disable cache for one image only(a specific image from that page)?
@Thorpe: It applies to HTTP responses. What is contained in the response is irrelevant. Whether it’s image data, HTML data or whatever else. If it didn’t work, you probably didn’t do it right. Check the HTTP headers on your response to see if they have been correctly assigned.
I wish this worked. Chrome doesn’t have any problems, but Firefox 14 and IE 8 refuse to refresh the images even with the above headers being sent. This would have been so much cleaner solution than adding some arbitrary parameters to the query string. sigh
@PawelKrakowiak Note that adding headers won’t work for images that are already cached, since the browser doesn’t even ask the server about them and therefore will never see the headers. They will work for any image requests made after you added them.
This solution is intended for programmers, not web designers. I thought I would point that out because one cannot just open and image and add headers to an image, unless they themselves are generating the image in a programming language and this seems to be confusing commenters.
Solution 1 is not great. It does work, but adding hacky random or timestamped query strings to the end of your image files will make the browser re-download and cache every version of every image, every time a page is loaded, regardless of whether or not the image has changed on the server.
Solution 2 is useless. Adding nocache headers to an image file is not only very difficult to implement, but it’s completely impractical because it requires you to predict when it will be needed in advance, the first time you load any image that you think might change at some point in the future.
Enter Etags.
The absolute best way I’ve found to solve this is to use ETAGS inside a .htaccess file in your images directory. The following tells Apache to send a unique hash to the browser in the image file headers. This hash only ever changes when the image file is modified and this change triggers the browser to reload the image the next time it is requested.
If you need to do it dynamically in the browser using javascript, here is an example.
var d = new Date(); document.getElementById(«graph»).src = «http://www.kitco.com/images/live/gold.gif?ver mt24″>