如何将远期过期标头添加到缩小的 css 文件/脚本中?
如何将远期过期标头添加到缩小的 css 文件/脚本中?我正在使用 minify 来组合 css 文件和 javascript 文件,但缩小版本没有遥远的到期日期。
how to i add add far-future expires headers to minified cssfiles/scripts? I am using minify to combine css files and javascripts files, but the minified versions don't have a far-future expiration date.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 Apache,那么这种事情就是正确的选择。根据安装的模块,有几种不同的方法可以做到这一点;有些比其他更容易。我推荐
expires
(docs) 和headers
(docs) 模块(它们'下面的示例中两者都需要,但如果您确实需要,可以仅使用headers
来实现)。此示例匹配 /js/ 中以 .js 扩展名结尾的所有文件,并设置相对于文件被访问时间的未来 10 年的到期时间。它还显式地将 Cache-Control 设置为公共;我们通过 SSL 运行所有内容,因此可能没有必要,但无论如何它都不会伤害你。
该示例也可以轻松扩展以匹配您的 CSS 位置和文件;只需复制、粘贴并更改位置匹配即可。
有很多网站可以为您提供这方面的完整概述;查看这个“缓存教程”,它似乎涵盖了所有内容。
If you're using Apache, then this sort of thing is the way to go. There are several different ways to do it depending on the modules installed; some make it easier than others. I recommend the
expires
(docs) andheaders
(docs) modules (they're both required for the example below, but you can pull it off with onlyheaders
if you really want).This example matches all files in /js/ that end with a .js extension and set an expiry time of 10 years into the future relative to the time the file is accessed. It also explicitly sets Cache-Control to be public; we run everything over SSL, so it might not be necessary otherwise, but it won't hurt you either way.
This example can easily be extended to match your CSS locations and files as well; just copy, paste and change the LocationMatch.
There are plenty of sites that will give you a full rundown on this; check out this one, "Caching Tutorial", which seems to cover it all.
/min/README.txt 有 发送远期过期标头的文档。
/min/README.txt has documentation for sending far-future expires headers.
这取决于您使用的网络服务器。不过,这不能通过修改 CSS 或脚本文件本身来完成。
That depends on what web server you are using. It can't be done by modifying the CSS or script files themselves, though.