vue项目上线后服务器资源更新而浏览器有缓存不更新

原因

浏览器的缓存机制(分为强缓存和协商缓存)
强缓存:即不向后端发送请求,直接从缓存中读取数据
协商缓存:向后端发起请求,看服务器资源是否更新,如果没有更新就返回304,如果更新了就返回200

解决方法:
/etc/nginx/conf.d/default.conf在服务器的这个文件夹找到配置文件,并下载下来修改,下载下来之后请先复制一份这个配置文件,避免修改出错导致服务器不能正常使用
1.nginx 配置,在在nginx.conf文件做设置,让 index.html 不缓存

location = /index.html {
	root	 /usr/share/nginx/html;
    add_header Cache-Control "no-cache, no-store";
}

或者直接在 location / 这里增加
add_header Cache-Control "no-cache, no-store";

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        add_header Cache-Control "no-cache, no-store";
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

no-cache, no-store可以只设置一个
no-cache浏览器会缓存,但刷新页面或者重新打开时 会请求服务器,服务器可以响应304,如果文件有改动就会响应200
no-store浏览器不缓存,刷新页面需要重新下载页面。
推荐两个都设置设置

关于浏览器缓存原因及解决方法相关连接
2.在 html 页面前面加 meta 标签。

<meta http-equiv="pragram" content="no-cache">
<meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate">
<meta name="viewport" content="width=device-width,initial-scale=1.0">

相关参考连接

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js是一个JavaScript框架,用于构建用户界面。在使用Vue.js开发网站时,由于浏览器缓存的存在,可能会导致版本更新的问题。 浏览器缓存是指浏览器在第一次请求网页后,会将该网页的一些资源文件(例如JavaScript和CSS文件)保存在本地。当用户再次访问相同的网页时,浏览器会直接从本地缓存中加载这些资源文件,以提高网页的加载速度。 然而,当使用Vue.js进行版本更新时,如果浏览器继续使用缓存的旧版本文件,就会导致网页显示异常或出现错误。为了解决这个问题,可以采取以下几种方法: 1. 使用文件版本号:在引入CSS和JavaScript文件时,可以在文件名中添加一个版本号,例如app.js?v=1.0。每次更新版本时,只需修改版本号即可,这样浏览器会认为是不同的文件,强制重新加载。 2. 添加Cache-Control头信息:通过在服务器端的响应头信息中添加Cache-Control字段,指定浏览器是否应该缓存文件,以及缓存的有效期。通过设置"no-cache"或"no-store"可以禁止浏览器缓存文件。 3. 使用Webpack的chunkhash:在使用Webpack打包项目时,可以使用chunkhash生成唯一的文件名,例如app.f8293f3.js。每次更新时,Webpack会自动生成新的chunkhash,从而强制浏览器重新加载文件。 除了以上方法,还可以使用一些高级技术,如Service Worker来实现离线缓存更新策略,或使用CDN来分发文件,以避免缓存问题。 总而言之,更新Vue.js版本时,需要考虑到浏览器缓存的存在。通过使用文件版本号、设置Cache-Control头信息或使用Webpack的chunkhash等方法,可以解决浏览器缓存问题,确保网页能够正确加载最新的文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值