tags:

views:

124

answers:

4

Hi,

When I upload a new version of CSS files for my website, there is an issue where the clients viewing the pages might have a cached version on their computers. They can't see the CSS updates (or even some other static assets like images) until they clear their cache, or press Ctrl + F5. This is obviously not an ideal solution.

One way to deal with this would be to rename the CSS files every time I uploaded a new build of the website. Is there any way to do this automatically via Visual Studio? I am using AnkhSVN for version control, so if I could get a script to do this automatically for the selected files, that would work too.

I didn't even know what exactly to Google for - could somebody point me in the right direction, and suggest best practices?

Edit: Oh, and forgot to mention - I would need to do this in combination with the expires tag on static assets. Since I'm still early into my research, I'm not sure how this ties in.

Thanks!

+8  A: 

One simple fix is to add a query string to the url of your css file. So instead of refering to file.css you refer to file.css?a=1. The next time you change the css file you just change the query and the file will be redownloaded.

Rune Grimstad
And real life example of this technique can be found in this site's source code!
ironsam
Interesting - so if the querystring value is the same, the browser knows that it has the latest versions...might be the solution I was looking for!
Alternatively, if your css changes frequently or your site is in development, you can set it up to change to query string to a random value each time the page is displayed. That will force the browser to download the css file each time the page is viewed.
Rune Grimstad
A: 

If you want to use HTTP headers, consider the ETag: http://en.wikipedia.org/wiki/HTTP_ETag

ZippyV
A: 

There's an excellent solution to prevent CSS and Javascript caching by using a function on this blog entry.

It uses the SVN revision number on the query string, but this could easily be modified to use a random number or a date/time string.

Joe Sergeant
+1  A: 

I always use a version number when calling a css. For example

<html>
  <title>
    <script src="myScript.js?v=1.1"/>
    <link rel="stylesheet" href="myStyle.css?v=1.1" />
  </title>
  <body>
  </body>
</html>

By incrementing this number you will instruct the browser to get a new version of the stylesheet/javascript. This also comes in handy when using AJAX requests, some browsers also tend to cache them.

You can adapt this technique and build some code around it to auto increment the number. For example you can use a generated number (or datetime) which is generated at the start of the users session. When the user comes back later he gets a new session, thus a new number.

You can also use the build number from the assembly (you can instruct .NET to auto increment the build number in the assembly), or keep a build number in your web.config which you manually update.

But the key really is to use a querystring behind your script/css reference because browsers not always obey the expire header and using a (new) query string will force the browser to get a new version.

Gertjan