Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why adding version number to CSS file path?

Tags:

css

I noticed some websites put the version numbers (especially) in the CSS file path. For example:

<link rel="stylesheet" type="text/css" href="style.css?v=12345678" /> 

What is the main purpose to put the version number? If the purpose is to remember when the CSS file was updated last time, shouldn't the version number added as a comment inside the CSS file?

like image 923
user966585 Avatar asked Oct 06 '11 07:10

user966585


People also ask

How do I add a version number to a CSS file?

To use CSS versioning, you simply need to add a query string parameter on the href attribute in your HTML page. The query string parameter ? v=2 after the style. css file name is the CSS versioning trick.

Does CSS have a version?

CSS was released in 2017. The latest version is W3. CSS 4.15 December 2020.

What are the versions of CSS?

There are three versions of CSS – CSS1, CSS2 and CSS3.

How do I know my CSS version?

You can find out what CSS features are supported in which browsers from the following sites: http://www.quirksmode.org/css/contents.html.


1 Answers

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.

like image 60
aviraldg Avatar answered Nov 04 '22 21:11

aviraldg