Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Forcing browsers not to use their cache for images

Reply
Thread Tools

Forcing browsers not to use their cache for images

 
 
Edward Diener
Guest
Posts: n/a
 
      01-25-2008
I am working on a web application which programatically changes an image
on the server side ( via ImageMagick ) on the roundtrip between client
and server. When the web page, in which the img tag resides, is
redisplayed, I need Firefox to reread the web page in order to redisplay
the image correctly rather than take the image from its cache.

I have been told that the only reliable way to do this is by using the
content header of the web page to specify certain values which,
according to W3C, browsers are supposed to follow to tell it how to
treat the cache. I have done this, according to the documentation I have
found, and yet the browser does not redisplay the image from the URL but
continues to display it by using its cache. This displays the image
incorrectly since it has changed on the server in the roundtrip between
client and server and back again.

The problem occurs in Firefox ( 2.0.0.11 ) and with IE6 and IE7 also. I
have not tried to test it with any other browsers since the
specification for the web application is that it is only guaranteed to
work with Firefox and IE. The fact that the problem exists with IE as
well as Firefox suggests that some combination in the content header is
not correct to force browsers to not use the cache but instead to reread
the data from the img URL.

Here is the content header, without the cookies, from such a page, as
extracted live:

--------------------------------------------------------

Content-type: text/plain
HTTP/1.1 200 OK
Server: ""
Date: Thu, 24 Jan 2008 16:01:45 GMT
Content-type: text/html
Pragma: no-cache
Cache-control: no-cache,no-store,must-revalidate

--------------------------------------------------------

If anybody can see what is incorrect in the content header and tell me
what I have to change in order to cause the browser to redisplay the
image from the URL and not from its cache, it would be highly
appreciated by me.

Getting browsers to redisplay the image correctly, from changes made on
the server side, is crucial to this particular application, and this
problem has persisted for much of the life of the application without a
resolution.

Thank you !
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-25-2008
Edward Diener wrote:
> [...]
> The problem occurs in Firefox ( 2.0.0.11 ) and with IE6 and IE7 also. [...]
> Here is the content header,


There is no such thing.

> without the cookies, from such a page, as extracted live:
>
> --------------------------------------------------------
>
> Content-type: text/plain

^^^^^^^^^^^^^^^^^^^^^^^^
> HTTP/1.1 200 OK
> Server: ""

^^
That header is invalid. See RFC 2616, section 14.38:

http://www.rfc-editor.org/rfc/rfc2616.txt

> Date: Thu, 24 Jan 2008 16:01:45 GMT
> Content-type: text/html


Declaring the encoding of the resource with the `charset' parameter is
strongly recommended:

http://www.w3.org/TR/html401/charset.html#h-5.2.2

That aside, have you not already declared the Content-Type? There can only
be one Content-Type header per response message, and the status code has to
come before all headers.

> Pragma: no-cache
> Cache-control: no-cache,no-store,must-revalidate


You have declared the caching policy, but you have not provided any header
that would allow the user agent to recognize a change in the resource as
compared to the cached one. The Date header does not suffice (see RFC 2616,
section 14.1, you have to provide one or more of Content-Length (14.13),
Content-MD5 (14.15), ETag (14.19), Expires (14.21), or Last-Modified (14.29).

See also http://www.mnot.net/cache_docs/


The From header of your NetNews message is invalid too (not to mention
disregarding Netiquette, see RFC 1036, section 2.1.1 (which refers to RFC
2822, section 3.4.1), and possibly the Acceptable Use Policy of your Usenet
access provider. Please fix that.


HTH

PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 
Reply With Quote
 
 
 
 
Edward Diener
Guest
Posts: n/a
 
      01-25-2008
Thomas 'PointedEars' Lahn wrote:
> Edward Diener wrote:
>> [...]
>> The problem occurs in Firefox ( 2.0.0.11 ) and with IE6 and IE7 also. [...]
>> Here is the content header,

>
> There is no such thing.


There is no such thing as an HTML page content header ? Please explain.

>
>> without the cookies, from such a page, as extracted live:
>>
>> --------------------------------------------------------
>>
>> Content-type: text/plain

> ^^^^^^^^^^^^^^^^^^^^^^^^
>> HTTP/1.1 200 OK
>> Server: ""

> ^^
> That header is invalid. See RFC 2616, section 14.38:


That is wonderfully cryptic but I am guessing you are telling me that
the Server must have a value rather than an empty string.

>
> http://www.rfc-editor.org/rfc/rfc2616.txt
>
>> Date: Thu, 24 Jan 2008 16:01:45 GMT
>> Content-type: text/html

>
> Declaring the encoding of the resource with the `charset' parameter is
> strongly recommended:
>
> http://www.w3.org/TR/html401/charset.html#h-5.2.2


