部署后强制刷新缓存
如果您无法更改引用该文件的 URI(例如,无法添加时间戳参数),是否有办法强制客户端缓存重新加载 HTML 文件?
这是我的情况:
- 一个插件部署到 1000 个用户
- 该插件加载
example.com/page.html
,它调用script.js
- 资源 URI
example.com /page.html
无法更改(无插件更新) page.html
已更改。我需要从用户缓存中清除旧的page.html
,以便加载新的page.html
。
有什么想法吗?访问?旧的 PHP API新的 page.html
调用?
谢谢!
Is there a way to force a client's cache to reload an HTML file if you can't change the URI referencing that file (e.g., can't add a timestamp param)?
Here's my situation:
- A plugin deployed to a 1000 users
- That plugin loads
example.com/page.html
which calls onscript.js
- The resource URI
example.com/page.html
cannot be changed (w/o plugin updates) page.html
has been changed. I need to clear the oldpage.html
from users' cache so the newpage.html
can load.
Any ideas? Htaccess? The PHP API that the old & new page.html
call on?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,如果浏览器已经缓存了页面,则很难告诉它不要使用其缓存版本,因为在确定其缓存版本已过时之前,它可能不会费心再次检查。您只需向所有用户发送一封蜗牛邮件,通知他们按 ctrl+f5 :)
浏览器有可能至少会尝试使用 HEAD 请求来检查修改后的时间戳,然后再提供其服务不过,缓存版本。在这种情况下,以下内容将为您提供帮助。
浏览器使用 HTTP 标准标头从 Web 服务器协商其内容。今后,如果您想告诉浏览器不要缓存文件,则必须发送适当的 HTTP 标头。如果您想在 PHP 中执行此操作,您可以使用
header
函数将适当的 HTTP 标头发送到浏览器:如果必须通过 HTML 完成,您可以在页面标头中执行以下操作
:但是,您无法确定浏览器是否会满足您不缓存页面的请求。还有一些其他东西,例如电子标签之类的东西,但坦率地说,如果页面已经缓存,我认为这不会对您有帮助。
更新
来自 上的 HTTP/1.1 规范响应可缓存性:
Well, if the page is already cached by a browser it's difficult to tell it not to use its cached version because it probably won't bother to check again before it determines its cached version is stale. You'll just have to send a snail-mail letter to all of your users informing them to press ctrl+f5 :)
There is a chance that the browser might at least try a HEAD request to check the modified timestamp before it serves up its cached version, though. In this case the following will help you out.
Browsers negotiate their content from your web server using HTTP standard headers. Going forward if you want to tell a browser not to cache a file, you have to send the appropriate HTTP headers. If you want to do this in PHP you can use the
header
function to send the appropriate HTTP headers to the browser:If it has to be done via HTML you can do the following in your page header:
There is no way for you to be sure if the browser will honor your request that it not cache a page, however. There are some other things like eTags and whatnot but frankly I don't think that's going to help you out if the page is already cached.
UPDATE
From the HTTP/1.1 specification on Response Cacheability:
也许 PHP 可用于向 javascript 调用添加时间戳。然后你可以在持续时间内运行它............例如:
Perhaps PHP could be used to add a timestamp to the javascript call. You could then run this for the duration...........For example:
这个答案可能过于简单化,但您始终可以将 GET 值附加到包含文件版本的文件末尾。现代浏览器通常将其视为新版本,并会忽略以前的缓存并获取最新的缓存。因此,当您希望缓存最新的代码时,您所要做的就是更新版本。
例如:
您甚至可以编写一个 PHP 脚本,当文件根据某些特定参数(例如“上次更新”)发生更改时,该脚本会自动为您执行此操作。
This answer may be oversimplified, but you could always append a GET value to the end of the file that contains a file version. Modern browsers usually take that as a new version and will ignore previous caches and fetch the newest one. So all you'd have to do is update the version when you want the most up-to-date code to be cached instead.
For example:
You could even go as far as writing a PHP script that automatically does this for you when a file changes based on some specific parameter like "last updated."
您可以更改文件名,例如 page_v2.html,这将使浏览器将该页面加载为新页面。
You can change the file name, for example page_v2.html which will make the browser load the page as a new page.