3

Vue项目首屏打开速度慢的解决方

 2 years ago
source link: https://www.huhexian.com/33367.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Vue项目首屏打开速度慢的解决方

青山 2022-03-2210:25:43评论845字

最近接手了一个后台管理系统,技术栈主要是vue全家桶+elementui,老大打开测试环境页面的时候,说看到首页需要6秒钟,那如何进行优化呢?

首先我们需要安装webpack-bundle-analyzer

  1. // webpack.prod.conf.js
  2. if (config.build.bundleAnalyzerReport) {
  3. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  4. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  5. // config/index.js
  6. build: {
  7. // Run the build command with an extra argument to
  8. // View the bundle analyzer report after build finishes:
  9. // `npm run build --report`
  10. // Set to `true` or `false` to always turn it on or off
  11. bundleAnalyzerReport: process.env.npm_config_report

运行npm run build --report

我们可以看到,vendor中的elementui占了500k,怎么优化呢?

在webpack配置文件中增加,接下来就是见证奇迹的时刻。

  1. externals: {
  2. 'vue': 'vue',
  3. 'element-ui': 'ELEMENT',
  4. 'axios': 'axios'

再看一下我们的vendor体积

vendor一共才195k

那缺少的elementui文件去哪里找呢?答案是cdn引用。

之前项目里还有引用moment,但是这个库实在是太大了,在github上我找到一个跟momentapi完全一样的库,但是文件大小只要2kb。

其他优化方法还有ssr,这个最好用nuxtjs来做,自己配置ssr实在太麻烦了。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK