网站更新缓存
如果您有一个正在运行的网站(使用 PHP、MySQL、JS、CSS)并说您更改了某些功能。确保当用户访问该网站时,更改的页面会被更新而不是从缓存加载的最佳方法是什么?
If you have a live website (using PHP,MySQL,JS,CSS) and say you change some functionality. What is the best way to make sure, that when a user visits the site, the changed pages are updated and not loaded from cache?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为此,就有了文件加载器。当加载css、js或任何其他文件时,必须设置缓存控制标头。您可以通过编辑 .htaccess 或使用 php 来完成此操作。
.htaccess 演示
PHP 演示:
For that reson there is file loaders made for. When you load css, js or any other file, you must set cache control header. You can do it by editing .htaccess or with php.
.htaccess demo
PHP demo:
通过 HTML,在 Head 部分放置...
Via HTML, in the Head section put...
如果每个浏览器符合标准,您无法真正控制每个浏览器的策略,但通常会发生的情况是浏览器会从每个 HTML 下载标头,然后再决定是否应从缓存加载或重新下载。
检查 RFC HTTP 1.1 标头字段 了解更多详细信息(特别是在缓存控制下) )
You can't really control the policy each browser, if it complies to the standards, but what commonly happens is the browser does download the header from each HTML before deciding whether or not it should be loaded from cache or re-downloaded.
Check RFC HTTP 1.1 Header Field for more details (under cache control in particular)
除非您有某种形式的缓存,否则用户将请求数据并检查数据是否已更新。通常,除非您有主动缓存,否则您的 PHP 不会被缓存。
您的 JS 和 CSS 必须通过 Apache(或您使用的任何 Web 服务器)下的标头进行设置。
Unless you have some form of caching the user will be requesting data and checking if it has been updated. Usually your PHP is not cached unless you have active caching.
Your JS and CSS would have to be set via HEADERS under say Apache (or whatever web server you utilize).
您可以使用 window.applicationCache.swapCache();和 window.location.reload();一旦 javascript 确认网站有更新版本,就强制重新加载页面。
更好的选择可能是以某种方式提示用户,让他们知道网站已更新。这样,页面就不会突然重新加载。
You can use the window.applicationCache.swapCache(); and window.location.reload(); to force reload the page once javascript confirms that there's an updated version of the site.
A better option may be to prompt the user in some way, letting them know that the site updated. That way, the page doesn't just suddenly reload.