CodeGuide icon indicating copy to clipboard operation
CodeGuide copied to clipboard

关于vuepress页面缓存,Nginx配置记录📝

Open fuzhengwei opened this issue 4 years ago • 0 comments

背景:默认情况下使用nginx作为h5静态资源的服务器时,这些资源在浏览器端是会有缓存的,特别是微信浏览器中,缓存非常严重。

方案:所以项目开始部署阶段就需要配置好nginx禁止这些资源的缓存,nginx需要做如下配置

error_page 404 https://bugstack.cn;
location / {
    root   html;
    index  index.html;
    add_header Cache-Control no-cache;
}

补充:这个nginx修改后是不会立即影响到客户端的,如果某个客户端曾经打开过,那后续还是会优先从缓存中获取,除非刷新请求一次修改后的页面,再往后才生效。以上方式相当于所有的都会去掉缓存,单vue页面我们只需要去掉html的缓存即可,因为js等会自动加上hash,所以即便缓存也没关系,所以应该加个条件:

if ($request_filename ~* .*\.(?:htm|html))
{
    add_header Cache-Control "no-store, no-cache";
}

fuzhengwei avatar Feb 16 '22 23:02 fuzhengwei