Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Css file caching

Tags:

css

caching

I have a system where cache max-age is set to 0 and there is problem, when I have made some changes in my style.css fail the changes dont apear to client. Browser will use the old cached version of css. I have simple question: Does naming css file as style.css?123 will be cached as a new?

like image 322
Tim Avatar asked Nov 04 '10 09:11

Tim


2 Answers

Yes, adding a unique query string to the resource's URI will force the client to fetch a "fresh" version (since the client doesn't know that it's merely an update of a previously cached resource). This is known as fingerprinting and you typically use a timestamp or an incrementing version number1 of the CSS file.

Google Web Fundamentals has a great article on HTTP cache optimization. Especially the section titled "Invalidating and updating cached responses:"

How do you get the best of both worlds: client-side caching and quick updates? You change the URL of the resource and force the user to download the new response whenever its content changes. Typically, you do this by embedding a fingerprint of the file, or a version number, in its filename—for example, style.x234dff.css.

Do note, that the fingerprint does not need to be a sequential number. Any value – hash, version, etc. – will do as long as the risk of collisions is limited.


1) This is what's done here on SO, e.g. http://sstatic.net/js/global-login.js?v=12

like image 161
jensgram Avatar answered Oct 21 '22 11:10

jensgram


You can append a unique query string, although this will use bandwidth.

You can rename your CSS file every time you make a change, IE:

main-v1.css main-v2.css main-v3.css

And then re-reference it in your pages. This saves bandwidth and forces browsers to reload it.

like image 31
Tom Gullen Avatar answered Oct 21 '22 13:10

Tom Gullen