views:

430

answers:

6

One solution would be to add a QueryString variable to the url that is a random guid, but that seems a little messy. Is there a setting somewhere that will prevent the browser from displaying a cached version of a page?

+3  A: 

Hi Slim, ONE aproach would be to add an 'Expires or a Cache-Control Header'.

This has been extracted from Yahoo Best Practices (http://developer.yahoo.com/performance/rules.html)

There are two things in this rule:

* For static components: implement "Never expire" policy by setting far future Expires header
* For dynamic components: use an appropriate Cache-Control header to help the browser with conditional requests

Browsers (and proxies) use a cache to reduce the number and size of HTTP requests, making web pages load faster. A web server uses the Expires header in the HTTP response to tell the client how long a component can be cached. This is a far future Expires header, telling the browser that this response won't be stale until April 15, 2010.

  Expires: Thu, 15 Apr 2010 20:00:00 GMT

If your server is Apache, use the ExpiresDefault directive to set an expiration date relative to the current date. This example of the ExpiresDefault directive sets the Expires date 10 years out from the time of the request.

  ExpiresDefault "access plus 10 years"

So essentially you will be able to set the expiry date to 'inform' the browser that cached component has expired. Therefore the browser will go and request for the site again.

Assuming you need this for web development, another way would be to force clear the cache. on Firefox, this can be done through CTRL + F5 or CTRL + SHIFT + R.

Hope this helps, Lucas

Lycana
+10  A: 

You can add a meta-tag like this.

<meta http-equiv="pragma" content="no-cache" />
meep
+3  A: 

Set the Cache-Control header to no-cache.

whaley
+5  A: 

In our ASP.Net projects we create a BasePage all other pages inherit from. In the base page we have a function

Public Sub DisableCaching()
    With Response
        .Expires = 0
        .ExpiresAbsolute = Date.Today.AddDays(-1)
        .AddHeader("pragma", "no-cache")
        .AddHeader("cache-control", "no-cache")
    End With
End Sub

We call that on any page we don't want cached.

Gary.Ray
the lines:1.- .AddHeader("cache-control", "no-cache")2.- .CacheControl = "no-cache"Are doing exactly the same thing that is adding a header cache-control with "no-cache" value.
backslash17
Any way: this is the correct way to do it in VB.NET
backslash17
I removed the duplicate line - thx backslash17
Gary.Ray
Ath what stage of the cycle would be the most effective place to call this method?
Anthony
In our projects it is usually the first line in Page_Load.
Gary.Ray
+1  A: 

One solution would be to add a QueryString variable to the url that is a random guid, but that seems a little messy

Why messy? It's the most reliable way. It is not necessary guid, it could be current time.

Shrike
JohnFx
I didn't mean to offend. It just doesn't feel right to me.
Slim
One major disadvantage is search engines may consider every single URL to be a separate page. e.g. index?guid=1234 and index?guid=1235 are distinct URLs as far as most search engines are concerned, even if they contain the exact same content. URLs are for identifying resources, headers are for controlling caching.
Frank Farmer
+2  A: 

Try any of this:

Response.Cache.SetExpires(DateTime.Now.AddSeconds(360));
Response.Cache.SetCacheability(HttpCacheability.Private)
Response.Cache.SetSlidingExpiration(true);

Page.Response.Cache.SetCacheability(HttpCacheability.NoCache)

Also see this question.

eKek0