资源 URL 中的 ETag 或哈希值,例如。 js、css、图像
我想确保每次更改 Web 应用程序中的资源(css、js 和图像)时都会刷新它们,并保留相同的资源名称(这样我就不需要更改 html 中对其的引用)。所以我想至少有两个明显的解决方案:
- 在响应标头中包含 ETAG
- 在 Url 中包含哈希(即 /css/{hash}/style.css)
我更喜欢第二个想法,因为也许一些较旧的代理或浏览器会忽略 etag - 是这样吗?但在 css 中将哈希值放入图像的 url 中要困难一些。所以最后我想我会选择图像的第一个选项,以及 css 和 js 的第一个和第二个选项。
你有什么想法?首先就足够了,如果资源发生变化,每个相当现代的软件都会请求刷新资源。
I want to make sure resources in web application (css, js, and images) will be refreshed every time I change them leaving the same name of the resource (so that I dont need to change references in html to it). So I guess there are at least two obvious solutions:
- Include ETAG in the response header
- Include Hash in Url (ie /css/{hash}/style.css)
I liked second idea better, because maybe some older proxies or browser would ignore etag - is that right ? But it is bit more difficult to put hashes into url of images in css. So finally I guess I will go with the first option for images, and both the first and the second for css and js.
What are your thougts ? Would first just be enough, and every fairly modern software will request refresh of the resource if it will change.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关于第二个,您可以使用 Apache 重写规则来完成此任务。我假设您可以使用您使用的任何编程语言对文件的修改/创建日期进行哈希处理。
.htaccess
RewriteEngine 开启
重写库 /
RewriteRule ^css/[^/]+/style.css$ css/style.css [L]
Concerning the second, you could use a Apache Rewrite Rule to accomplish that. I assume you could hash the date modified/created of the file with whatever programming language you are using.
.htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^css/[^/]+/style.css$ css/style.css [L]
根据我的经验,浏览器/代理通常会尊重 etag。不过,我通常发现我还需要设置上次修改时间。还要确保您的 etag 格式正确,因为某些浏览器对此很挑剔。
From my experience an etag is usually respected by browsers/proxies. However I've generally found that I've needed to set the last modified also. Also make sure that you have the etag formed correctly as some browsers are picky about that.