lbk60ox 发表于 2024-7-27 02:01:04

页面性能优化办法有哪些?


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(点击</span><span style="color: black;">上方公众号</span><span style="color: black;">,可快速关注)</span></p><span style="color: black;">作者:浪里行舟</span><span style="color: black;">https://segmentfault.com/a/1190000016745587</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">引子</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">互联网有一项著名的8秒原则。用户在<span style="color: black;">拜访</span>Web网页时,<span style="color: black;">倘若</span>时间超过8秒就会感到不耐烦,<span style="color: black;">倘若</span>加载需要太<span style="color: black;">长期</span>,<span style="color: black;">她们</span>就会放弃<span style="color: black;">拜访</span>。大部分用户<span style="color: black;">期盼</span>网页能在2秒之内就完成加载。事实上,加载时间每多1秒,你就会流失7%的用户。8秒并不是准确的8秒钟,只是向网站<span style="color: black;">研发</span>者<span style="color: black;">显示</span>了加载时间的重要性。那<span style="color: black;">咱们</span><span style="color: black;">怎样</span>优化页面性能,<span style="color: black;">加强</span>页面加载速度呢?这是本文<span style="color: black;">重点</span>要探讨的问题,然而性能优化是个综合性问题,<span style="color: black;">无</span>标准答案,想要面面俱到<span style="color: black;">陈列</span>出来,并非易事。本文只关注<span style="color: black;">有些</span>核心要点,以下是我总结性能优化<span style="color: black;">平常</span>的办法:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">1、</span>资源压缩与合并</span></strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">重点</span><span style="color: black;">包含</span>这些方面:html压缩、css 压缩、js的压缩和混乱和文件合并。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">资源压缩<span style="color: black;">能够</span>从文件中去掉多余的字符,<span style="color: black;">例如</span>回车、空格。你在编辑器中写代码的时候,会<span style="color: black;">运用</span>缩进和注释,这些<span style="color: black;">办法</span>无疑会让你的代码简洁<span style="color: black;">况且</span>易读,但它们<span style="color: black;">亦</span>会在文档中添加多余的字节。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">1.html压缩</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">html代码压缩<span style="color: black;">便是</span>压缩这些在文本文件中有<span style="color: black;">道理</span>,<span style="color: black;">然则</span>在HTML中不<span style="color: black;">表示</span>的字符,<span style="color: black;">包含</span>空格,制表符,换行符等,还有<span style="color: black;">有些</span>其他<span style="color: black;">道理</span>的字符,如HTML注释<span style="color: black;">亦</span><span style="color: black;">能够</span>被压缩。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">怎样</span>进行html压缩:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">运用</span>在线网站进行压缩(<span style="color: black;">研发</span>过程中<span style="color: black;">通常</span><span style="color: black;">不消</span>)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">nodejs <span style="color: black;">供给</span>了html-minifier工具</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">后端模板引擎渲染压缩</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.css代码压缩:</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">css代码压缩简单<span style="color: black;">来讲</span><span style="color: black;">便是</span>无效代码删除和css语义合并</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">怎样</span>进行css压缩:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">运用</span>在线网站进行压缩(<span style="color: black;">研发</span>过程中<span style="color: black;">通常</span><span style="color: black;">不消</span>)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">运用</span>html-minifier工具</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">运用</span>clean-css对css压缩</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/zPh0erYjkib1xgScnLPOGoXZfqsqGFCNhNhz9PpJxAgjv0NRqnjS93wWZiaytvGLRGnIRpwWVe3LRMiawvEP6XOyA/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">3.js的压缩和混乱</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">js的压缩和混乱<span style="color: black;">重点</span><span style="color: black;">包含</span>以下这几部分:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">无效字符的删除</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">剔除注释</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">代码语义的缩减和优化</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">代码<span style="color: black;">守护</span>(代码逻辑变得混乱,降低代码的可读性,这点很重要)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">怎样</span>进行js的压缩和混乱</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">运用</span>在线网站进行压缩(<span style="color: black;">研发</span>过程中<span style="color: black;">通常</span><span style="color: black;">不消</span>)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">运用</span>html-minifier工具</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">运用</span>uglifyjs2对js进行压缩</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">其实css压缩与js的压缩和混乱比html压缩收益要大得多,<span style="color: black;">同期</span>css代码和js代码比html代码多得多,<span style="color: black;">经过</span>css压缩和js压缩带来流量的减少,会非常<span style="color: black;">显著</span>。<span style="color: black;">因此</span>对大<span style="color: black;">机构</span><span style="color: black;">来讲</span>,html压缩可有可无,但css压缩与js的压缩和混乱必须要有!</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">4.文件合并</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/zPh0erYjkib1xgScnLPOGoXZfqsqGFCNhBd3vicrqY5iatzdZhibiczRMkMx4r0rwriaQkYdDTMsMGdPx0M4WcWB3MpA/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">从上图<span style="color: black;">能够</span>看出不合并请求有以下缺点:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">文件与文件之间有<span style="color: black;">插进</span>的上行请求,<span style="color: black;">增多</span>了N-1个网络延迟</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">受丢包问题影响更严重</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">keep-alive方式可能会<span style="color: black;">显现</span><span style="color: black;">情况</span>,经过代理服务器时可能会被断开,<span style="color: black;">亦</span><span style="color: black;">便是</span>说<span style="color: black;">不可</span><span style="color: black;">始终</span>保持keep-alive的状态</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">压缩合并css和js<span style="color: black;">能够</span>减少网站http请求的次数,但合并文件可能会带来问题:首屏渲染和缓存失效问题。那该<span style="color: black;">怎样</span>处理这问题呢?—-公共库合并和<span style="color: black;">区别</span>页面的合并。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">怎样</span>进行文件合并</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">运用</span>在线网站进行文件合并</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">运用</span>nodejs实现文件合并(gulp、fis3)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">2、</span>非核心代码异步加载异步加载的方式</span></strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">1、异步加载的方式</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">异步加载的三种方式——async和defer、动态脚本创建</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">① async方式</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">async属性是HTML5新增属性,需要Chrome、FireFox、IE9+浏览器支持</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">async属性规定一旦脚本可用,则会异步执行</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">async属性仅适用于<span style="color: black;">外边</span>脚本</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>是多个脚本,该<span style="color: black;">办法</span><span style="color: black;">不可</span><span style="color: black;">保准</span>脚本按<span style="color: black;">次序</span>执行</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">&lt;script </span><span style="color: black;">type</span><span style="color: black;">=</span><span style="color: black;">"text/javascript"</span><span style="color: black;">src</span><span style="color: black;">=</span><span style="color: black;">"xxx.js"</span><span style="color: black;">async</span><span style="color: black;">=</span><span style="color: black;">"async"</span><span style="color: black;">&gt;</span><span style="color: black;">&lt;/script&gt;</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">② defer方式</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">兼容所有浏览器</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">defer属性规定<span style="color: black;">是不是</span>对脚本执行进行延迟,直到页面加载为止</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>是多个脚本,该<span style="color: black;">办法</span><span style="color: black;">能够</span><span style="color: black;">保证</span>所有设置了defer属性的脚本按<span style="color: black;">次序</span>执行</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>脚本不会改变文档的内容,可将defer属性加入到script标签中,以便加快处理文档的速度</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">③动态创建script标签</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在还没定义defer和async前,异步加载的方式是动态创建script,<span style="color: black;">经过</span>window.onload<span style="color: black;">办法</span><span style="color: black;">保证</span>页面加载完毕再将script标签<span style="color: black;">插进</span>到DOM中,<span style="color: black;">详细</span>代码如下:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">function</span><span style="color: black;">addScriptTag</span><span style="color: black;">(</span><span style="color: black;">src</span><span style="color: black;">){</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">var</span><span style="color: black;">script</span><span style="color: black;"> = </span><span style="color: black;">document</span><span style="color: black;">.</span><span style="color: black;">createElement</span><span style="color: black;">(</span><span style="color: black;">script</span><span style="color: black;">);</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">script</span><span style="color: black;">.</span><span style="color: black;">setAttribute</span><span style="color: black;">(</span><span style="color: black;">"type"</span><span style="color: black;">,</span><span style="color: black;">"text/javascript"</span><span style="color: black;">);</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">script</span><span style="color: black;">.</span><span style="color: black;">src</span><span style="color: black;"> = </span><span style="color: black;">src</span><span style="color: black;">;</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">document</span><span style="color: black;">.</span><span style="color: black;">body</span><span style="color: black;">.</span><span style="color: black;">appendChild</span><span style="color: black;">(</span><span style="color: black;">script</span><span style="color: black;">);</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">}</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">window</span><span style="color: black;">.</span><span style="color: black;">onload</span><span style="color: black;"> = </span><span style="color: black;">function</span><span style="color: black;">(){</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">addScriptTag</span><span style="color: black;">(</span><span style="color: black;">"js/index.js"</span><span style="color: black;">);</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">}</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2、异步加载的区别</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1)defer是在HTML解析完之后才会执行,<span style="color: black;">倘若</span>是多个,<span style="color: black;">根据</span>加载的<span style="color: black;">次序</span>依次执行</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2)async是在加载完之后立即执行,<span style="color: black;">倘若</span>是多个,执行<span style="color: black;">次序</span>和加载<span style="color: black;">次序</span>无关</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">async和defer</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">其中蓝色线<span style="color: black;">表率</span>网络读取,红色线<span style="color: black;">表率</span>执行时间,这俩都是针对脚本的;绿色线<span style="color: black;">表率</span> HTML 解析。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">3、</span>利用浏览器缓存</span></strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">针对</span>web应用<span style="color: black;">来讲</span>,缓存是<span style="color: black;">提高</span>页面性能<span style="color: black;">同期</span>减少服务器压力的利器。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">浏览器缓存类型</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1.强缓存:不会向服务器发送请求,直接从缓存中读取资源,在chrome<span style="color: black;">掌控</span>台的network选项中<span style="color: black;">能够</span>看到该请求返回200的状态码,并且size<span style="color: black;">表示</span>from disk cache或from memory cache;</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">关联</span>的header:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Expires :response header里的过期时间,浏览器再次加载资源时,<span style="color: black;">倘若</span>在这个过期时间内,则命中强缓存。它的值为一个绝对时间的GMT格式的时间字符串, <span style="color: black;">例如</span>Expires:Thu,21 Jan 2018 23:39:02 GMT</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Cache-Control :这是一个相对时间,在配置缓存的时候,以秒为单位,用数值<span style="color: black;">暗示</span>。当值设为max-age=300时,则<span style="color: black;">表率</span>在这个请求正确返回时间(浏览器<span style="color: black;">亦</span>会记录下来)的5分钟内再次加载资源,就会命中强缓存。<span style="color: black;">例如</span>Cache-Control:max-age=300,</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">简单概括:其实这两者差别不大,区别就在于 Expires 是http1.0的产物,Cache-Control是http1.1的产物,两者<span style="color: black;">同期</span>存在的话,Cache-Control优先级高于Expires;在某些不支持HTTP1.1的环境下,Expires就会发挥用处。<span style="color: black;">因此</span>Expires其实是过时的产物,现<span style="color: black;">周期</span>它的存在只是一种兼容性的写法。强缓存判断<span style="color: black;">是不是</span>缓存的依据来自于<span style="color: black;">是不是</span>超出某个时间<span style="color: black;">或</span>某个时间段,而不关心服务器端文件<span style="color: black;">是不是</span><span style="color: black;">已然</span>更新,这可能会<span style="color: black;">引起</span>加载文件不是服务器端最新的内容,那<span style="color: black;">咱们</span><span style="color: black;">怎样</span>获知服务器端内容较客户端<span style="color: black;">是不是</span>已经<span style="color: black;">出现</span>了更新呢?此时<span style="color: black;">咱们</span>需要协商缓存策略。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2.协商缓存:向服务器发送请求,服务器会<span style="color: black;">按照</span>这个请求的request header的<span style="color: black;">有些</span>参数来判断<span style="color: black;">是不是</span>命中协商缓存,<span style="color: black;">倘若</span>命中,则返回304状态码并带上新的response header<span style="color: black;">通告</span>浏览器从缓存中读取资源;<span style="color: black;">另一</span>协商缓存需要与cache-control共同<span style="color: black;">运用</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">关联</span>的header:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">①Last-Modified和If-Modified-Since:当<span style="color: black;">第1</span>次请求资源时,服务器将资源传递给客户端时,会将资源最后更改的时间以“Last-Modified: GMT”的形式加在实体首部上<span style="color: black;">一块</span>返回给客户端。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Last-Modified: Fri, 22 Jul 2016 01:47:00 GMT</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">客户端会为资源标记上该信息,下次再次请求时,会把该信息附带在请求报文中一并带给服务器去做<span style="color: black;">检测</span>,若传递的时间值与服务器上该资源<span style="color: black;">最后</span>修改时间是一致的,则说明该资源<span style="color: black;">无</span>被修改过,直接返回304状态码,内容为空,<span style="color: black;">这般</span>就节省了传输数据量 。<span style="color: black;">倘若</span>两个时间不一致,则服务器会发回该资源并返回200状态码,和<span style="color: black;">第1</span>次请求时类似。<span style="color: black;">这般</span><span style="color: black;">保准</span>不向客户端重复发出资源,<span style="color: black;">亦</span><span style="color: black;">保准</span>当服务器有变化时,客户端能够得到最新的资源。一个304响应比一个静态资源<span style="color: black;">一般</span>小得多,<span style="color: black;">这般</span>就节省了网络带宽。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/zPh0erYjkib1xgScnLPOGoXZfqsqGFCNhomVyXrZaBHmfXQfS89lSBFBLia4JkMLmWs9185ZVMD1WaKXu1hIUcLg/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">但last-modified 存在<span style="color: black;">有些</span>缺点:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Ⅰ.某些服务端<span style="color: black;">不可</span>获取精确的修改时间</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Ⅱ.文件修改时间改了,但文件内容却<span style="color: black;">无</span>变</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">既然<span style="color: black;">按照</span>文件修改时间来决定<span style="color: black;">是不是</span>缓存尚有不足,能否<span style="color: black;">能够</span>直接<span style="color: black;">按照</span>文件内容<span style="color: black;">是不是</span>修改来决定缓存策略?—-ETag和If-None-Match</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">②ETag和If-None-Match:Etag是上一次加载资源时,服务器返回的response header,是对该资源的一种<span style="color: black;">独一</span>标识,只要资源有变化,Etag就会重新生成。浏览器在下一次加载资源向服务器发送请求时,会将上一次返回的Etag值放到request header里的If-None-Match里,服务器只需要比较客户端传来的If-None-Match跟自己服务器上该资源的ETag<span style="color: black;">是不是</span>一致,就能很好地判断资源相对客户端而言<span style="color: black;">是不是</span>被修改过了。<span style="color: black;">倘若</span>服务器<span style="color: black;">发掘</span>ETag匹配不上,<span style="color: black;">那样</span>直接以常规GET 200回包形式将新的资源(当然<span style="color: black;">亦</span><span style="color: black;">包含</span>了新的ETag)发给客户端;<span style="color: black;">倘若</span>ETag是一致的,则直接返回304知会客户端直接<span style="color: black;">运用</span>本地缓存<span style="color: black;">就可</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/zPh0erYjkib1xgScnLPOGoXZfqsqGFCNhWeuBIXxH4wv4FZx25HIqKY6U6dEPDMFWWicZ7D88qRWk7QoJfUGx6rw/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">两者之间对比:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">首要</span>在精确度上,Etag要优于Last-Modified。Last-Modified的时间单位是秒,<span style="color: black;">倘若</span>某个文件在1秒内改变了多次,<span style="color: black;">那样</span><span style="color: black;">她们</span>的Last-Modified其实并<span style="color: black;">无</span><span style="color: black;">表现</span>出来修改,<span style="color: black;">然则</span>Etag每次都会改变<span style="color: black;">保证</span>了精度;<span style="color: black;">倘若</span>是负载<span style="color: black;">平衡</span>的服务器,各个服务器生成的Last-Modified<span style="color: black;">亦</span>有可能不一致。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">第二在性能上,Etag要逊于Last-Modified,毕竟Last-Modified只需要记录时间,而Etag需要服务器<span style="color: black;">经过</span>算法来计算出一个hash值。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">第三在优先级上,服务器校验优先<span style="color: black;">思虑</span>Etag</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">缓存的机制</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">强制缓存优先于协商缓存进行,若强制缓存(Expires和Cache-Control)生效则直接<span style="color: black;">运用</span>缓存,若不生效则进行协商缓存(Last-Modified / If-Modified-Since和Etag / If-None-Match),协商缓存由服务器决定<span style="color: black;">是不是</span><span style="color: black;">运用</span>缓存,若协商缓存失效,<span style="color: black;">那样</span><span style="color: black;">表率</span>该请求的缓存失效,重新获取请求结果,再存入浏览器缓存中;生效则返回304,继续<span style="color: black;">运用</span>缓存。<span style="color: black;">重点</span>过程如下:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/zPh0erYjkib1xgScnLPOGoXZfqsqGFCNh7PqQlvflXMk8Jsrn9wCuFsu2HPtJQPPtYfPZweAoNUxibGsupXyfEbg/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">用户<span style="color: black;">行径</span>对浏览器缓存的影响</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1.<span style="color: black;">位置</span>栏<span style="color: black;">拜访</span>,链接<span style="color: black;">转</span>是正常用户<span style="color: black;">行径</span>,将会触发浏览器缓存机制;</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2.F5刷新,浏览器会设置max-age=0,跳过强缓存判断,会进行协商缓存判断;</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3.ctrl+F5刷新,跳过强缓存和协商缓存,直接从服务器拉取资源。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>想<span style="color: black;">认识</span><span style="color: black;">更加多</span>缓存机制,请猛戳 深入理解浏览器的缓存机制</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">4、</span><span style="color: black;">运用</span>CDN</span></strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">大型Web应用对速度的追求并<span style="color: black;">无</span>止步于仅仅利用浏览器缓存,<span style="color: black;">由于</span>浏览器缓存始终只是为了<span style="color: black;">提高</span>二次<span style="color: black;">拜访</span>的速度,<span style="color: black;">针对</span>首次<span style="color: black;">拜访</span>的加速,<span style="color: black;">咱们</span>需要从网络层面进行优化,最<span style="color: black;">平常</span>的手段<span style="color: black;">便是</span>CDN(Content Delivery Network,内容分发网络)加速。<span style="color: black;">经过</span>将静态资源(例如javascript,css,<span style="color: black;">照片</span>等等)缓存到离用户很近的相同网络运营商的CDN节点上,<span style="color: black;">不仅</span>能<span style="color: black;">提高</span>用户的<span style="color: black;">拜访</span>速度,还能节省服务器的带宽消耗,降低负载。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/zPh0erYjkib1xgScnLPOGoXZfqsqGFCNhOvRy4xa1hJ29CRXibuTJUjxC0PQAHeVmg5qSjzbJr4V8ialysza1ABiaQ/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">CDN是怎么做到加速的呢?</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">其实这是CDN服务商在全国各个省份<span style="color: black;">安排</span>计算节点,CDN加速将网站的内容缓存在网络边缘,<span style="color: black;">区别</span>地区的用户就会<span style="color: black;">拜访</span>到离自己<span style="color: black;">近期</span>的相同网络线路上的CDN节点,当请求达到CDN节点后,节点会判断自己的内容缓存<span style="color: black;">是不是</span>有效,<span style="color: black;">倘若</span>有效,则立即响应缓存内容给用户,从而加快响应速度。<span style="color: black;">倘若</span>CDN节点的缓存失效,它会<span style="color: black;">按照</span>服务配置去<span style="color: black;">咱们</span>的内容源服务器获取最新的资源响应给用户,并将内容缓存下来以便响应给后续<span style="color: black;">拜访</span>的用户。<span style="color: black;">因此呢</span>,一个地区内只要有一个用户先加载资源,在CDN中<span style="color: black;">创立</span>了缓存,该地区的其他后续用户都能<span style="color: black;">因此呢</span>而受益。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">5、</span>预解析DNS</span></strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">资源预加载是另一个性能优化技术,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span>该技术来预先<span style="color: black;">通知</span>浏览器某些资源可能在将来会被<span style="color: black;">运用</span>到。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">经过</span> DNS 预解析来告诉浏览器<span style="color: black;">将来</span><span style="color: black;">咱们</span>可能从某个特定的 URL 获取资源,当浏览器真正<span style="color: black;">运用</span>到该域中的某个资源时就<span style="color: black;">能够</span>尽快地完成 DNS 解析。例如,<span style="color: black;">咱们</span>将来可从 example.com 获取<span style="color: black;">照片</span>或音频资源,<span style="color: black;">那样</span><span style="color: black;">能够</span>在文档顶部的 &lt;head&gt; 标签中加入以下内容:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">&lt;link rel="dns-prefetch" href="//example.com"&gt;</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">当<span style="color: black;">咱们</span>从该 URL 请求一个资源时,就<span style="color: black;">再也不</span>需要等待 DNS 的解析过程。该技术对<span style="color: black;">运用</span>第三方资源<span style="color: black;">尤其</span>有用。<span style="color: black;">经过</span>简单的一行代码就<span style="color: black;">能够</span><span style="color: black;">通知</span><span style="color: black;">哪些</span>兼容的浏览器进行 DNS 预解析,这<span style="color: black;">寓意</span>着当浏览器真正请求该域中的某个资源时,DNS 的解析就<span style="color: black;">已然</span>完<span style="color: black;">成为了</span>,从而节省了宝贵的时间。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">另一</span>需要<span style="color: black;">重视</span>的是,浏览器会对a标签的href自动启用DNS Prefetching,<span style="color: black;">因此</span>a标签里<span style="color: black;">包括</span>的域名不需要在head中手动设置link。<span style="color: black;">然则</span>在HTTPS下不起<span style="color: black;">功效</span>,需要meta来强制开启功能。这个限制的<span style="color: black;">原由</span>是防止窃听者<span style="color: black;">按照</span>DNS Prefetching推断<span style="color: black;">表示</span>在HTTPS页面中超链接的主机名。下面这句话<span style="color: black;">功效</span>是强制打开a标签域名解析</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">&lt;meta http-equiv="x-dns-prefetch-control" content="on"/&gt;</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【关于投稿】</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span><span style="color: black;">大众</span>有原创好文投稿,请直接给公号发送留言。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">①&nbsp;留言格式:</span><span style="color: black;">【投稿】+《&nbsp;<span style="color: black;">文案</span>标题》+&nbsp;<span style="color: black;">文案</span>链接</span><span style="color: black;">②&nbsp;示例:</span><span style="color: black;">【投稿】《不要自<span style="color: black;">叫作</span>是程序员,我十<span style="color: black;">数年</span>的&nbsp;IT&nbsp;职场总结》:http://blog.jobbole.com/94148/</span><span style="color: black;">③&nbsp;最后请附上您的个人简介哈~</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">觉得本文对你有<span style="color: black;">帮忙</span>?请分享给<span style="color: black;">更加多</span>人</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">关注「前端大全」,<span style="color: black;">提高</span>前端技能</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/zPh0erYjkib0lZCEKibSLcyLMVa3iaNzhWkSPnEBk28r5AAcL4fS03LQn1RWA5M58d7kvysRCibKpHibjs1szyRmnOQ/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></span></p>




nuanhome 发表于 2024-8-31 06:01:32

“BS”(鄙视的缩写)‌

听听海 发表于 2024-9-3 09:53:55

我完全赞同你的观点,思考很有深度。

nqkk58 发表于 2024-11-11 08:08:39

我深受你的启发,你的话语是我前进的动力。
页: [1]
查看完整版本: 页面性能优化办法有哪些?