美文网首页
vue、angular等index.html缓存问题

vue、angular等index.html缓存问题

作者: 拳拳 | 来源:发表于2021-07-29 17:08 被阅读0次

    本文引自

    https://github.com/ant-design/ant-design-pro/issues/1365

    浏览器中,默认会对 html css js 等静态文件、以及重定向进行缓存,如果在HEAD头中指定:

    <head>

    <meta http-equiv="Expires" content="0">

    <meta http-equiv="Pragma" content="no-cache">

    <meta http-equiv="Cache-control" content="no-cache">

    <meta http-equiv="Cache" content="no-cache">

    </head>

    浏览器不会缓存html,但是还是会对重定向缓存,并且这种方式并不规范,可能有的浏览器不支持。

    解决方案是:

    1) 对hash过的静态文件还是采用默认方式,客户端会缓存。

    2)对html文件,返回时增加头:Cache-Control,必须每次来服务端校验,根据etag返回200或者304

    对应的nginx配置如下:

    server{

        listen  80; #监听端口

        server_name example.com

        # 前端静态文件

        location ~* \.(gif|jpg|jpeg|png|css|js|ico|eot|otf|fon|font|ttf|ttc|woff|woff2)$ {

            root /var/www/example-fe/dist/;

        }

        # 前端html文件

        location / {

            # disable cache html

            add_header Cache-Control 'no-cache, must-revalidate, proxy-revalidate, max-age=0';

            root /var/www/example-fe/dist/;

            index index.html index.htm;

            try_files $uri /index.html;

        }

    }

    由于浏览器缓存静态文件的时间不可控,我们可以在nginx上自己配置expires 1M(1个月)

        # 前端静态文件

        location ~* \.(gif|jpg|jpeg|png|css|js|ico|eot|otf|fon|font|ttf|ttc|woff|woff2)$ {

            root /var/www/example-fe/dist/;

            expires 1M;

            add_header Cache-Control "public";

        }

    相关文章

      网友评论

          本文标题:vue、angular等index.html缓存问题

          本文链接:https://www.haomeiwen.com/subject/hwcfvltx.html