21

I have seen this in many sites:

<script src="/file.js?query=string"></script>

<link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/stackoverflow/all.css?v=86c1a91ea87b">
<link rel="apple-touch-icon image_src" href="//cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png?v=fd7230a85918">

Why are developers passing a query string for these scripts and stylesheets, etc.?

Update: I agree this is duplicate But can you tell me how to 'search' to get those original questions?

user3699274
  • 335
  • 1
  • 2
  • 9
  • 10
    That's a cache-buster. – SLaks Jun 22 '14 at 18:06
  • 3
    There's no reason to waste bandwidth by redownloading resources that don't change. The problem is if you *do* change them, browsers won't download the new version if the URL is the same. Adding a query string with a version number or the file hash will force browsers to redownload the new version of your resource. – Blender Jun 22 '14 at 18:07
  • Related (when and when not do this): http://stackoverflow.com/questions/3870726/force-refresh-of-cached-data/3870743 – Tim M. Jun 23 '14 at 00:01
  • Bad practice, change the filename instead without appending a query string. eg. myfile.v1.js becomes myfile.v2.js – Basil Musa Dec 12 '16 at 06:44

4 Answers4

28

This is used for invalidating caches.

Check out this brief (albeit old) explanation: http://css-tricks.com/update-on-css-caching/

When the query string is updated, it forces the client's browser to download an updated copy of the stylesheet or script.

Mulan
  • 129,518
  • 31
  • 228
  • 259
  • 2
    Note that some browsers do not take querystrings into account when determining if a file name is different. So, changing the name of the actual file is more bulletproof. A good opportunity to use [semver](http://semver.org/). Alternatively, you may append a hash to the basename of the file: `myscript.js` becomes `myscript.2fc57a.js` – Kevin Dice Jun 20 '16 at 15:38
8

One thing that can be used for is to force the browser to re-pull scripts or stylesheets, by changing the query string. That way, in case the file ever changes, and the site wants to force any users to pull the new one instead of using a cached copy, they just change the query string.

For example:

<script src="//whatever.com/something.js"></script>

If you have this on your page, then a browser will pull it down once, then probably cache it for awhile, so every time your page loads, they'll use the cached copy unless they have a reason to try to re-pull (like F5-refreshing the page).

If you use a random query string, then once you change your query string in your markup, the browser has to go pull a new one, since the browser thinks it's a new file (regardless of whether or not it actually is). And the content server will ignore the query string parameters on static files, so it doesn't care what you put:

<script src="//whatever.com/something.js?v=12345"></script>

The browser will grab the file again, whether it needs it or not.

<script src="//whatever.com/something.js?v=98765"></script>

Now it will do it again.

Joe Enos
  • 39,478
  • 11
  • 80
  • 136
  • Isn't F5 refreshing a regular refresh? and ctrl+F5 is a hard refresh? – Brad Jun 23 '14 at 05:01
  • `CTRL+F5` or `SHIFT+F5` is known as "no-cache refresh". See: http://superuser.com/questions/220179/how-can-i-do-a-cache-refresh-in-google-chrome – skytreader Jun 23 '14 at 08:16
  • Try firing up Fiddler and watching the different behavior as you do things - it's pretty interesting if you haven't seen it before. For example, jquery.com, filter Fiddler to jquery.min.js. You see a full request/response the 1st time. If you move page-to-page, it doesn't even show up because the browser has cached it and unquestionably uses its copy. If you F5-refresh, you get a request with If-Modified-Since, & a 304-Not Modified response - if the file had changed, a regular F5 would've picked up the change (assuming the server isn't lying). Ctrl-F5, you get a full request/response again. – Joe Enos Jun 23 '14 at 14:54
3

The developers of a web application which are in evolving stage and get changed often encounter the issue that their JavaScript code or CSS changes do not start working until you clear the cache of browser. In case of a live application, you cannot ask users to clear the cache, so to ensure that changes start working developers append the query string to CSS or JavaScript files.

The reason to do this is because browsers cache the GET calls, so multiple calls to JavaScript, that is,

<script src="//example.com/myscript.js"></script>

will not always get the new copy so to overcome this query-string helps:

<script src="//example.com/myscript.js?v=62345"></script>

Any random unique data as query string tells the browser that it is a different call from the previous one, so it always get the new copy. Stack Overflow is also using this technique.

That caching is helpful in cases when your files never get changed or rarely change like a jQuery file that is the reason it is recommended to use a CDN for these files as it already get cached by some other website.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
1

One possibility besides attempting to prevent the CSS from being cached is that those sites are generating their files server-side. The original question, before it was edited to change its meaning, was asking about "scripts and style sheets etc". Because I saw the script element first, which doesn't have a value appended to it that looks like a hash value, my answer would be that it's possible that they are trying to roll all of their scripts into a single file to cut down on HTTP requests, which will speed up their site. That is a recommended best practice for speeding up your site by Yahoo.

After pulling together the appropriate files, the server can then save them to a file as a cached version and avoid fetching everything dynamically again unless the individual pieces are updated.

If I was using PHP:

$page = $_GET['query'];

switch($page)
{
    case 'homepage':
      /* There would be some code here, checking to see if there's 
       * a cached version that could be served up, before doing the 
       * extra work of rolling the scripts together */
      foreach($scripts as $script)
          $combined_script_file .= $script;
      echo $combined_script_file;
      ...
    break;
    case 'blog':
      ...
}

There are plenty of different use cases for server-side processing of files. This is just one.

Alex W
  • 37,233
  • 13
  • 109
  • 109