Slim
Slim

Reputation: 5965

How do you prevent the browser from displaying a cached version of a page?

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?

Upvotes: 5

Views: 1666

Answers (6)

Gary.Ray
Gary.Ray

Reputation: 6501

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.

Upvotes: 6

eKek0
eKek0

Reputation: 23289

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.

Upvotes: 2

Shrike
Shrike

Reputation: 9500

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.

Upvotes: 1

whaley
whaley

Reputation: 16265

Set the Cache-Control header to no-cache.

Upvotes: 3

janhartmann
janhartmann

Reputation: 15003

You can add a meta-tag like this.

<meta http-equiv="pragma" content="no-cache" />

Upvotes: 9

Lycana
Lycana

Reputation: 1164

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

Upvotes: 3

Related Questions