首页 > 解决方案 > 防止某些页面上的 HTML 缓存

问题描述

我试图只缓存我的资产,而不是我网站上几个页面的实际 HTML 页面。

目前我的缓存控制标头设置为:

cache-control: max-age=0, no-cache, must-revalidate

我的 .htaccess 文件

# 1 Day for most static assets
<filesMatch ".(css|jpg|jpeg|png|gif|js|ico|JPG|woff)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>

在移动设备上,当用户打开我的网站浏览到给定页面,关闭他们的浏览器应用程序然后重新打开时,该页面将从缓存中加载。

当我设置以下标题时,移动浏览器会在重新打开浏览器时正确重新加载页面,但现在它会重新加载所有内容(javascript、css、图像等)。我只希望 html 重新加载。

response.headers["Cache-Control"] = "no-cache, no-store"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"

我怎样才能做到这一点?

提前致谢。

更新了 .htaccess 文件

<IffilesMatch ".(css|jpg|jpeg|png|gif|js|ico|JPG|woff)$">
Header set Cache-Control "max-age=2592000, public"
</If>
<Else>
Header set Pragma "no-cache"
Header set Expires = "0"
Header set Expires = "Fri, 01 Jan 1990 00:00:00 GMT"
Header set Cache-Control = "no-cache, no-store"
</Else>

标签: ruby-on-railsrubyhtmlbrowser-cache

解决方案


尝试完整的标题集以确保您访问所有浏览器。

<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />

这通常涉及跨浏览器兼容性的多个问题,包括移动设备。


推荐阅读