部署后强制刷新缓存

发布于 2024-12-27 18:44:25 字数 437 浏览 3 评论 0原文

如果您无法更改引用该文件的 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 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.

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

Thanks!

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

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

发布评论

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

评论(4

回心转意 2025-01-03 18:44:25

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

浏览器有可能至少会尝试使用 HEAD 请求来检查修改后的时间戳,然后再提供其服务不过,缓存版本。在这种情况下,以下内容将为您提供帮助。

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

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

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

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

:但是,您无法确定浏览器是否会满足您不缓存页面的请求。还有一些其他东西,例如电子标签之类的东西,但坦率地说,如果页面已经缓存,我认为这不会对您有帮助。


更新

来自 上的 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:

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

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">

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:

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

薄荷梦 2025-01-03 18:44:25

也许 PHP 可用于向 javascript 调用添加时间戳。然后你可以在持续时间内运行它............例如:

check_if_cache_should_be_disabled.php

<前><代码>


页面.php

Perhaps PHP could be used to add a timestamp to the javascript call. You could then run this for the duration...........For example:

check_if_cache_should_be_disabled.php

<?php
$deployment_flag = true; // Uncomment this if you want to disable normal cache.
//$deployment_flag = false // Uncomment this if you want to enable normal cache.
?>

page.php

<script src="/js/script.js<?php 
require('check_if_cache_should_be_disabled.php');
//  File Get Contents can be used for a remote server
//file_get_contents('http://yourserver.com/check_if_cache_should_be_disabled.php');
if ($deployment_flag == true) {
print ( '?ts='.date() );
}
?>"></script>
贪了杯 2025-01-03 18:44:25

这个答案可能过于简单化,但您始终可以将 GET 值附加到包含文件版本的文件末尾。现代浏览器通常将其视为新版本,并会忽略以前的缓存并获取最新的缓存。因此,当您希望缓存最新的代码时,您所要做的就是更新版本。

例如:

<script type="javascript" src="example.js?version=2.0.8"></script>

您甚至可以编写一个 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:

<script type="javascript" src="example.js?version=2.0.8"></script>

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."

美男兮 2025-01-03 18:44:25

您可以更改文件名,例如 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.

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