15

I've noticed that on some websites (including SO) the link to the CSS will look like:

<link rel="stylesheet" href="http://sstatic.net/so/all.css?v=6638"> 

I would say its safe to assume that ?v=6638 tells the browser to load version 6638 of the css file. But can I do this on my websites and can I include different versions of my CSS file just by changing the numbers?

Sripathi Krishnan
  • 30,948
  • 4
  • 76
  • 83
Bob Dylan
  • 4,393
  • 9
  • 40
  • 58

1 Answers1

24

That loads all.css with a different query string so that if version 6637, for instance, is already cached on your machine, you'll get the new one (6638). Changing that number (in this case) will not give you a different file.

This is just a cache trick so they can send the file down with no expiration (i.e. you never have to ask for it again), because when it does change, the "file name", changes.


That said, you could make it so you load a different version based on the query string parameter. Doing so would be slightly non-trivial and akin to how you get different questions when you pass a different question ID to the URL of this page.

Michael Haren
  • 105,752
  • 40
  • 168
  • 205
  • So does the browser just savse the number from the last time you visit and then if it doesn't match the next time, it reloads the CSS file, otherwise it just uses the cached one? – Bob Dylan Apr 14 '10 at 00:29
  • Yes, more or less. The thing here is that the files are sent down with extra data (headers) that say "never ask for this file again--it never changes". Since they do that, the site is faster (one less request each load). BUT when the file *does* change, they communicate that by changing its name. – Michael Haren Apr 14 '10 at 00:30
  • 2
    Try to ignore that the version number is a querystring parameter--think of the whole thing--path, name, query string-- as part of the file name. When any of it change, it's treated as a completely unique "file" or resource. – Michael Haren Apr 14 '10 at 00:32
  • Thanks! Also the edit to your question was great, thanks for clarifying. – Bob Dylan Apr 14 '10 at 00:32