php 的 `filemtime` 是浏览器缓存或缓存清除的好主意吗?
Is php's `filemtime` a good idea for Browser Caching or Cache Busting?
我正在使用 php 的 filemtime
为文件添加版本,这是个好主意吗?
<link rel="stylesheet" href="css/custom.css?v=<?=filemtime("./css/custom.css")?>"/>
<script src="js/custom.js?v=<?=filemtime("js/custom.js")?>"></script>
这在源代码中显示为
<link rel="stylesheet" href="css/custom.css?v=1564681659"/>
<script src="js/custom.js?v=1564599819"></script>
?v
和 ?ver
一样吗?
P.S:我正在对 .js 和 css 文件、bootstrap、jquery 等(尽管都是本地文件)做同样的事情。
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.
话虽这么说,您可以使用各种东西来对文件进行版本化。使用 filemtime
是一个很好的方法。我相信这是我见过人们使用的最主流的方式之一。您可以离开它并知道它会始终正常工作并且不会很快发生碰撞。我不知道 ?v
和 ?ver
之间有什么区别,但 ?v
是我见过的 90% 情况下使用的那个,也许更多。希望这有帮助。
我正在使用 php 的 filemtime
为文件添加版本,这是个好主意吗?
<link rel="stylesheet" href="css/custom.css?v=<?=filemtime("./css/custom.css")?>"/>
<script src="js/custom.js?v=<?=filemtime("js/custom.js")?>"></script>
这在源代码中显示为
<link rel="stylesheet" href="css/custom.css?v=1564681659"/>
<script src="js/custom.js?v=1564599819"></script>
?v
和 ?ver
一样吗?
P.S:我正在对 .js 和 css 文件、bootstrap、jquery 等(尽管都是本地文件)做同样的事情。
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.
话虽这么说,您可以使用各种东西来对文件进行版本化。使用 filemtime
是一个很好的方法。我相信这是我见过人们使用的最主流的方式之一。您可以离开它并知道它会始终正常工作并且不会很快发生碰撞。我不知道 ?v
和 ?ver
之间有什么区别,但 ?v
是我见过的 90% 情况下使用的那个,也许更多。希望这有帮助。