Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a queryString doing in this stylesheet's href?

Tags:

css

Browsing the boilerplate code at http://html5boilerplate.com/ I'm puzzled at this usage:

<link rel="stylesheet" href="css/style.css?v=1">
like image 391
justSteve Avatar asked Oct 14 '10 00:10

justSteve


People also ask

What is a query string used for?

A querystring is a set of characters input to a computer or Web browser and sent to a query program to recover specific information from a database .

What is query string in c#?

A query string is a collection of characters input to a computer or web browser. A Query String is helpful when we want to transfer a value from one page to another.

What is a query string example?

A query string commonly includes fields added to a base URL by a Web browser or other client application, for example as part of an HTML, choosing the appearance of a page, or jumping to positions in multimedia content.

What is the query string in php?

A query string is a term for adding/passing data in the URL. It is not language specific.


2 Answers

To force update if it is already in browser cache. the v is probably short for version.

like image 155
Simon Avatar answered Oct 18 '22 01:10

Simon


To expand on Simon's correct answer...

Often to conserve bandwidth, stylesheets (amongst other site assets) send headers to the browser that say they should expire a long time from now (often a year). They also send the 304 not modified header.

This is great, but what if someone wants to update the stylesheet? If it was requested as style.css, and subsequent requests were to style.css, the end user would never redownload it (not for a year anyway).

To combat this, you can append a query string that changes when the file does. For example, it can be easily done in PHP

<?php
    $file = 'style.css';
?>

<style type="text/css" rel="stylesheet" href="<?php echo $file . '?v=' . filemtime($file); ?>" />

Now, when the file is updated, the query string changes and the file is redownloaded to all end users. It will not be downloaded again until (a) the expiry time is up or (b) the query string changes again.

like image 44
alex Avatar answered Oct 18 '22 01:10

alex