Vue首評加載速度及白屏時間優(yōu)化詳解
項目背景
測試環(huán)境的管理后臺應客戶需求最近加了點東西,加載的東西變多,使得整個項目變得有點臃腫了,首屏以及刷新后渲染速度變得極其緩慢。之前10s左右還能勉強接受,這次一下干到30s,整個人都崩潰了,花了點時間優(yōu)化了一下。

環(huán)境:vue:2.6.11,vue-cli:3.0
splitChunks
看到上面圖片里的文件其實并不大,最大的也就287k。
這也是優(yōu)化過的,之前都是有的最大為1m左右,在vue.config.js配置splitChunks,這樣針對的大文件都變小了,但是時間上并沒有提升多少。只不過是性能優(yōu)化了一點。
chainWebpack: config => {
if (process.env.NODE_ENV === "production") {
config.optimization.splitChunks({
chunks: "all",
maxInitialRequests: 4,
cacheGroups: {
default: {
name: "common",
minChunks: 5, // 模塊被引用2次以上的才抽離
priority: 1,
reuseExistingChunk: true, // 復用公共模塊
},
vendors: {
// 默認拆分node_modules
test: /[\\/]node_modules[\\/]/,
minChunks: 1, // 默認1
priority: 10,
name (module) {
// get the name. E.g. node_modules/packageName/not/this/part.js
// or node_modules/packageName
const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1]
// npm package names are URL-safe, but some servers don't like @ symbols
return `npm.${packageName.replace('@', '')}`
}
},
elementUI: {
name: "chunk-elementUI", // split elementUI into a single package
priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
test: /[\\/]node_modules[\\/]_?element-ui(.*)/, // in order to adapt to cnpm
},
echarts: {
name: "chunk-echarts", // split elementUI into a single package
priority: 30, // the weight needs to be larger than libs and app or it will be packaged into libs or app
test: /[\\/]node_modules[\\/]_?echarts(.*)/, // in order to adapt to cnpm
},
xlsx: {
name: "chunk-xlsx", // split elementUI into a single package
priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
test: /[\\/]node_modules[\\/]_?xlsx(.*)/, // in order to adapt to cnpm
},
editor: {
name: "chunk-editor", // split elementUI into a single package
priority: 50, // the weight needs to be larger than libs and app or it will be packaged into libs or app
test: /[\\/]node_modules[\\/]_?vue-json-editor(.*)/, // in order to adapt to cnpm
},
},
});
}
},CDN
通過上面的那步,文件變小了,但是對于一些文件來說echarts、XLSX、element-ui來說,還是加載有點久。配置CDN后,也就幾百毫秒久加載完成了。
先在index.html配置,順序不要變,vue在首先引入
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico" rel="external nofollow" >
<link rel="stylesheet" rel="external nofollow" >
<!-- 引入 element ui.css -->
<link rel="external nofollow" rel="stylesheet">
<script src="https://cdn.bootcss.com/vue/2.6.11/vue.min.js"></script>
<script src="https://cdn.bootcss.com/element-ui/2.15.7/index.js"></script>
</head>
<body>
<noscript>
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app">
</div>
<!-- built files will be auto injected -->
</body>
<script src="https://cdn.bootcss.com/echarts/3.7.0/echarts.min.js"></script>
<script src="https://unpkg.com/xlsx/dist/xlsx.full.min.js"></script>
</html>接下來在vue.config.js配置,element-ui上面要先把vue配置好
configureWebpack: (config) => {
return {
externals:{
'vue': "Vue",
'echarts': 'echarts',
'element-ui': 'ELEMENT',
XLSX: "XLSX",
},
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"windows.jQuery": "jquery",
}),
],
};
},然后把main.js引入的vue、elementui、echart、XLSX全部注釋掉
如果要用的話直接使用。例如:
//某些js文件引入elementui
ELEMENT.Message.error(error.message)
//echarts
this.myMiddleChart = echarts.init(this.$refs.myChart);//自己加的代碼
this.myMiddleChart.setOption(this.option)
this.lineChart = echarts.init(this.$refs.lineChart);//自己加的代碼
this.lineChart.setOption(this.lineOption)
let table = document.getElementById('table');
let worksheet = XLSX.utils.table_to_sheet(table);
let workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, 'sheet');
// 以上四行也可以直接一行搞定,如果不需要對表格數(shù)據(jù)進行修改的話
let workbooked = XLSX.utils.table_to_book(document.getElementById('table'))
XLSX.writeFile(workbooked, '報表.xlsx');GZIP
使用CDN引入,特定資源加載速度確實變快了,但是大多數(shù)文件加載還是比較慢的,加載資源也比較大。
在vue.config.js配置如下
configureWebpack: (config) => {
return {
externals:{
'vue': "Vue",
'echarts': 'echarts',
'element-ui': 'ELEMENT',
XLSX: "XLSX",
},
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"windows.jQuery": "jquery",
}),
new CompressionPlugin({
algorithm: "gzip", // 使用gzip壓縮
test: /\.js$|\.html$|\.css$/, // 匹配文件名
filename: "[path].gz[query]", // 壓縮后的文件名(保持原文件名,后綴加.gz)
minRatio: 0.8, // 壓縮率小于1才會壓縮
threshold: 10240, // 對超過10k的數(shù)據(jù)壓縮
deleteOriginalAssets: false, // 是否刪除未壓縮的源文件,謹慎設置,如果希望提供非gzip的資源,可不設置或者設置為false(比如刪除打包后的gz后還可以加載到原始資源文件)
}),
],
};
},然后去服務器上,找到nginx,對制定的服務開始gzip壓縮
# 開啟gzip
gzip on;
# 低于1kb的資源不壓縮
gzip_min_length 1k;
# 設置壓縮所需要的緩沖區(qū)大小
gzip_buffers 4 16k;
# 壓縮級別【1-9】,越大壓縮率越高,同時消耗cpu資源也越多,建議設置在4左右。
gzip_comp_level 4;
# 需要壓縮哪些響應類型的資源,缺少自己補。
gzip_types text/plain application/javascript application/x-javascript text/css application/xml;
# 配置禁用gzip條件,支持正則。此處表示ie6及以下不啟用gzip(因為ie低版本不支持)
gzip_disable "MSIE [1-6]\.";
# 是否添加“Vary: Accept-Encoding”響應頭,
gzip_vary on;

