4lqedz 发表于 2024-7-27 12:18:16

网站性能优化三大策略


    <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 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></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;">1. </span><span style="color: black;">尽可能减少要传输的数据量</span></span></strong></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 style="color: black;">首要</span>,删除所有未<span style="color: black;">运用</span>的部分,例如</span><span style="color: black;">JavaScript</span><span style="color: black;">中不可<span style="color: black;">拜访</span>的函数、带有永远不匹配任何元素的<span style="color: black;">选取</span>器的样式,以及永远<span style="color: black;">隐匿</span>在</span><span style="color: black;">CSS</span><span style="color: black;">中的</span><span style="color: black;">HTML</span><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><span style="color: black;">JS</span><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><span style="color: black;">GZIP</span><span style="color: black;">(大<span style="color: black;">都数</span>浏览器都能理解)。最后,还有缓存。当浏览器<span style="color: black;">第1</span>次呈现页面时,这不会有什么帮助,但会在以后的<span style="color: black;">拜访</span>中节省<span style="color: black;">非常多</span>。但关键是要记住两件事:</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 style="color: black;">倘若</span><span style="color: black;">运用</span></span><span style="color: black;">CDN</span><span style="color: black;">,请<span style="color: black;">保证</span>支持缓存并在其中正确设置。</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 style="color: black;">期盼</span>有一种<span style="color: black;">办法</span><span style="color: black;">能够</span>从您的<span style="color: black;">方向</span>更早地更新它。将文件的</span>“指纹”嵌入到<span style="color: black;">URL</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 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>“<span style="color: black;">private</span><span style="color: black;">”指令防止</span><span style="color: black;">CDN</span><span style="color: black;">缓存私有数据。<span style="color: black;">亦</span><span style="color: black;">能够</span>优化</span><span style="color: black;">web</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;">2. </span><span style="color: black;">减少关键资源的总数</span></span></strong></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 style="color: black;">因此呢</span>,<span style="color: black;">咱们</span><span style="color: black;">能够</span>跳过流程中未直接<span style="color: black;">触及</span>的所有样式,还有所有的脚本。</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;"><span style="color: black;"><span style="color: black;">为了告诉浏览器不需要特定的</span><span style="color: black;">CSS</span><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 style="color: black;">同期</span>降低所有其他样式表的优先级(它们将被处理,但不会<span style="color: black;">做为</span>关键渲染路径的一部分)。例如,<span style="color: black;">倘若</span>向引用打印页面样式的样式标记添加</span><span style="color: black;">media=</span><span style="color: black;">“</span><span style="color: black;">print</span><span style="color: black;">”属性,则当介质未打印时(即在浏览器中<span style="color: black;">表示</span>页面时),这些样式不会干扰关键呈现路径。</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 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></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/669a4ae6ad244b6da984383b2db1460a~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1722610565&amp;x-signature=Vxm10RuYJfKv%2FUBl0VwUw%2FpXkCo%3D" style="width: 50%; margin-bottom: 20px;"></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;"><span style="color: black;"><span style="color: black;">如上所述,脚本是解析器阻塞的,<span style="color: black;">由于</span>它们<span style="color: black;">能够</span>改变</span><span style="color: black;">DOM</span><span style="color: black;">和</span><span style="color: black;">CSSOM</span><span style="color: black;">。<span style="color: black;">因此呢</span>,不改变它们的脚本<span style="color: black;">不该</span>该是块解析,从而节省<span style="color: black;">咱们</span>的时间。为了实现这一点,所有脚本标记都必须标记为</span><span style="color: black;">async</span><span style="color: black;">或</span><span style="color: black;">defer</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;">async</span><span style="color: black;">的脚本不会阻止</span><span style="color: black;">DOM</span><span style="color: black;">构造或</span><span style="color: black;">CSSOM</span><span style="color: black;">,<span style="color: black;">由于</span>它们<span style="color: black;">能够</span>在构建</span><span style="color: black;">CSSOM</span><span style="color: black;">之前执行。<span style="color: black;">然则</span>请记住,内联脚本无论<span style="color: black;">怎样</span>都会阻止</span><span style="color: black;">CSSOM</span><span style="color: black;">,除非您将它们放在</span><span style="color: black;">CSS</span><span style="color: black;">之上。相比之下,标记为“延迟”的脚本将在页面加载结束时进行<span style="color: black;">评定</span>。<span style="color: black;">因此呢</span>,它们<span style="color: black;">不该</span>影响文档(否则将触发重新呈现)。</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 style="color: black;">运用</span></span><span style="color: black;">defer</span><span style="color: black;">,脚本直到页面加载事件触发后才执行,而</span><span style="color: black;">async</span><span style="color: black;"><span style="color: black;">准许</span>脚本在解析文档时在后台运行。</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;">3. </span><span style="color: black;">缩短关键渲染路径长度</span></span></strong></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;">CRP</span><span style="color: black;">长度应缩短至可能的最小值。在某种程度上,<span style="color: black;">以上</span><span style="color: black;">办法</span><span style="color: black;">能够</span>做到这一点。</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 style="color: black;">做为</span>样式标记属性的<span style="color: black;">媒介</span><span style="color: black;">查找</span>将减少必须下载的资源总数。脚本标记属性</span><span style="color: black;">defer</span><span style="color: black;">和</span><span style="color: black;">async</span><span style="color: black;">将防止相应的脚本阻止解析。<span style="color: black;">运用</span></span><span style="color: black;">GZIP</span><span style="color: black;">缩小、压缩和归档资源将减少传输数据的<span style="color: black;">体积</span>(从而减<span style="color: black;">少许</span>据传输时间)。内联某些样式和脚本<span style="color: black;">能够</span>减少浏览器和服务器之间的往返次数。</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 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><span style="color: black;">ATF</span><span style="color: black;">内容,</span><span style="color: black;">ATF</span><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></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 style="color: black;">包括</span>了网站响应的所有方面,如缓存、设置</span><span style="color: black;">CDN</span><span style="color: black;">、重构、资源优化等,但所有这些都<span style="color: black;">能够</span>逐步完成。<span style="color: black;">做为</span>一名</span><span style="color: black;">web</span><span style="color: black;"><span style="color: black;">研发</span>人员,您应该将本文<span style="color: black;">做为</span>参考,并始终记住在实验前后<span style="color: black;">测绘</span>性能。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">认识</span><span style="color: black;">更加多</span></a></p>




b1gc8v 发表于 2024-9-26 05:49:40

真情实感,其含义为认真了、走心了的意思,是如今的饭圈常用语。

wrjc1hod 发表于 2024-10-20 06:39:48

期待你更多的精彩评论,一起交流学习。

m5k1umn 发表于 2024-10-24 09:28:18

对于这个问题,我有不同的看法...

7wu1wm0 发表于 2024-10-31 01:07:05

我完全同意你的看法,期待我们能深入探讨这个问题。

1fy07h 发表于 2024-11-6 23:33:24

真情实感,其含义为认真了、走心了的意思,是如今的饭圈常用语。
页: [1]
查看完整版本: 网站性能优化三大策略