Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why pass parameters to CSS and JavaScript link files like src="../cnt.js?ver=4.0"?

Tags:

When I saw many sites' source code, parameters were passed to the linking file (CSS/JavaScript).

In the Stack Overflow source, I got

<script type="text/javascript" src="http://sstatic.net/js/master.js?v=55c7eccb8e19"></script> 

Why is master.js?v=55c7eccb8e19 used?

I am sure that JavaScript/CSS files can't get the parameters.

What is the reason?

like image 850
Aakash Chakravarthy Avatar asked Jun 28 '10 10:06

Aakash Chakravarthy


People also ask

Can you mix CSS and Javascript?

No, you can't combine js and css into one file.


2 Answers

It is usually done to prevent caching.

Let's say you deploy version 2 of your new application and you want to cause the clients to refresh their CSS, you could add this extra parameter to indicate that it should re-request it from the server. Of course there are other approaches as well, but this is pretty simple.

like image 194
Evan Trimboli Avatar answered Oct 18 '22 08:10

Evan Trimboli


As the others have said, it's probably an attempt to control caching, although I think it's best to do so by changing the actual resource name (foo.v2.js, not foo.js?v=2) rather than a version in the query string. (That doesn't mean you have to rename files, there are better ways of mapping that URL to the underlying file.) This article, though four years old and therefore ancient in the web world, is still a quite useful discussion. In it, the author claims that you don't want to use query strings for versions because:

...According the letter of the HTTP caching specification, user agents should never cache URLs with query strings. While Internet Explorer and Firefox ignore this, Opera and Safari don’t...

That statement may not be quite correct, because what the spec actually says is

...since some applications have traditionally used GETs and HEADs with query URLs (those containing a "?" in the rel_path part) to perform operations with significant side effects, caches MUST NOT treat responses to such URIs as fresh unless the server provides an explicit expiration time...

(That emphasis at the end is mine.) So using a version in the query string may be fine as long as you're also including explicit caching headers. Provided browsers implement the above correctly. And proxies do. You see why I think you're better off with versions in the actual resource locator, rather than query parameters (which [again] doesn't mean you have to constantly rename files; see the article linked above for more). You know browsers, proxies, etc. along the way are going to fetch the updated resource if you change its name, which means you can give the previous "name" a never-ending cache time to maximize the benefit of intermediate caches.

Regarding:

I am sure that Js/CSS files can't get the parameters.

Just because the result coming back is a JavaScript or CSS resource, it doesn't mean that it's a literal file on the server's file system. The server could well be doing processing based on the query string parameters and generating a customized JavaScript or CSS response. There's no reason I can't configure my server to route all .js files to (say) a PHP handler that looks at the query string and returns something customized to match the fields given. Thus, foo.js?v=2 may well be different from foo.js?v=1 if I've set up my server to do so.

like image 23
T.J. Crowder Avatar answered Oct 18 '22 09:10

T.J. Crowder