刪除文件預加載
以上搞完之后,首屏加載速度確實有提升。
但是頁面刷新還是有點慢,mac和windows加載速度不一致,mac更慢一點。用lighthouse跑了一下,還是不行。速度的話windows10s,mac19s,還是不行。加載的空白時間還是過長。
因為 vuecli 3默認開啟 prefetch(預先加載模塊),提前獲取用戶未來可能會訪問的內(nèi)容
我看了一下,我每次刷新后回預加載11個路由,這的確很慢。體驗沒有那么好。于是我把這個預加載給刪了
chainWebpack: config => {
//刪除預加載
config.plugins.delete('prefetch')
if (process.env.NODE_ENV === "production") {
config.optimization.splitChunks({
chunks: "all",
maxInitialRequests: 4,
cacheGroups: {
default: {
name: "common",
minChunks: 5, // 模塊被引用2次以上的才抽離
priority: 1,
reuseExistingChunk: true, // 復用公共模塊
},
vendors: {
// 默認拆分node_modules
test: /[\\/]node_modules[\\/]/,
minChunks: 1, // 默認1
priority: 10,
name (module) {
// get the name. E.g. node_modules/packageName/not/this/part.js
// or node_modules/packageName
const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1]
// npm package names are URL-safe, but some servers don't like @ symbols
return `npm.${packageName.replace('@', '')}`
}
},
});
}
},這樣整體的加載速度,提升到3s,最快的時候1.7s就可以加載完成。

Lighthouse的分數(shù)也跑到66分,也算及格了,最開始只有33。

家里的網(wǎng)跑到75,絕絕子

到此這篇關于Vue首評加載速度及白屏時間優(yōu)化詳解的文章就介紹到這了,更多相關Vue加載優(yōu)化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue+better-scroll 實現(xiàn)通訊錄字母索引的示例代碼
通訊錄字母索引是常用的一種功能,本文主要介紹了Vue+better-scroll 實現(xiàn)通訊錄字母索引,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06
vant使用datetime-picker組件設置maxDate和minDate的坑及解決
這篇文章主要介紹了vant使用datetime-picker組件設置maxDate和minDate的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
Vue+Jwt+SpringBoot+Ldap完成登錄認證的示例代碼
本篇文章主要介紹了Vue+Jwt+SpringBoot+Ldap完成登錄認證的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05

