过期标头的智能解决方案

发布于 2024-11-05 05:24:22 字数 150 浏览 3 评论 0原文

我们希望为使用的图像、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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无声无音无过去 2024-11-12 05:24:22

据我所知,没有这样的捷径。

即使是——那又有什么意义呢?发送这样的标头首先就会破坏未来到期标头的目的。

设置过期标头时,您需要向元素添加某种资源版本控制,例如

更改资产的路径将实现相同的目标。

是的,这很麻烦。但是缓存失效是一个难题,确实没有简单的解决方法。

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.

耳根太软 2024-11-12 05:24:22

您可能想要做的是对静态内容进行版本控制。假设您有一个 main.css 文件,那么您可以通过将其重命名为 main_0.css (只是一个示例)来对其进行版本控制,然后将缓存过期设置为一年。如果您需要更新 main.css,只需增加版本号并更新您的引用即可。然后所有客户端都将获得最新版本。

有几种解决方案可以为您执行此版本控制,但这是基本原则。

当遇到与缓存相关的问题时,我一直发现这些资源非常有用:

  1. http://code.google.com/intl/no/speed/page-speed/docs/caching.html
  2. http://code.google.com/intl/no /speed/page-speed/docs/filters.html

希望这有帮助。

编辑

这是一个版本控制解决方案(mod_pagespeed),它执行我上面解释的操作:

  1. http://code.google.com/intl/no/speed/page-speed/docs/filter-cache-extend.html

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:

  1. http://code.google.com/intl/no/speed/page-speed/docs/caching.html
  2. http://code.google.com/intl/no/speed/page-speed/docs/filters.html

Hope this helps.

EDIT

Here is a versioning solution (mod_pagespeed) that does what i explained above:

  1. http://code.google.com/intl/no/speed/page-speed/docs/filter-cache-extend.html
貪欢 2024-11-12 05:24:22

是否可以在加载 xhtml 的文件中添加元或其他标记,告诉浏览器刷新每个元素,无论现有图像、CSS 或 js 设置了什么缓存?

据我所知还没有。

我们希望为使用过的图像、CSS 和 JavaScript 设置过期标头,以提高页面速度

在我看来这是一件好事。

但是我们知道修改 css 或 js 脚本时存在缓存问题。

如果您要对 CSS 或 JS 进行重大更改,那么无论如何您都应该暂存更改。通过将新的 CSS 和 JS 放在不同的路径上来管理这些更改,并在进行更改时更改 HTML 中的引用。这使您可以:

  • 立即推出/返回
  • 通过动态生成这些引用进行选择性推出
  • 网络日志现在提供清晰的审计跟踪以跟踪错误报告
  • 避免 CSS 和 JS 短期缓存的任何问题

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?

Not as far as I'm aware.

We want to set expires headers for used images, css and javascript to improve pagespeed

This is a good thing in my opinion.

but we are aware of the cachingproblem when modifying a css or js script.

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:

  • instantly roll out / back
  • make selective rollouts by dynamically generating those references
  • web logs now provide clear audit trail for following up bug reports
  • avoid any issues with short term caching of CSS and JS
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文