部署后强制刷新缓存 [英] Force cache refresh after deployment

查看:90
本文介绍了部署后强制刷新缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您无法更改引用该文件的 URI(例如,无法添加时间戳参数),是否可以强制客户端的缓存重新加载 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)?

这是我的情况:

  • 为 1000 个用户部署的插件
  • 该插件加载 example.com/page.html 调用 script.js
  • 资源 URI example.com/page.html 无法更改(无插件更新)
  • page.html 已更改.我需要从用户的缓存中清除旧的 page.html,以便可以加载新的 page.html.
  • A plugin deployed to a 1000 users
  • That plugin loads example.com/page.html which calls on script.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 old page.html from users' cache so the new page.html can load.

有什么想法吗?访问?PHP API 那个旧的 &新的 page.html 调用?

Any ideas? Htaccess? The PHP API that the old & new page.html call on?

谢谢!

推荐答案

好吧,如果页面已经被浏览器缓存了,很难告诉它不要使用它的缓存版本,因为它可能不会在之前再次检查它确定它的缓存版本是陈旧的.您只需向所有用户发送一封蜗牛邮件,通知他们按 ctrl+f5 :)

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 :)

不过,浏览器可能至少会尝试一个 HEAD 请求来检查修改后的时间戳,然后才能提供其缓存版本.在这种情况下,以下内容将对您有所帮助.

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.

浏览器使用 HTTP 标准标头从您的网络服务器协商其内容.以后如果你想告诉浏览器不要缓存文件,你必须发送适当的 HTTP 标头.如果您想在 PHP 中执行此操作,您可以使用 header 函数将适当的 HTTP 标头发送到浏览器:

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:

header('Cache-Control: no-cache');
header('Pragma: no-cache');

如果必须通过 HTML 完成,您可以在页眉中执行以下操作:

If it has to be done via HTML you can do the following in your page header:

<meta http-equiv="Expires" content="Tue, 01 Jan 1995 12:12:12 GMT">
<meta http-equiv="Pragma" content="no-cache">

但是,您无法确定浏览器是否会满足您不缓存页面的请求.还有一些其他的东西,比如 eTags 等等,但坦率地说,如果页面已经被缓存,我认为这不会帮助你.

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.

更新

来自关于响应可缓存性:

如果既没有缓存验证器也没有明确的过期时间与响应相关联,我们不希望它被缓存,但是某些缓存可能会违反此期望(例如,当很少或没有可用的网络连接).

If there is neither a cache validator nor an explicit expiration time associated with a response, we do not expect it to be cached, but certain caches MAY violate this expectation (for example, when little or no network connectivity is available).

这篇关于部署后强制刷新缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