What Does Appending "V=1" to CSS and JavaScript Urls in Link and Script Tags Do

What does appending ?v=1 to CSS and JavaScript URLs in link and script tags do?

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.

How does appending ?v=1 in script load new javascript

  1. Most web servers will serve up a static file no matter what query string you put at the end of the URL.
  2. Browsers cache data based on its URL.
  3. Changing the URL to a different one means you won't get the cached version from the old URL but, given (1), the (new version of the) file stored in the same location on the server's disk will be served.

But how does this help? How the query string going to work? I don't have the file name changed nor did I wrote any script for doing anything with query string (?v=1) .

Then it doesn't help. You have to change the query string when you change the file.

What am I missing? Do I need to change the file name

No

or do I need to have a repository for sure to get this working?

You need to change the query string when you release a new version of the file.

One way to do that would be to use a build script that sets the URL to (for example) the commit id that last changed the file in a version control repository.

whats does the ?history=1 mean after a .js file?

It ensures that users have the latest version of a JS or CSS file in case an older version is cached by their browser.

What does ?v=2 mean in css?

If you set caches to expire far in the future adding ?v=2 will let the server know this is a new file but you won't need to give it a unique name ( saving you a global search and replace)

Append Dynamic version(variable ) in Script tag and stylesheet based on time

If you are looking for the shortest solution, how about this?

<script>document.write('<link href="/assets/cder.css?v=' + Date.now() + '" rel="stylesheet" />');</script>

A worthy alternative should be:

<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = '/assets/cder.css?v=' + Date.now();
document.body.appendChild(link);
</script>

Well, you must escape the closing script tag as follows:

<script>document.write('<script src="/assets/abc.js?v=' + Date.now() + '"><\/script>');</script>

An example of how to add several scripts:

<script>  var scripts = [    '/assets/abc.js',    '/assets/def.js',  ];
for (var i = 0; i < scripts.length; i++) { var script = document.createElement('script'); script.onerror = function() { alert('Could not load ' + this.src); }; script.src = scripts[i] + '?v=' + Date.now(); document.body.appendChild(script); }</script>

Why adding version number to CSS file path?

From HTML5 ★ Boilerplate Docs:

What is ?v=1" '?v=1' is the JavaScript/CSS Version Control with
Cachebusting

Why do you need to cache JavaScript CSS? Web page designs are getting
richer and richer, which means more scripts and stylesheets in the
page. A first-time visitor to your page may have to make several HTTP
requests, but by using the Expires header you make those components
cacheable. This avoids unnecessary HTTP requests on subsequent page
views. Expires headers are most often used with images, but they
should be used on all components including scripts, stylesheets etc.

How does HTML5 Boilerplate handle JavaScript CSS cache? HTML5
Boilerplate comes with server configuration files: .htacess,
web.config and nginx.conf. These files tell the server to add
JavaScript CSS cache control.

When do you need to use version control with cachebusting?
Traditionally, if you use a far future Expires header you have to
change the component's filename whenever the component changes.

How to use cachebusting? If you update your JavaScript or CSS, just
update the "?v=1" to "?v=2", "?v=3" ... This will trick the browser
think you are trying to load a new file, therefore, solve the cache
problem.

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

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.



Related Topics



Leave a reply



Submit