如何添加“版本”查询字符串到网页静态资源的 URL?
我注意到 Stack Overflow 添加了 ?v=xxxxxxxxxxxx
:
输入图像描述">
Cache-Control max-age=604800
这些静态资源发送的 Cache-Control 响应标头相结合,可确保相同的脚本或样式表(在浏览器中)缓存一周。
我想使用这种技术。您能告诉我如何将该查询字符串添加到 URL 中吗?我的源代码如下所示:
<script src="js/script.js"></script>
我
<link href="css/style.css" rel="stylesheet">
假设 v
的值由文件的 Last-Modified 值确定。这是否意味着对于我的 .php
网页的每个请求,我都必须访问每个静态资源的最后修改信息? (我觉得这会对性能造成影响。)
I've noticed that Stack Overflow adds ?v=xxxxxxxxxxxx
:
to the scripts and style sheets that are bound to the web-page. This, in conjunction with the Cache-Control
response header
Cache-Control max-age=604800
which is being sent for those static resources, makes sure that the same script or style sheet is cached (in the browser) for one week.
I would like to use this technique. Could you tell me how that query string is added to the URLs? My source code looks like so:
<script src="js/script.js"></script>
and
<link href="css/style.css" rel="stylesheet">
I assume that the value of v
is determined by the Last-Modified value of the file. Does that mean that for every request to my .php
web-page, I have to access the last-modified information for every static resource? (I feel that would be a performance hit.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,为了方便起见,我将
filemtime()
作为 GET 参数附加。有些人会附加版本控制中的数字,例如修订号。Generally, for ease, I append the
filemtime()
as a GET param. Some people append a number from version control, such as a revision number.不同(版本)的页面模板将需要不同版本的 .js 和 .css - 因此您可以将依赖版本字符串静态嵌入到页面模板中。
Different (versions of) page templates will need different versions of the .js and .css - so you can statically embed the dependent versions string into the page tempalte.