OK, I hear you !

>
> That aside, have you not already declared the Content-Type? There can only
> be one Content-Type header per response message, and the status code has to
> come before all headers.


Was there not a "Content-type: text/html" in the above ? Are you saying
that the content type is invalid because the status code comes after it
? I don't generate a status code, I just set the content header fields
in my code.

>
>> Pragma: no-cache
>> Cache-control: no-cache,no-store,must-revalidate

>
> You have declared the caching policy, but you have not provided any header
> that would allow the user agent to recognize a change in the resource as
> compared to the cached one. The Date header does not suffice (see RFC 2616,
> section 14.1, you have to provide one or more of Content-Length (14.13),
> Content-MD5 (14.15), ETag (14.19), Expires (14.21), or Last-Modified (14.29).


If I provide an Expires, what do I set the value to ? Is -1 a valid
value ? Previously I had an Expires: -1, to force expiration, but that
did not cause my problem to go away.

>
> See also http://www.mnot.net/cache_docs/
>
>
> The From header of your NetNews message is invalid too (not to mention
> disregarding Netiquette, see RFC 1036, section 2.1.1 (which refers to RFC
> 2822, section 3.4.1), and possibly the Acceptable Use Policy of your Usenet
> access provider. Please fix that.


Sorry but this is way too cryptic for me. I appreciate your knowledge
but I have no idea what you mean by saying the From header is invalid.
If you are criticizing my Usenet message to this newsgroup you are just
being silly and hypercritical to no good end.

Hopefully restoring the Expires: -1 value and putting in a valid Server
value will fix my problem but if it does not I will post again.

Thanks for your help !
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-25-2008
Edward Diener wrote:
> Thomas 'PointedEars' Lahn wrote:
>> The From header of your NetNews message is invalid too (not to mention
>> disregarding Netiquette, see RFC 1036, section 2.1.1 (which refers to
>> RFC 2822, section 3.4.1), and possibly the Acceptable Use Policy of
>> your Usenet access provider. Please fix that.

>
> Sorry but this is way too cryptic for me.


You could have used your favorite search engine instead of playing stupid.

> I appreciate your knowledge but I have no idea what you mean by saying
> the From header is invalid. If you are criticizing my Usenet message to
> this newsgroup you are just being silly and hypercritical to no good end.
>

,-<http://www.att.net/csbellsouth/s/s.d...tt.htm&leg=aup
|
| Spam/E-mail/Usenet Abuse
|
| Violation of the CAN-SPAM Act of 2003, or any state or federal law
| regulating e-mail services, constitutes an automatic violation of this AUP
| and AT&T reserves the right to seek damages and other available relief
| against Customer, as applicable.
|
| Spam/E-mail/Usenet Abuse is prohibited on AT&T IP Services. Examples of
| Spam/E-mail/Usenet Abuse include but are not limited to the following
| activities:
|
| [...]
| # posting a single message, or messages to online forums or newsgroups,
| that could reasonably be expected to provoke complaints;
| # posting messages to or canceling or superseding messages on an online
| forum or newsgroup in a manner that violates the rules of the forum or
| newsgroup or that contain forged header information.
| [...]

You have been warned.


Score adjusted,

PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
 
Reply With Quote
 
Stevo
Guest
Posts: n/a
 
      01-25-2008
Edward Diener wrote:
> I have been told that the only reliable way to do this is by using the
> content header of the web page to specify certain values which,


I don't think that this is the only way to do it. I think individual
browser settings override any settings your server might return. You may
tell the browser to request the file every time, but if the browser is
set to only check once per session, I believe the browser wins and does
exactly that.

You can reload the image again with a random query string. That's a
pretty standard way of doing things.

A js function like this would reload the image and break the caching:

function reloadImage(imgobj,imgpath)
{
var rand = Math.floor(Math.random()*10000000);
imgpath += "?" + rand;
if(imgobj)
imgobj.src=imgpath;
}

You'd get requests to your server like this:

myimg.gif?123456
myimg.gif?7959831234
etc.
 
Reply With Quote
 
Edward Diener
Guest
Posts: n/a
 
      01-26-2008
Thomas 'PointedEars' Lahn wrote:
> Edward Diener wrote:
>> Thomas 'PointedEars' Lahn wrote:
>>> The From header of your NetNews message is invalid too (not to mention
>>> disregarding Netiquette, see RFC 1036, section 2.1.1 (which refers to
>>> RFC 2822, section 3.4.1), and possibly the Acceptable Use Policy of
>>> your Usenet access provider. Please fix that.

>> Sorry but this is way too cryptic for me.

>
> You could have used your favorite search engine instead of playing stupid.


It is you who are stupid since clear communication seems beyond you, and
you seem to think that cryptic communication is somehow real "cool"
rather than just being annoying.

>
>> I appreciate your knowledge but I have no idea what you mean by saying
>> the From header is invalid. If you are criticizing my Usenet message to
>> this newsgroup you are just being silly and hypercritical to no good end.
>>

> ,-<http://www.att.net/csbellsouth/s/s.d...tt.htm&leg=aup
> |
> | Spam/E-mail/Usenet Abuse
> |
> | Violation of the CAN-SPAM Act of 2003, or any state or federal law
> | regulating e-mail services, constitutes an automatic violation of this AUP
> | and AT&T reserves the right to seek damages and other available relief
> | against Customer, as applicable.
> |
> | Spam/E-mail/Usenet Abuse is prohibited on AT&T IP Services. Examples of
> | Spam/E-mail/Usenet Abuse include but are not limited to the following
> | activities:
> |
> | [...]
> | # posting a single message, or messages to online forums or newsgroups,
> | that could reasonably be expected to provoke complaints;
> | # posting messages to or canceling or superseding messages on an online
> | forum or newsgroup in a manner that violates the rules of the forum or
> | newsgroup or that contain forged header information.
> | [...]
>
> You have been warned.


I am terribly scared that the thought police, led by you, will come and
take me away.

>
>
> Score adjusted,


Whatever that means !!!
 
Reply With Quote
 
Edward Diener
Guest
Posts: n/a
 
      01-26-2008
Stevo wrote:
> Edward Diener wrote:
>> I have been told that the only reliable way to do this is by using the
>> content header of the web page to specify certain values which,

>
> I don't think that this is the only way to do it. I think individual
> browser settings override any settings your server might return. You may
> tell the browser to request the file every time, but if the browser is
> set to only check once per session, I believe the browser wins and does
> exactly that.


So you are saying that there is essentially no programmatic way of
forcing browsers to reload an image from its URL rather than taking the
image from its cache ?

You may be right but that seems utterly illogical from this programmer's
point of view. It essentially says that any web application, which
depends on images being changed on the server side be reflected by the
browser's display of that image, can not possibly work. This makes web
application programming utterly hopeless.

>
> You can reload the image again with a random query string. That's a
> pretty standard way of doing things.
>
> A js function like this would reload the image and break the caching:
>
> function reloadImage(imgobj,imgpath)
> {
> var rand = Math.floor(Math.random()*10000000);
> imgpath += "?" + rand;
> if(imgobj)
> imgobj.src=imgpath;
> }
>
> You'd get requests to your server like this:
>
> myimg.gif?123456
> myimg.gif?7959831234
> etc.


Already tried and does not work. The browser ignores the request string
part of the URL and continues to reload the image from its cache.

I have to believe that there is some simple way of telling the browser,
for a particular web page, to go out and reload the image from its URL
rather than assuming it has the most recent version of that image in its
cache. The fact that it has been impossible to find out from anyone or
anywhere what that way is still amazes me, as if I were asking for some
hidden secret which only initiates are allowed to know <g>.

Thanks for your suggestion, nonetheless. I do appreciate it.
 
Reply With Quote
 
Stevo
Guest
Posts: n/a
 
      01-26-2008
Randy Webb wrote:
> Thomas 'PointedEars' Lahn said the following on 1/25/2008 6:09 PM:
> <snip>
>> You have been warned.

>
> "If you don't do exactly as Thomas thinks you should, he will tell on
> you!". But, he "cares about the Net", he isn't a "control freak".
> What a joke you are sometimes.


It would be incredibly amusing if Thomas's CV/Resume had "Good
communication skills" and "team player" on it. I think his personality
is more suited to be a traffic warden.

It's sad when technical knowledge (which none of us doubt he has plenty
of ... and I expect he has more than me in the web technology field) is
wrapped up in such a package.
 
Reply With Quote
 
Stevo
Guest
Posts: n/a
 
      01-26-2008
Edward Diener wrote:
> Stevo wrote:
>> Edward Diener wrote:

> The browser ignores the request string
> part of the URL and continues to reload the image from its cache.


That doesn't match up with my experience. I've been using that technique
for a long time. The browser isn't intelligent enough to realize that
it's the same URL (which of course it isn't).

> I have to believe that there is some simple way of telling the browser,
> for a particular web page, to go out and reload the image from its URL
> rather than assuming it has the most recent version of that image in its
> cache. The fact that it has been impossible to find out from anyone or
> anywhere what that way is still amazes me, as if I were asking for some
> hidden secret which only initiates are allowed to know <g>.
>
> Thanks for your suggestion, nonetheless. I do appreciate it.


Perhaps that Comet Programming thread is something for you. That implies
the kind of server pushed content updating. That doesn't help you if it
means you have to re-architect a lot of stuff of course.

I notice that Yahoo Finance does regular image updates when you're
looking at a daily stock chart. They could be using some Ajax method, or
they might just overwrite the innerHTML once a minute by using a
random query string on the image.
 
Reply With Quote
 
Edward Diener
Guest
Posts: n/a
 
      01-26-2008
Stevo wrote:
> Edward Diener wrote:
>> Stevo wrote:
>>> Edward Diener wrote:

>> The browser ignores the request string part of the URL and continues
>> to reload the image from its cache.

>
> That doesn't match up with my experience. I've been using that technique
> for a long time. The browser isn't intelligent enough to realize that
> it's the same URL (which of course it isn't).


I hear you but unfortunately when I tried this the browser still
reloaded the image form its cache. The browser could obviously be
intelligent enough to parse the img tag so that anything after the ? is
ignored, although it may not be correct HTML to do that. Does it
actually mean anything to give an image URL some request parameters ?

>
>> I have to believe that there is some simple way of telling the
>> browser, for a particular web page, to go out and reload the image
>> from its URL rather than assuming it has the most recent version of
>> that image in its cache. The fact that it has been impossible to find
>> out from anyone or anywhere what that way is still amazes me, as if I
>> were asking for some hidden secret which only initiates are allowed to
>> know <g>.
>>
>> Thanks for your suggestion, nonetheless. I do appreciate it.

>
> Perhaps that Comet Programming thread is something for you. That implies
> the kind of server pushed content updating. That doesn't help you if it
> means you have to re-architect a lot of stuff of course.
>
> I notice that Yahoo Finance does regular image updates when you're
> looking at a daily stock chart. They could be using some Ajax method, or
> they might just overwrite the innerHTML once a minute by using a random
> query string on the image.


I wish your random query string on the image worked for me.

The only other thing I can think of is that I need a valid expires date
or last updated date in the content header, and I am not giving one to
the browsers, so that even though I have the correct cache control
values the browser still grabs the image from its cache.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem forcing pages not to cache. damiensawyer@yahoo.com.au HTML 4 05-23-2008 04:18 AM
Firefox browsers cache images David Albert MCSE 0 10-29-2007 10:52 AM
forcing Explorer not to cache javascript code. kaston3 Javascript 3 07-15-2006 12:44 PM
Since MSN CHAT went pay per use. Is their any other free ones out their Hugh Computer Support 8 05-19-2004 05:52 PM
What the pros use to power their flashes... and their digital cameras. Dan Sullivan Digital Photography 21 01-04-2004 04:40 PM



Advertisments