解决微信入口文件缓存问题

目前微信会对页面进行缓存,会导致代码在服务器更新后,用户无法及时加载到,因为访问的还是用户客户端缓存的数据。如果后端接口已改变,而前端调用页面还访问的以前的,会导致无法正常使用。

为了避免此问题,让用户看到最新的代码,需要禁止微信缓存入口文件,其他文件依然可以缓存。
注意:在修改nginx配置文件时,需要将编辑器设置为utf-8,否则重启nginx可能会报错 “unkonwn directive … ”之类的错误

以nginx配置为例:

location / {
  root  /usr/share/nginx/html;
  index index.html index.htm;
  try_files $uri $uri/ /index.html;
  # 禁用缓存
  add_header Last-Modified $date_gmt;
  add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
  if_modified_since off;
  expires off;
  etag off;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js)$ {
  root  /usr/share/nginx/html;
  access_log off;
  expires 30d;
}

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器