# html静态资源不缓存
html资源如果缓存的话,项目升级后如果有更新前端资源,浏览器需要主动清理缓存才能是升级后的前端介质生效。在 Nginx 上配置不缓存 HTML 文件即可解决此问题。
Nginx具体配置如下:
location / {
root ${userDir}${appDir};
# 配置页面不缓存html和htm结尾的文件
if ($request_filename ~* .*\.(?:htm|html)$)
{
add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
}
if ($request_filename ~* /remoteEntry\.js$)
{
add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
}
index index.html index.htm;
try_files $uri /index.html;
}
# 如何解决移动端钉钉资源更新缓存问题?
# 问题描述
AFCenter移动端集成钉钉后,当项目升级或修复前端BUG时,用户必须手动清理缓存才能获取最新资源。如何实现用户无感更新?
# 解决方案:
通过修改前端入口文件名称来强制资源刷新(缓存失效):
修改移动端入口文件 /mobile/index.html → 重命名为 index1.html 同步更新钉钉配置中的访问地址,指向 index1.html
# 后续每次更新时:
按序列递增文件名:index2.html → index3.html → ... 钉钉配置中同步修改对应文件名
# 实现原理
浏览器缓存通过URL识别资源,文件路径变更会被视为新资源请求,自动跳过旧缓存加载最新文件。