9q13nh 发表于 2024-10-9 00:42:22

优化网页速度的7种办法


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">来人人都是<span style="color: black;">制品</span>经理【起点学院】,BAT实战派<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>讲了面向对象编程的<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>这么不按规矩出牌,哈哈。</p><img src="https://p3-sign.toutiaoimg.com/3c2000c07dad50798e2~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728452642&amp;x-signature=X0Zp77BBTCJXMEPgA56IXrQNukc%3D" style="width: 50%; margin-bottom: 20px;">
    <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>优化它的加载速度,有时打开一个网页要等待10来秒<span style="color: black;">才可</span>加载完成,虽然网页内容很不错,<span style="color: black;">然则</span>给我的<span style="color: black;">第1</span>印象<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>简单粗暴,又<span style="color: black;">特别有</span>疗效的优化<span style="color: black;">办法</span>,我觉得有必要给各位<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>,<span style="color: black;">咱们</span>来看下网页的加载流程。打开一个网页,会先拉取一个html页面,<span style="color: black;">而后</span>浏览器解析了这个html页面后,会<span style="color: black;">按照</span>页面的内容,去拉取javascript、css和<span style="color: black;">照片</span>文件,<span style="color: black;">最后</span><span style="color: black;">按照</span>这些文件,将页面渲染出来。</p><img src="https://p3-sign.toutiaoimg.com/3c2000c07db2a28ea7b~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728452642&amp;x-signature=5sXwGzcL2YiPLqBJ83Fo%2FscSWMQ%3D" style="width: 50%; margin-bottom: 20px;">
    <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><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>就上去了。</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>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">1.优化<span style="color: black;">照片</span>资源的格式和<span style="color: black;">体积</span></h3>
    <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>单个的文件的<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>webp &gt; jpeg &gt; png &gt; bmp。<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>,一个196*196<span style="color: black;">体积</span>区域展示的<span style="color: black;">照片</span>,它的文件竟然达到了几兆,最后才<span style="color: black;">发掘</span>把1960*1960分辨率的原图拉下来了。</p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">2.开启网络压缩</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">大部分浏览器在发出请求时,会带上这个标记「Accept-Encoding: gzip, deflate」,<span style="color: black;">暗示</span>这个浏览器<span style="color: black;">能够</span>接受以gzip压缩方式传输数据,<span style="color: black;">倘若</span>你的网页服务器<span style="color: black;">亦</span>支持gzip压缩数据,<span style="color: black;">那样</span>数据以gzip方式传输时,会减少70~80%的流量。</p><img src="https://p3-sign.toutiaoimg.com/3c2000c07dd9f15cfa1~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728452642&amp;x-signature=MUAnLUtzP5sbbIqL761%2FQRJTwOg%3D" style="width: 50%; margin-bottom: 20px;">
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">3.<span style="color: black;">运用</span>浏览器缓存</h3>
    <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>把这些资源文件设置为可缓存的,<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>就大大加快了网页的加载速度。</p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">4.减少重定向请求</h3>
    <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>的页面,<span style="color: black;">例如</span>说在手机上<span style="color: black;">拜访</span><span style="color: black;">博客</span>,会从weibo.com重定向至weibo.cn,每一次重定向都会<span style="color: black;">引起</span>浏览器重新发起请求,延长加载时间。<span style="color: black;">针对</span>这种<span style="color: black;">状况</span>,应该尽可能<span style="color: black;">运用</span>响应式设计,一个weibo.com站点覆盖至所有终端。</p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">5.<span style="color: black;">运用</span>CDN存储静态资源</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">CDN是一种静态内容分发网络,它在<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服务器上拉取,<span style="color: black;">这般</span><span style="color: black;">能够</span><span style="color: black;">保准</span>他最快速的<span style="color: black;">得到</span>该资源。据砖家统计,网络资源中有70%的是静态资源。这就<span style="color: black;">寓意</span>着,有70%的内容产生后是不会变化,<span style="color: black;">那样</span>将它们<span style="color: black;">所有</span>放在CDN上面,<span style="color: black;">能够</span><span style="color: black;">提高</span>这70%的资源的下载速度。</p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">6.减少DNS<span style="color: black;">查找</span>次数</h3>
    <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>的<span style="color: black;">照片</span>挂在<span style="color: black;">区别</span>当域名下,<span style="color: black;">例如</span>说<span style="color: black;">照片</span>A挂在a.pm-teacher.com,<span style="color: black;">照片</span>B挂在b.pm-teacher.com。当一个网页<span style="color: black;">同期</span><span style="color: black;">运用</span><span style="color: black;">照片</span>A和<span style="color: black;">照片</span>B时,浏览器需要<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>放在一个域名下。</p><img src="https://p3-sign.toutiaoimg.com/3c2000c07df473d9e86~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728452642&amp;x-signature=dYO%2BHh9pM0czkrRwxD2%2FQ0ZeXoU%3D" style="width: 50%; margin-bottom: 20px;">
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">7.压缩css和js内容</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span>说的压缩和第2点并不重复,上面<span style="color: black;">说到</span>的压缩是不改变文件内容的压缩。而css和js中有<span style="color: black;">海量</span>的空格和变量命名(如hello="hello word";),<span style="color: black;">倘若</span>将这些空格去除,并用简单的字母来代换变量名(如a="hello word";),<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>对加快拉取速度是有<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>你有<span style="color: black;">无</span>看出来,上面<span style="color: black;">说到</span>的优化<span style="color: black;">方法</span>的核心就3点:减少请求数、减少资源<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>有做过类似的优化吧。</p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">#专栏作家#</h3>
    <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;">微X</span>公众号(pm_teacher),人人都是<span style="color: black;">制品</span>经理专栏作家。资深程序猿,专注客户端<span style="color: black;">研发</span>若干年,对前端、后台技术略懂,热衷于对新的科技<span style="color: black;">行业</span>的探索。</p>




页: [1]
查看完整版本: 优化网页速度的7种办法