解决vue应用在微信端缓存严重的问题

由于vue在打包时会自动在更改过的js文件上加上hash过的后缀,所以js一般在上线后都会更新。但是index.html不会,由于index.html被缓存而饮用了老的js文件,如果这些老的文件在微信端被缓存那用用户登上去看的时候就不会发现有更新。为了让最新的应用对每个用户立即生效,要做的是 1. 马上丢弃原有缓存 2. 让html不缓存。在nginx上配置可以解决

    location / {
        try_files $uri $uri/ /index.html;
        index index.html;
        add_header Cache-Control "no-store,max-age=0";

    }


    location ~* .(html)$ {
          access_log off;
          add_header  Cache-Control  "no-store,max-age=0";
    }
    location ~* .(css|js|png|jpg|jpeg|gif|gz|svg|mp4|ogg|ogv|webm|htc|xml|woff)$ {
         access_log off;
         add_header Cache-Control max-age=604800;
    }

这里注意光加上no-store并不能完全解决问题,no-store表示response不缓存,但如果原先已经有缓存了那还是有问题,max-age=0表示让现有缓存立即失效

喜欢艺术的码农
原文地址:https://www.cnblogs.com/zjhgx/p/14639438.html