Good Old Caching

Posted on 2002-05-09
Medium Priority
Last Modified: 2010-04-09
Same old story - I am trying to stop my html pages from caching and make it refresh everytime it opens.

I have tried the pragma, no cache META tags at the top head as well as again before the end html tag.

This does not work - certainly in my IE5 anyway.

I have tried the asp equivalent and that does not work either.

Any suggestions!! I'm totally stumped. There is nothing in IIS is there although I have tried everything.

Amazingly it doesn't even refresh after I empty the temporary cached info

Question by:carlmc
LVL 18

Accepted Solution

mgfranz earned 300 total points
ID: 6998896
Using .asp pages;

  '-- Use the following script at the extreme beginning of the specific ASP pages

'-- Prevent caching at the proxy server
Response.CacheControl = "no-store" ' HTTP 1.1 Cache-Control header : prevent all caching of a particular Web resource
'Response.CacheControl = "no-cache" ' HTTP 1.1 Cache-Control header : prevent all caching of a particular Web resource
'Response.CacheControl="private" ' HTTP 1.1 Cache-Control header : prevent the caching at the proxy server
'' Response.CacheControl="public"     ' HTTP 1.1 Cache-Control header : enable caching at the proxy server

' Prevent clients from caching web pages :
' no-cache prevents caching only when used over a secure (https://) connection.
' Treated identically to Expires: -1 if used in a non-secure page.
Response.AddHeader "Pragma", "no-cache" ' (META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE")
'Response.AddHeader "cache-control", "no-store"
' Note the Web page may still be cached in the Temporary Internet Files folder.
' see http://support.microsoft.com/support/kb/articles/Q222/0/64.ASP for reasons & resolutions
'-- Set page expiration date
Response.Expires = -1 ' cached but marked as immediately expired. (META HTTP-EQUIV="Expires" CONTENT="-1")
''Response.ExpiresAbsolute = Now() -1
' However, the page remains in the disk cache ("Temporary Internet Files") and is used in appropriate situations
' without contacting the remote Web server, such as when the BACK and FORWARD buttons are used to access the navigation history

HTML pages;

<META HTTP-EQUIV="expires" CONTENT="-1">

LVL 53

Expert Comment

ID: 6998934
You start with META tags in the HEAD of the page.
Not well supported by some browsers.

The first instructs no caching:
<meta HTTP-EQUIV="Pragma" CONTENT="no-cache">

The second is necessary because IE5 ignores the pragma:

That expires the page as soon as it is born.

However that may still not work in IE because it it is in the head where
it has to be.  In some kind of twisted logic IE sees the META tags, but
because the page has not yet been loaded there is nothing to cache so it
ignores it. Then when the page is loaded, it caches it.  The solution
from Micro$oft is to duplicate the tags after the end of the body.  
However they have to be in the head so you end up with this silly looking,
but effective page setup:

<META HTTP-EQUIV="Pragma" CONTENT="no-cache">

all the other head stuff


All the good stuff that makes the page worthwhile

<META HTTP-EQUIV="Pragma" CONTENT="no-cache">

For ASP if you  want the same non-cache effect, here's the header
<% Response.CacheControl = "no-cache" %>>
<% Response.AddHeader "Pragma", "no-cache" %>
<% Response.Expires = -1 %>

I don't know if once does it for ASP or if you have to repeat
that as well.

And of course Netscrap always has to add a twist of its own.  Pages
with forms in Netscrap can be a problem especially for secure environments.

Netscape suggests the following JavaScript be used in the BODY tag
of all pages that should not be cached:
<body onLoad="if ('Navigator' == navigator.appName) document.forms[0].reset();">

With all of that there is still a small chance that there is still an
unreported bug or a fringe browser that will screw up but this should
be 99% effective.

LVL 18

Expert Comment

ID: 6998950
"I don't know if once does it for ASP or if you have to repeat that as well."

No, ASP is fine with the initial call, however each page that you want to expire needs to have the properties set.

Good form tip for Nestcape CD.
LVL 11

Expert Comment

ID: 7010291
Firstly the Microsoft article that MGFranz quoted is a bit misleading and actually contridicted by the associated article. (http://support.microsoft.com/default.aspx?scid=kb;en-us;Q234067)

Firstly some early varients of IE5 don't actually support HTTP 1.1 although the options say they are sending 1.1 they aren't they are still operating at 1.0.

Secondly and importantly IE4 & 5 DO NOT parse the header of an HTML document and process HTTP-EQUIV tags (with the exception of the refresh). These tags are processed by the server which then converts them into the appropriate http header message. However the server only does this for HTML files not ASP files. Consequently it doesn't matter how many of these META tags you put in a ASP file because nothing is looking at them.

Therefore for an ASP document you will always need to add the caching directives as MGFranz has demonstrated.

The implication by Microsoft that the no-cache pragma is only used for HTTPS is wrong. Sure IE might only use this but if there is a HTTP1.0 proxy in the chain and no no-cache pragma is sent, then the proxy will cache the page (any 1.1 messages will be ignored but passed on). Unless a valid expiration has also been added then subsequent refreshes are likely to grabbed the cached copy from the proxy.

Microsofts suggestion that expires-1 should be used is a little iffy as well. According to the standard the expires directive only works if there is a date header as well. Many servers don't send this out so if they have followed the spec (which I grant you is doubtful) it shouldn't work a lot of the time (I know IIS5 sends it out but I'm pretty sure IIS4 didn't)

There is another little wrinkle in that there is a bug present in IE5 at least that sometimes a page becomes corrupted in cache. Once this occurs the page will always come from the cache regardless of any caching directive or expirations. Significant on a server log because you can see people effectively jump pages (EG Page 1 leads to page 2 but no request of any type is ever made for Page 1). The only way that you will clear this is to clear the browser cache.

So far as Netcrap is concerned the big problem in cache terms is with non html files. All caching and expiring strategies don't work. I don't know about V6 I refuse to put the lump of sh*t on my machine.



Author Comment

ID: 7010454
Thanks MGFranz, your asp code seems to work.
Also thanks mouatts for that explanation.


Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question