What does appending "?v=1" to CSS and JavaScript URLs in link and script tags do?
I have been looking at a HTML 5 boilerplate template (from http://html5boilerplate.com/) and noticed the use of "?v=1"
in URLs when referring to CSS and JavaScript files.
- What does appending
"?v=1"
to CSS and JavaScript URLs in link and script tags do? - Not all JavaScript URLs have the
"?v=1"
(example from the sample below:js/modernizr-1.5.min.js
). Is there a reason why this is the case?
Sample from their index.html
:
<!-- CSS : implied media="all" -->
<link rel="stylesheet" href="css/style.css?v=1">
<!-- For the less-enabled mobile browsers like Opera Mini -->
<link rel="stylesheet" media="handheld" href="css/handheld.css?v=1">
<!-- All JavaScript at the bottom, except for Modernizr which enables HTML5 elements & feature detects -->
<script src="js/modernizr-1.5.min.js"></script>
<!------ Some lines removed ------>
<script src="js/plugins.js?v=1"></script>
<script src="js/script.js?v=1"></script>
<!--[if lt IE 7 ]>
<script src="js/dd_belatedpng.js?v=1"></script>
<![endif]-->
<!-- yui profiler and profileviewer - remove for production -->
<script src="js/profiling/yahoo-profiling.min.js?v=1"></script>
<script src="js/profiling/config.js?v=1"></script>
<!-- end profiling code -->
Solution 1:
These are usually to make sure that the browser gets a new version when the site gets updated with a new version, e.g. as part of our build process we'd have something like this:
/Resources/Combined.css?v=x.x.x.buildnumber
Since this changes with every new code push, the client's forced to grab a new version, just because of the querystring. Look at this page (at the time of this answer) for example:
<link ... href="http://sstatic.net/stackoverflow/all.css?v=c298c7f8233d">
I think instead of a revision number the SO team went with a file hash, which is an even better approach, even with a new release, the browsers only forced to grab a new version when the file actually changes.
Both of these approaches allow you to set the cache header to something ridiculously long, say 20 years...yet when it changes, you don't have to worry about that cache header, the browser sees a different querystring and treats it as a different, new file.
Solution 2:
This makes sure you are getting the latest version from of the css or js file from the server.
And later you can append "?v=2"
if you have a newer version and "?v=3", "?v=4"
and so on.
Note that you can use any querystring
, 'v' is not a must for example:
"?blah=1
" will work as well.
And
"?xyz=1002"
will work.
And this is a common technique because browsers are now caching js and css files better and longer.