Need Browser to Not Cache Ajax Image

I am banging my head against the wall here. I have a gd-generated image that keeps being changed via ajax on my page. Since the image is generated based on querystring parameters (which don’t change) but is based on a model (that is constantly changing), the browser caches the image and you have to do a page refresh to update the image. I have hacked around it by appending a random number to the querystring, but that should make every image be cached to the browser, which I don’t want to do to my clients if I don’t have to (there will be LOTS of images). What I have found online is that I need to change the headers that I am sending out. Here’s what I have and it doesn’t work. I don’t know if the headers aren’t being sent or if I am sending the wrong headers or a sickly combination of the two…

headers[‘Cache-Control’] = ‘no-cache, must-revalidate, post-check=0, pre-check=0’ headers[‘Pragma’] = ‘public’ headers[‘Expires’] = “0” send_data image.jpeg, :type => “image/jpeg”, :disposition => “inline” #this line works

Any help would be greatly appreciated.

Maybe someone might come up with a header-based solution, but you could also add an extra parameter with a timestamp to the url (don’t know if the :with parameter of the Rails Javascript helper will do here, I tend to write up my own Javascript instead of relying on the Rails helpers). Rails itself uses a similar solution for the css and javascript files that are loaded (it adds a timestamp). This would mean your browser would send out requests like:

Best regards

Peter De Berdt


That’s almost exactly what I am doing right now, except I use rand( rather than a timestamp (in the off chance of more than 1 req/sec). I just don’t like the idea of spamming my customers’ cache with images if I don’t have to.

Thank you for your help.