网页加载速度优化

引言

因为最近工做需求,须要极大优化用户的加载速度。
下面我就从网络请求优化的方面进行一次总结吧。😀css

部分图片采用雪碧图

雪碧图也叫CSS精灵, 是一种CSS图像合成技术,将多张图片何为一张从而减小网络请求。


如b站的图标:
https://s1.hdslb.com/bfs/stat...
html

部分图片采用svg

SVG 是使用 XML 来描述二维图形和绘图程序的语言。


SVG 与 JPEG 和 GIF 图像比起来,尺寸更小,且可压缩性更强,加载速度更快。而且 SVG 可伸缩,图像可在任何的分辨率下被高质量地打印。


如这个loading图标:
服务器

<?xml version="1.0" encoding="utf-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="margin: auto; background: rgb(255, 255, 255); display: block; shape-rendering: auto;" width="200px" height="200px" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid">
<g transform="rotate(0 50 50)">
  <rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#fe718d">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.9166666666666666s" repeatCount="indefinite"></animate>
  </rect>
</g><g transform="rotate(30 50 50)">
  <rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#fe718d">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.8333333333333334s" repeatCount="indefinite"></animate>
  </rect>
</g><g transform="rotate(60 50 50)">
  <rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#fe718d">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.75s" repeatCount="indefinite"></animate>
  </rect>
</g><g transform="rotate(90 50 50)">
  <rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#fe718d">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.6666666666666666s" repeatCount="indefinite"></animate>
  </rect>
</g><g transform="rotate(120 50 50)">
  <rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#fe718d">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.5833333333333334s" repeatCount="indefinite"></animate>
  </rect>
</g><g transform="rotate(150 50 50)">
  <rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#fe718d">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.5s" repeatCount="indefinite"></animate>
  </rect>
</g><g transform="rotate(180 50 50)">
  <rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#fe718d">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.4166666666666667s" repeatCount="indefinite"></animate>
  </rect>
</g><g transform="rotate(210 50 50)">
  <rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#fe718d">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.3333333333333333s" repeatCount="indefinite"></animate>
  </rect>
</g><g transform="rotate(240 50 50)">
  <rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#fe718d">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.25s" repeatCount="indefinite"></animate>
  </rect>
</g><g transform="rotate(270 50 50)">
  <rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#fe718d">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.16666666666666666s" repeatCount="indefinite"></animate>
  </rect>
</g><g transform="rotate(300 50 50)">
  <rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#fe718d">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.08333333333333333s" repeatCount="indefinite"></animate>
  </rect>
</g><g transform="rotate(330 50 50)">
  <rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#fe718d">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="0s" repeatCount="indefinite"></animate>
  </rect>
</g>
<!-- [ldio] generated by https://loading.io/ --></svg>

部分图片转 base64 编码

Base64就是一种基于64个可打印字符来表示二进制数据的方法。


将部分图片转为 base64 编码可减小网络请求、图片服务器压力利于页面加载。网络

压缩 html、css、js 等静态资源

编写代码时咱们常使用缩进和换行,这样在无形中增长了代码文件体积。


在相同的网络状况洗,咱们的代码体积越小,用户在访问咱们的网页时加载速度就更快。


对于 html、css 在发布到线上版本前,咱们可将代码全部的缩进和换行删除,从而下降代码体积。如“知乎”:





对于 js 在发布到线上版本前,咱们可将代码全部的缩进和换行删除的同时,将全部变量命名和方法名替换为更简短的单词,从而下降代码体积。如“知乎”:


svg

CDN

CDN(Content Delivery Network)是指内容分发网络,也称为内容传送网络,这个概念始于1996年,是美国麻省理工学院的一个研究小组为改善互联网的服务质量而提出的。





咱们将html、css、js、图标、字体、视频、音乐等静态资源部署到cdn上的各个节点,可极大优化在不一样地区、不一样网络运营商用户的网络请求速度。性能

Gzip

HTTP 协议上的 Gzip 编码是一种用来改进 WEB 应用程序性能的技术。大流量的 WEB 站点经常使用 Gzip 压缩技术来让用户感觉更快的速度。


使用 Gzip 压缩可将文件大小压缩将近三分之二的大小:


字体


- RZeeY优化

相关文章
相关标签/搜索