Table of Contents
使用CDN资源,减小服务器带宽压力
路由懒加载
将一些静态js css放到其他地方(如OSS),减小服务器压力
按需加载三方资源,如iview,建议按需引入iview中的组件
使用nginx开启gzip减小网络传输的流量大小
webpack开启gzip压缩
Home Web Front-end JS Tutorial How to optimize the first screen loading speed of single-page applications in Vue (details)

How to optimize the first screen loading speed of single-page applications in Vue (details)

Aug 27, 2018 am 10:39 AM
javascript nginx vue.js

本篇文章给大家带来的内容是关于vue中如何优化单页应用首屏加载速度(详细) ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

单页应用会随着项目越大,导致首屏加载速度很慢!!!以下给出在下知道的几种优化方案

  1. 使用CDN资源,减小服务器带宽压力

  2. 路由懒加载

  3. 将一些静态js css放到其他地方(如OSS),减小服务器压力

  4. 按需加载三方资源,如iview,建议按需引入iview中的组件

  5. 使用nginx开启gzip减小网络传输的流量大小

  6. webpack开启gzip压缩

  7. 若首屏为登录页,可以做成多入口,登录页单独分离为一个入口

使用CDN资源,减小服务器带宽压力

在index.html中引入cdn资源

...
  <body>
    <p id="app">
    </p>
    <!-- built files will be auto injected -->
    <script src="https://cdn.bootcss.com/vue/2.5.2/vue.min.js"></script>
    <script src="https://cdn.bootcss.com/vue-router/3.0.1/vue-router.min.js"></script>
    <script src="https://cdn.bootcss.com/vuex/3.0.1/vuex.min.js"></script>
    <script src="https://cdn.bootcss.com/vue-resource/1.5.1/vue-resource.min.js"></script>
  </body>
  ...
Copy after login

修改 build/webpack.base.conf.js

module.exports = {
  context: path.resolve(__dirname, &#39;../&#39;),
  entry: {
    app: &#39;./src/main.js&#39;
  },
  externals:{
    &#39;vue&#39;: &#39;Vue&#39;,
    &#39;vue-router&#39;: &#39;VueRouter&#39;,
    &#39;vuex&#39;:&#39;Vuex&#39;,
    &#39;vue-resource&#39;: &#39;VueResource&#39;
  },
  ...
}
Copy after login

修改src/main.js src/router/index.js 注释掉import引入的vue,vue-resource

// import Vue from &#39;vue&#39;
// import VueResource from &#39;vue-resource&#39;
// Vue.use(VueResource)
Copy after login

路由懒加载

const workCircle = r => require.ensure([], () => r(require(&#39;@/module/work-circle/Index&#39;)), &#39;workCircle&#39;)
const workCircleList = r => require.ensure([], () => r(require(&#39;@/module/work-circle/page/List&#39;)), &#39;workCircleList&#39;)
Copy after login

将一些静态js css放到其他地方(如OSS),减小服务器压力

注意这里的js文件,需要将结果抛出,然后在需要用到该js的组件中import引入

按需加载三方资源,如iview,建议按需引入iview中的组件

按需引用请查看iview官方文档iview

使用nginx开启gzip减小网络传输的流量大小

配置nginx,可以参考Nginx开启Gzip压缩大幅提高页面加载速度

webpack开启gzip压缩

这里需要配合Nginx服务器,Nginx开启gzip

  • config/index.js中

module.exports = {
  build: {
    ...
    // Gzip off by default as many popular static hosts such as
    // Surge or Netlify already gzip all static assets for you.
    // Before setting to `true`, make sure to:
    // npm install --save-dev compression-webpack-plugin
    productionGzip: true, // 就是这里开启gzip,vue-cli搭建项目,这里默认为false
    productionGzipExtensions: [&#39;js&#39;, &#39;css&#39;],

    // Run the build command with an extra argument to
    // View the bundle analyzer report after build finishes:
    // `npm run build --report`
    // Set to `true` or `false` to always turn it on or off
    bundleAnalyzerReport: process.env.npm_config_report
  }
}
Copy after login

build/webpack.prod.conf.js中

使用vue-cli构建项目时,默认会有这段代码

if (config.build.productionGzip) {
  const CompressionWebpackPlugin = require(&#39;compression-webpack-plugin&#39;)
  webpackConfig.plugins.push(
    new CompressionWebpackPlugin({
      asset: &#39;[path].gz[query]&#39;,
      algorithm: &#39;gzip&#39;,
      test: new RegExp(
        &#39;\\.(&#39; +
        config.build.productionGzipExtensions.join(&#39;|&#39;) +
        &#39;)$&#39;
      ),
      threshold: 10240,
      minRatio: 0.8
    })
  )
}
Copy after login

若首屏为登录页,可以做成多入口,登录页单独分离为一个入口

相关推荐:

Vue怎么使用CDN优化首屏加载的速度

js延迟加载思想和首屏延迟加载讲解

The above is the detailed content of How to optimize the first screen loading speed of single-page applications in Vue (details). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

Understanding Vue.js: Primarily a Frontend Framework Understanding Vue.js: Primarily a Frontend Framework Apr 17, 2025 am 12:20 AM

Vue.js is a progressive JavaScript framework released by You Yuxi in 2014 to build a user interface. Its core advantages include: 1. Responsive data binding, automatic update view of data changes; 2. Component development, the UI can be split into independent and reusable components.

See all articles