queryString在这个样式表的href中做了什么?

时间:2023-01-25 10:52:35

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

浏览http://html5boilerplate.com/上的样板代码我很困惑这个用法:

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

2 个解决方案

#1


10  

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

如果已在浏览器缓存中,则强制更新。 v可能是版本的缩写。

#2


7  

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.

通常为了节省带宽,样式表(以及其他站点资产)向浏览器发送标题,表示它们应该从现在(通常是一年)过期很长时间。他们还发送304未修改的标题。

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).

这很好,但如果有人想要更新样式表怎么办?如果它是作为style.css请求的,并且后续请求是style.css,那么最终用户永远不会重新下载它(无论如何都不会重新下载)。

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中轻松完成

<?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.

现在,当文件更新时,查询字符串会更改,文件将重新加载到所有最终用户。在(a)到期时间结束或(b)查询字符串再次更改之前,不会再次下载。

#1


10  

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

如果已在浏览器缓存中,则强制更新。 v可能是版本的缩写。

#2


7  

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.

通常为了节省带宽,样式表(以及其他站点资产)向浏览器发送标题,表示它们应该从现在(通常是一年)过期很长时间。他们还发送304未修改的标题。

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).

这很好,但如果有人想要更新样式表怎么办?如果它是作为style.css请求的,并且后续请求是style.css,那么最终用户永远不会重新下载它(无论如何都不会重新下载)。

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中轻松完成

<?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.

现在,当文件更新时,查询字符串会更改,文件将重新加载到所有最终用户。在(a)到期时间结束或(b)查询字符串再次更改之前,不会再次下载。