开发者

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

Browsing the boilerplate code at http://html5boilerplate.com/ I'm puzzl开发者_如何学Ced at this usage:

<link rel="stylesheet" href="css/style.css?v=1">


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