当前位置:网站首页>[web page performance optimization] what about the slow loading speed of the first screen of SPA (single page application)?
[web page performance optimization] what about the slow loading speed of the first screen of SPA (single page application)?
2022-07-25 18:21:00 【Cherry pill peach】
List of articles
Tips : The following is the main body of this article , The following cases can be used for reference
One 、 What is first screen loading
First screen time (First Contentful Paint), It refers to the web address entered by the browser from the response user , By the time the first screen content rendering is completed , At this time, the whole web page does not have to be completely rendered , But you need to show what the current window needs
The first screen loading can be said to be in the user experience above all Link
About calculating the first screen time
utilize performance.timing Data provided :
adopt DOMContentLoad perhaps performance To calculate the first screen time
// Scheme 1 :
document.addEventListener('DOMContentLoaded', (event) => {
console.log('first contentful painting');
});
// Option two :
performance.getEntriesByName("first-contentful-paint")[0].startTime
// performance.getEntriesByName("first-contentful-paint")[0]
// Will return a PerformancePaintTiming Example , The structure is as follows :
{
name: "first-contentful-paint",
entryType: "paint",
startTime: 507.80000002123415,
duration: 0,
};
Two 、 The reason for slow loading
In the process of page rendering , The factors leading to slow loading speed may be as follows :
- Network delay problem
- Whether the volume of the resource file is too large
- Whether the resource repeatedly sends requests to load
- When loading scripts , The rendering is blocked
3、 ... and 、 Solution
Common ones SPA First screen optimization mode
- Reduce the size of the entry file
- Static resource local cache
UIThe frame loads on demand- Compression of image resources
- Package components repeatedly
- Turn on
GZipCompress - Use
SSR
Reduce the size of the entry file
The common method is lazy loading , Divide the components corresponding to different routes into different code blocks , When a route is requested, it will be packaged separately , Make the entry file smaller , The loading speed is greatly increased
stay vue-router When configuring routing , It adopts the form of dynamically loading routes
routes:[
path: 'Blogs',
name: 'ShowBlogs',
component: () => import('./components/ShowBlogs.vue')
]
Load the route as a function , In this way, the respective routing files can be packaged separately , Only when parsing a given route , Will load the routing component
Static resource local cache
The back end returns the resource problem :
- use
HTTPcache , Set upCache-Control,Last-Modified,EtagEqual response head - use
Service WorkerOffline caching
Rational utilization of front endlocalAtorage
UI The frame loads on demand
In daily use UI frame , for example element-UI、 perhaps antd, We often refer directly to the whole UI library
import ElementUI from 'element-ui'
Vue.use(ElementUI)
But in fact, if the only component used is a button , Pagination , form , Input and warning words We can introduce it on demand
import {
Button, Input, Pagination, Table, TableColumn, MessageBox } from 'element-ui';
Vue.use(Button)
Vue.use(Input)
Vue.use(Pagination)
Package components repeatedly
hypothesis A.js File is a common library , Now there are multiple routes in use A.js file , This causes duplicate file downloads
Solution : stay webpack Of config In file , modify CommonsChunkPlugin Configuration of
minChunks: 3
minChunks by 3 It means that you will use 3 Pull out the bags for times and above , Put into the public dependency file , Avoid repeated loading of components
Compression of image resources
Although the picture resources are not in the encoding process , But it is the biggest factor that affects page performance
For all picture resources , We can compress properly
For the... Used on the page icon , You can use online font icons , Or sprite , Merge many small icons on the same diagram , To alleviate http Request pressure .
Turn on GZip Compress
After unpacking , We'll use gzip Do some compression install compression-webpack-plugin
cnmp i compression-webpack-plugin -D
stay vue.congig.js Introduce and modify webpack To configure
const CompressionPlugin = require('compression-webpack-plugin')
configureWebpack: (config) => {
if (process.env.NODE_ENV === 'production') {
// Modify the configuration for the production environment ...
config.mode = 'production'
return {
plugins: [new CompressionPlugin({
test: /\.js$|\.html$|\.css/, // Match file name
threshold: 10240, // For more than 10k The data is compressed
deleteOriginalAssets: false // Delete the original file or not
})]
}
}
We also need to make corresponding configuration on the server , If the browser that sent the request supports gzip , Just send it gzip File format , My server uses express It's framed , Just install it compression Can use
const compression = require('compression')
app.use(compression()) // It is called before other middleware is used
Use SSR
SSR(server side render), That is, server-side rendering , Components or pages are generated by the server html character string , Sending it to the browser to build a server-side rendering from scratch is very complicated , vue It is recommended to use Nuxt.js Implement server-side rendering
summary
There are many ways to reduce the first screen rendering time , Generally speaking, it can be divided into two parts : Resource loading optimization and Page rendering optimization
You can choose various ways to optimize the first screen rendering according to your project
边栏推荐
- [MySQL]数据库中的索引为什么是用B+树来实现? 哈希表/红黑树/B树是否可行呢?
- Conversion between integer and string in C language
- 文件基础知识
- Related operations of figure
- JZ71 跳台阶扩展问题
- Basic operation of bidirectional linked list
- [QNX Hypervisor 2.2用户手册]9.4 dryrun
- Use of C language cjson Library
- 【网页性能优化】SPA(单页面应用)首屏加载速度慢怎么办?
- 「行话」| 用DevOps高效交付游戏,是种什么体验?
猜你喜欢

乐观锁解析

网易严选库存中心设计实践

超全Mavan标签详解

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

【网页性能优化】SPA(单页面应用)首屏加载速度慢怎么办?

喜讯!瑞云科技被授予“海上扬帆”5G融合应用专委会成员单位

Li Kai: the interesting and cutting-edge audio and video industry has always attracted me

结合GHS MULTI使用瑞萨E1仿真器实现对瑞萨RH850单片机的仿真调试

tkinter GUI版通信录管理系统

7. 依赖注入
随机推荐
GAN的详细介绍及其应用(全面且完整)
uniapp滚动条置顶效果、自定义页面滚动条的位置(整理)
Unittest framework application
Oracle uses impdp import to report an error: ora-39001: invalid parameter value ora-39000: dump file description error ora-39088: file name cannot contain path description
大话DevOps监控,团队如何选择监控工具?
Could not stop Cortex-M device! please check the JTAG cable的解决办法
SQL那些事
Optimistic lock pessimistic lock applicable scenario
Auditing related notes
NC15 求二叉树的层序遍历
Bl602 development environment setup
工程师必看的示波器探头安全使用说明书
OV7725 yuv 640* [email protected] Profile
[MySQL]数据库中的索引为什么是用B+树来实现? 哈希表/红黑树/B树是否可行呢?
Pan domain name configuration method
一周活动速递|深入浅出第8期;Meetup成都站报名进行中
Tensor to img && imge to tensor (pytorch的tensor转换)
mysql的小数number类型select之后丢失了前面的0
php内存管理机制与垃圾回收机制
srec_cat 常用参数的使用