过期标头的智能解决方案
我们希望为使用的图像、CSS 和 JavaScript 设置过期标头以提高页面速度,但我们意识到修改 CSS 或 JS 脚本时会出现缓存问题。
是否可以在加载 xhtml 的文件中添加元或其他标记,告诉浏览器刷新每个元素,无论现有图像、css 或 js 设置了什么缓存?
We want to set expires headers for used images, css and javascript to improve pagespeed, but we are aware of the cachingproblem when modifying a css or js script.
Is it possible to add a meta or other tag in the file which loads the xhtml which tells the browser to refresh every element, no matter what caching is set on existing images, css or js?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,没有这样的捷径。
即使是——那又有什么意义呢?发送这样的标头首先就会破坏未来到期标头的目的。
设置过期标头时,您需要向元素添加某种资源版本控制,例如
更改资产的路径将实现相同的目标。
是的,这很麻烦。但是缓存失效是一个难题,确实没有简单的解决方法。
As far as I know, there's no such shortcut.
And even if it was - what would be the point? Sending such a header would defeat the purpose of the future expiration header in the first place.
When setting expiration headers, you need to add some sort of asset versioning to to your elements, such as
<link rel="stylesheet" href="css/style.css?v=2">
Changing the path to the asset would achieve the same goal.
Yes this is a hassle. But cache invalidation is a hard problem, there's really no easy way around it.
您可能想要做的是对静态内容进行版本控制。假设您有一个 main.css 文件,那么您可以通过将其重命名为 main_0.css (只是一个示例)来对其进行版本控制,然后将缓存过期设置为一年。如果您需要更新 main.css,只需增加版本号并更新您的引用即可。然后所有客户端都将获得最新版本。
有几种解决方案可以为您执行此版本控制,但这是基本原则。
当遇到与缓存相关的问题时,我一直发现这些资源非常有用:
希望这有帮助。
编辑
这是一个版本控制解决方案(mod_pagespeed),它执行我上面解释的操作:
What you probably want to do is version your static content. so say you have a main.css file then you can version this by renaming it to main_0.css (just an example) and then you would set the cache expire to a year. if you ever need to update main.css just increment the version number and update your references. Then all clients will get the latest version.
There are several solutions that can do this versioning for you, but this is the basic principle..
I have always found theese resources very usefull when having cache related questions:
Hope this helps.
EDIT
Here is a versioning solution (mod_pagespeed) that does what i explained above:
据我所知还没有。
在我看来这是一件好事。
如果您要对 CSS 或 JS 进行重大更改,那么无论如何您都应该暂存更改。通过将新的 CSS 和 JS 放在不同的路径上来管理这些更改,并在进行更改时更改 HTML 中的引用。这使您可以:
Not as far as I'm aware.
This is a good thing in my opinion.
If you're making drastic changes to CSS or JS then you ought to be staging the changes anyway. Manage these changes by having the new CSS and JS on a different path and change the references in your HTML when making the change. This allows you to: