.htaccess:缓存控制,如何处理网站更新?

发布于 2024-10-19 06:30:54 字数 552 浏览 5 评论 0原文

我刚刚搜索了网络,但找不到一个好的答案:

FF 的 Google 页面速度扩展告诉我在我的网站 (PHP) 上缓存文件。因此,我更新了我的 .htaccess (在网站的 beta 区域)以缓存某些类型的文件:

ExpiresActive On
ExpiresDefault A0
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=2592000, public"
</FilesMatch>

在 beta 区域编码时,我注意到由于缓存控制设置,我例如,需要按 F5 才能获取最新的 .css 文件。这对我来说还不错……但是用户呢?

那么,我可以告诉浏览器(仅)在我更新站点(或文件过期)时重新下载所有文件,如果没有则使用缓存吗?

如果我能告诉浏览器:“嘿,更新时间之前的所有文件都是旧的,请重新下载它们 - 但是更新时间之后的文件可以,使用缓存,那就完美了。”

I just searched the web but could not find a good answer to this:

The Google page speed extension for FF told me to cache files on my website (PHP). Therefore I updated my .htaccess (in my beta-area of the website) in order to cache certain types of files:

ExpiresActive On
ExpiresDefault A0
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=2592000, public"
</FilesMatch>

While coding in the beta area, I noticed that due to the cache control settings, I need to press F5 to get the lastest .css file for example. That's not bad for me... however what about the users?

So can I tell the browser to re-download all files (only) when I update my site (or the file expires) and use the cache if not?

It would be perfect if I could tell the browser: "Hey, all files before Update-time are old, please re-download them - however files after Update-time are ok, use the cache."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

最偏执的依靠 2024-10-26 06:30:54

这是我有时使用的简单方法,不需要任何复杂的配置。

每当您修改 css 或 javascript 文件时,只需向标记添加一个虚拟参数即可。我通常使用当前日期和/或时间。例如:

这会强制浏览器在以下情况下下载文件的新副本:您需要更新它,同时仍然允许您在幕后保持一致的文件名。

Here's a simple approach I sometimes use, which doesn't require any complication configuration.

Whenever you modify a css or javascript file, simple add a dummy parameter to the markup. I typically use the current date and/or time. For example:

<link type="text/css" rel="stylesheet" href="site.css?120911" />

This forces the browser to download a new copy of the file when you need to update it, while still allowing you to maintain consistent file names behind the scenes.

余罪 2024-10-26 06:30:54

出于性能原因,您已向浏览器指示它应该缓存文件,而无需在很长时间之前向服务器请求该文件。

这就是浏览器正在做的事情:使用缓存中的文件,而不是向服务器询问任何内容——这是正确的行为。

当您修改文件并强制浏览器重新请求该文件并获取新版本时,唯一的解决方案就是更改其 URL

通常,这是通过在文件名称中集成版本号来完成的:

http://www.yoursite.com/my-file-123.css

并且,当文件更新时:

http://www.yoursite.com/my-file-124.css

由于 URL 已更改,浏览器的缓存中不会包含该文件,并且从服务器请求新版本。

当然,手动执行此操作相当不容易,而且很容易出错...

所以,通常(前提是您有一个构建脚本,可以从源代码控制中提取网站的源代码,并将它们打包用于生产),您将将此过程集成到站点的构建脚本中。

For performances reasons, you have indicated to the browser that it should cache a file, without requesting it from the server before a long time.

That's what the browser is doing : using the file from the cache, not asking anything from the server -- and this is the right behavior.

When you modify a file, to force browsers to re-request it, and get the new version, the only solution you have is to change its URL.

Typically, this is done by integrating a version number in the file's names :

http://www.yoursite.com/my-file-123.css

And, when a the file is updated :

http://www.yoursite.com/my-file-124.css

As the URL has changed, the browser doesn't have the file in its cache, and requests the new version from the server.

Of course, doing this by hand is quite not easy, and is error-prone...

So, generally (provided you have a build-script, that extracts the sources of your website from source-control, and packages them for production), you'll integrate this process in your site's building script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文