Friday, November 4, 2011

Hack to prevent Caching of Dynamic Images

Web caches are a necessary component of the web. So, when developing dynamic web contents it is important to be sure if an object is cached or not, otherwise the user may end up seeing some old cached content. The objective here is to block caching of images produced by a service which generates a new image in each call, so the image must be transmitted every time.

The HTML meta tags do not apply to the case of images (and their effectivity is even questionable for HTML pages). The HTTP cache directives are better suited for cache control since most of the web caches honor them. But they entail some customization of the web server in order to send the correct header for each file.

The simplest way to prevent caching of a dynamic image, is to change its url at each run. For that, one possibility is to completely change the filename. But a trick allows to keep the original filenames and still prevent caching, it consists in appending a random querysting to the filename like this:

<img src="output.png?r=1234">

the querystring is discarded by the webserver where output.png is. Still it is seen as a different object by the cache, causing the image to be reloaded every time the page is loaded. 


Source: http://www.itsjustpoison.com/blog/2008/05/12/trick-prevent-image-caching-of-dynamic-images
Source: http://www.i18nguy.com/markup/metatags.html
Reading: http://www.mnot.net/cache_docs/

No comments:

Post a Comment