# Need Browser to Not Cache Ajax Image

**URL:** <https://www.ruby-forum.com/t/need-browser-to-not-cache-ajax-image/142673>\
**Category:** Rails\
**Created:** [July 10, 2008, 9:32am UTC](https://www.ruby-forum.com/t/need-browser-to-not-cache-ajax-image/142673 "2008-07-10T09:32:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5e9695/32.png) [@Joe\_K](https://www.ruby-forum.com/u/Joe_K)\
**Post date:** [July 10, 2008, 9:32am UTC](https://www.ruby-forum.com/t/need-browser-to-not-cache-ajax-image/142673/1 "2008-07-10T09:32:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Joe\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5e9695/32.png) [@Joe\_K](https://www.ruby-forum.com/u/Joe_K)\
**Post date:** [July 10, 2008, 2:19pm UTC](https://www.ruby-forum.com/t/need-browser-to-not-cache-ajax-image/142673/2 "2008-07-10T14:19:54Z")

</div>

On 10 Jul 2008, at 09:31, Joe K wrote:

> and it doesn’t work. I don’t know if the headers aren’t being sent  
> 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:

[http://www.mydomain.com/image?time=20080710141812](http://www.mydomain.com/image?time=20080710141812)  
[http://www.mydomain.com/image?time=20080710141825](http://www.mydomain.com/image?time=20080710141825)  
[http://www.mydomain.com/image?time=20080710141845](http://www.mydomain.com/image?time=20080710141845)  
[http://www.mydomain.com/image?time=20080710141910](http://www.mydomain.com/image?time=20080710141910)  
…

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![Joe\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5e9695/32.png) [@Joe\_K](https://www.ruby-forum.com/u/Joe_K)\
**Post date:** [July 10, 2008, 11:30pm UTC](https://www.ruby-forum.com/t/need-browser-to-not-cache-ajax-image/142673/3 "2008-07-10T23:30:56Z")

</div>

Peter,

That’s almost exactly what I am doing right now, except I use  
rand(Time.now.to\_i) 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.

On Thu, Jul 10, 2008 at 8:19 AM, Peter De Berdt  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
