ruby-on-rails - 防止某些页面上的 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>
解决方案
尝试完整的标题集以确保您访问所有浏览器。
<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" />
这通常涉及跨浏览器兼容性的多个问题,包括移动设备。
推荐阅读
- flutter - 如何在 Widget initState() 中获取 Navigator 的上下文?
- c# - Buy X Pay for Y 算法
- perl - 我正在尝试通过使用字符串 MNPPORTIN,00:11:16 和常规 exp 和 grep 函数从数组中获取匹配的元素
- vb.net - Visual Basic - 自动化循环
- sparql - 优化 SPARQL 查询
- excel - 在 Excel 中对多行进行排序以将空列向右移动
- android - 设计跨平台应用程序的最佳方式
- python - 将 Python 脚本的输出链接到 C++ 程序的输入
- c - C语言推箱子游戏加载器的分段错误
- postgresql - 通过触发器或复制或外部数据包装器将数据从多个数据库插入到远程数据库表中