33

After deploying a new version of a website the browser loads everything from its cache from the old webpage until a force refresh is done. Images are old, cookies are old, and some AJAX parts are not working.

How should I proceed to serve the users with the latest version of the page after deploy?

The webpage is an ASP.Net webpage using IIS7+.

Germstorm
  • 9,269
  • 14
  • 61
  • 83

3 Answers3

37

You can append a variable to the end of each of your resources that changes with each deploy. For example you can name your stylesheets:

styles.css?id=1

with the id changing each time.

This will force the browser to download the new version as it cannot find it in its cache.

m.edmondson
  • 28,523
  • 26
  • 113
  • 191
  • 15
    Isn't there an easier way to do this? I am loading 1000s of resources – Germstorm Jun 07 '11 at 16:27
  • 4
    @Germstorm Set a variable in a config file and update it. Add that variable to all resource calls. One update, every URL changes. – ceejayoz Jun 07 '11 at 16:32
  • @ceejayoz - Thanks I didn't think of doing it that way – m.edmondson Jun 08 '11 at 07:58
  • @ceejayoz Old question, but what if the config file gets cached? – NorCalKnockOut Feb 26 '16 at 17:35
  • @NorCalKnockOut The config file would be server-side. If there's caching on it, that's controlled by you and/or your server, not the client. – ceejayoz Feb 26 '16 at 18:01
  • @ceejayoz Ahh duh. Thanks! – NorCalKnockOut Feb 26 '16 at 18:38
  • @ceejayoz can you, please explain how can I use a variable from the config file in my .aspx page? – Asad Jan 06 '17 at 12:43
  • @Asad No clue. In PHP it'd just be something like `styles.css?id=` but I have no idea what similar ASP syntax would be. – ceejayoz Jan 06 '17 at 13:35
  • @ceejayoz Thanks,this is how I managed this in my .aspx page. `` `` – Asad Jan 06 '17 at 16:35
  • 1
    This may seem like a strange question, but if you add the variable to the config file and call that variable in your resource call on the page, what happens when the page gets cached? Since the config file is on the server, the cached web page will call the new config value and get the correct resource, but the page is still cached, right? – Jolley71717 Mar 23 '18 at 19:05
3

For ASP.NET you can use the cache control and expires headers. You can also set up similar headers in IIS 7 for your images. If you have any other cookies you can expire them manually.

I have not tried it, but it looks like you can do an ever better job of bulk setting cache control in IIS 7. See this thread and this link. At that point you are only left with unsetting any custom cookies you have (which you won't be able to control with HTTP cache control settings).

I don't know of any method to "unset everything all at once" easily.

Community
  • 1
  • 1
hross
  • 3,463
  • 2
  • 23
  • 30
2

You could use http headers to control the cache of your clients.

I'll just leave this here for you. http://support.microsoft.com/kb/234067

stefgosselin
  • 8,696
  • 5
  • 38
  • 63
  • That can be bad performance-wise, though. Caching saves users a lot of bandwidth and time. – ceejayoz Jun 07 '11 at 16:42
  • You can (and should) control the cache header, via LAST-MODIFIED directive, or ETags, See [this page](http://www.webscalingblog.com/performance/caching-http-headers-last-modified-and-etag.html) for quick run-down, and [this one for more in depth explanations](http://www.web-caching.com/mnot_tutorial/) – stefgosselin Jun 07 '11 at 16:47
  • Basically, you would need 2 headers: `Cache-Control: must-revalidate` and `Last-Modified: 15 May 2011 17:43:00 GMT` this is the only way I know of to have control of client caching mecanisms. – stefgosselin Jun 07 '11 at 16:49