当前位置:网站首页>【网页性能优化】SPA(单页面应用)首屏加载速度慢怎么办?
【网页性能优化】SPA(单页面应用)首屏加载速度慢怎么办?
2022-07-25 18:14:00 【樱丸小桃子啊】
提示:以下是本篇文章正文内容,下面案例可供参考
一、什么是首屏加载
首屏时间(First Contentful Paint),指的是浏览器从响应用户输入网址地址,到首屏内容渲染完成的时间,此时整个网页不一定要全部渲染完成,但需要展示当前视窗需要的内容
首屏加载可以说是用户体验中最重要的环节
关于计算首屏时间
利用 performance.timing 提供的数据:
通过 DOMContentLoad 或者 performance 来计算出首屏时间
// 方案一:
document.addEventListener('DOMContentLoaded', (event) => {
console.log('first contentful painting');
});
// 方案二:
performance.getEntriesByName("first-contentful-paint")[0].startTime
// performance.getEntriesByName("first-contentful-paint")[0]
// 会返回一个 PerformancePaintTiming的实例,结构如下:
{
name: "first-contentful-paint",
entryType: "paint",
startTime: 507.80000002123415,
duration: 0,
};
二、加载慢的原因
在页面渲染的过程中,导致加载速度慢的因素可能如下:
- 网络延时问题
- 资源文件体积是否过大
- 资源是否重复发送请求去加载了
- 加载脚本的时候,渲染内容堵塞了
三、解决方案
常见的几种SPA首屏优化方式
- 减小入口文件体积
- 静态资源本地缓存
UI框架按需加载- 图片资源的压缩
- 组件重复打包
- 开启
GZip压缩 - 使用
SSR
减小入口文件体积
常用的手段是路由懒加载,把不同路由对应的组件分割成不同的代码块,待路由被请求的时候会单独打包路由,使得入口文件变小,加载速度大大增加
在 vue-router 配置路由的时候,采用动态加载路由的形式
routes:[
path: 'Blogs',
name: 'ShowBlogs',
component: () => import('./components/ShowBlogs.vue')
]
以函数的形式加载路由,这样就可以把各自的路由文件分别打包,只有在解析给定的路由时,才会加载路由组件
静态资源本地缓存
后端返回资源问题:
- 采用
HTTP缓存,设置Cache-Control,Last-Modified,Etag等响应头 - 采用
Service Worker离线缓存
前端合理利用localAtorage
UI框架按需加载
在日常使用UI框架,例如 element-UI、或者 antd,我们经常性直接引用整个 UI 库
import ElementUI from 'element-ui'
Vue.use(ElementUI)
但实际上如果用到的组件只有按钮,分页,表格,输入与警告的话 我们可以按需引入
import {
Button, Input, Pagination, Table, TableColumn, MessageBox } from 'element-ui';
Vue.use(Button)
Vue.use(Input)
Vue.use(Pagination)
组件重复打包
假设 A.js 文件是一个常用的库,现在有多个路由使用了 A.js 文件,这就造成了重复文件下载
解决方案:在 webpack 的 config 文件中,修改 CommonsChunkPlugin 的配置
minChunks: 3
minChunks 为3表示会把使用3次及以上的包抽离出来,放进公共依赖文件,避免了重复加载组件
图片资源的压缩
图片资源虽然不在编码过程中,但它却是对页面性能影响最大的因素
对于所有的图片资源,我们可以进行适当的压缩
对页面上使用到的 icon ,可以使用在线字体图标,或者雪碧图,将众多小图标合并到同一张图上,用以减轻 http 请求压力。
开启GZip压缩
拆完包之后,我们再用 gzip 做一下压缩 安装 compression-webpack-plugin
cnmp i compression-webpack-plugin -D
在 vue.congig.js 中引入并修改 webpack 配置
const CompressionPlugin = require('compression-webpack-plugin')
configureWebpack: (config) => {
if (process.env.NODE_ENV === 'production') {
// 为生产环境修改配置...
config.mode = 'production'
return {
plugins: [new CompressionPlugin({
test: /\.js$|\.html$|\.css/, //匹配文件名
threshold: 10240, //对超过10k的数据进行压缩
deleteOriginalAssets: false //是否删除原文件
})]
}
}
在服务器我们也要做相应的配置,如果发送请求的浏览器支持 gzip ,就发送给它 gzip 格式的文件,我的服务器是用 express 框架搭建的,只要安装一下 compression 就能使用
const compression = require('compression')
app.use(compression()) // 在其他中间件使用之前调用
使用SSR
SSR(server side render),也就是服务端渲染,组件或页面通过服务器生成html字符串,再发送到浏览器从头搭建一个服务端渲染是很复杂的, vue 应用建议使用 Nuxt.js 实现服务端渲染
总结
减少首屏渲染时间的方法有很多,总的来讲可以分为两大部分:资源加载优化 和 页面渲染优化
大家可以根据自己项目的情况选择各种方式进行首屏渲染的优化
边栏推荐
- 如何将exe文件添加到开机启动
- Stm8s003f3 internal flash debugging
- Use of stm8s003f3 UART
- Landmark buildings around the world
- Auditing相关注解
- 文件基础知识
- SDLC software development life cycle and model
- Bl602 development environment setup
- C LINQ de Duplication & de duplication sum
- Boomi won the "best CEO in diversity" and the "best company in career growth" and ranked among the top 50 in the large company category
猜你喜欢

BiSeNet v1

What is the relationship between cloud fluidization and cloud desktop

Problems faced by cloud XR and main application scenarios of cloud XR

ORB_SLAM3复现——上篇

MySQL 索引优化全攻略

Leetcode 101. symmetric binary tree & 100. same tree & 572. Subtree of another tree

Auditing相关注解
SQL optimizer parsing | youth training camp notes

大话DevOps监控,团队如何选择监控工具?

"Digital security" alert NFT's seven Scams
随机推荐
11.1-CM24 最近公共祖先
更新|3DCAT实时云渲染 v2.1.2版本全新发布
Talking about Devops monitoring, how does the team choose monitoring tools?
How to read a Book
云VR:虚拟现实专业化的下一步
使用sqldeveloper连接mysql
BiSeNet v1
Tkinter GUI address book management system
MySQL lost the previous 0 after the decimal number type select
C语言 libcurl交叉编译
网易严选库存中心设计实践
SQL那些事
Mysql database common commands
How many points did NPDP pass? How to pass with high scores?
Oracle使用impdp导入报错:ORA-39001: 参数值无效 ORA-39000: 转储文件说明错误 ORA-39088: 文件名不能包含路径说明
nodejs 简单例子程序之express
Oracle import error: imp-00038: unable to convert to environment character set handle
Error when starting MySQL on Linux
srec_cat 常用参数的使用
The milestone progress has been made in the joint development of whole human GPCR antibody drugs by baicalto and liberothera