Vue SPA first screen optimization solution

Vue SPA first screen optimization solution

Preface

When a regular vue project is packaged and accessed, it returns an html containing only div, and other content blocks are dynamically generated by js.

There are two big problems:

  • Not good for SEO
  • The first screen loading page is slow, and the user experience is not good

This article is a solution summarized by myself based on project experience. If there are any deficiencies, please point them out~

optimization

SSR

SSR (Server-Side Rendering) is server-side rendering, which renders Vue components into assembled HTML strings on the server side, and then sends them directly to the browser. Finally, these static tags need to be mixed into a fully interactive application on the client.
After redeploying the build project using ssr:

You can see that the returned content already contains the HTML code block of the first screen content, perfect!~.~

Speedy portal: A small demo based on vue-cli4.0+Typescript+SSR

Import on demand

Use es6 module for on-demand import

1. Import components on demand in routing files

# router.index.ts
export function createRouter() {
 return new Router({
 mode: 'history',
 routes: [
  {
  path: '/',
  name: 'Home',
  component: () => import(/* webpackChunkName: "Home" */ './views/Home.vue'),
  },
  {
  path: '/about',
  name: 'About',
  component: () => import(/* webpackChunkName: "about" */ './views/About.vue'),
  },
 ],
 });
}

2. Static libraries import modules on demand, not all of them

For example, in the element-ui library, I only want to use the button component:

import {
 button
} from 'element-ui';

Request optimization

1. CSS and JS placement order

The css file is placed in the header, and the js file is placed before the body, but vue has already handled it for us~

2. Use font icons, and use sprite images for icon resources

We know that HTTP requires three handshakes to establish a connection. Too many requests will affect the loading speed.

We should try to reduce unnecessary static resources, such as icons on the page, as shown in the following picture from Tencent's official website:

Directly import a completed image and set the image position according to background-position

Use CDN

Static resources are uploaded to CDN to improve access speed

Without CDN:

Using CDN:

It can be seen that after using CDN, static files will be compressed by GZIP, and the download speed will be greatly improved.

Entry chunk optimization

Splitting the entry chunk files and separating some static libraries can both speed up packaging and optimize loading.

As shown below, some static libraries are separated: vuejs, axios, vuex, etc. You can see that the browser will open multiple download threads for downloading. If these static libraries are not separated, the entry chunk will be very large, and the loading speed can be imagined~.~

Separate an element-ui library. We don't need to worry about the dev environment. We only separate it in the prod environment. We only need to remove the library in the vue packaging configuration:

# vue.config.js
configureWebpack: {
 externals:
 process.env.NODE_ENV === 'production'
  ? {
  'element-ui': 'element-ui',
  }
  : undefined,
},

# index.html manually import static resources <script src="/js/element-ui/element-ui.2.11.1.js"></script>

The above is the details of the Vue SPA first screen optimization plan. For more information about Vue SPA first screen optimization, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Vue SPA single-page application first screen optimization practice
  • Detailed explanation of Vue's self-implementation of dispatch and broadcast (dispatch and broadcast)
  • Vue implementation example of boradcast and dispatch
  • vue-cli single page pre-rendering seo-prerender-spa-plugin operation
  • Usage of store.commit and store.dispatch in vuex
  • Vue SPA first enters the loading animation implementation code
  • Vue realizes the effect of merging columns in data table rowspan
  • Routing caching problems and solutions in Vue SPA applications
  • Solve the problem that Vuex's Dispatch has no effect under Vue+Electron
  • VUE solves the WeChat signature and SPA WeChat invalid signature problem (perfect processing)
  • Detailed explanation of when the action in vuex is completed and how to call dispatch correctly

<<:  The perfect solution to the Chinese garbled characters in mysql6.x under win7

>>:  Detailed explanation of the pitfalls of nginx proxy socket.io service

Recommend

How to change the Ali source in Ubuntu 20.04

Note that this article does not simply teach you ...

Detailed explanation of the wonderful CSS attribute MASK

This article will introduce a very interesting at...

How to deploy Redis 6.x cluster through Docker

System environment: Redis version: 6.0.8 Docker v...

Implementation of Mysql User Rights Management

1. Introduction to MySQL permissions There are 4 ...

Let’s talk about the symbol data type in ES6 in detail

Table of contents Symbol Data Type The reason why...

Implementation of WeChat applet message push in Nodejs

Select or create a subscription message template ...

How to implement interception of URI in nginx location

illustrate: Root and alias in location The root d...

Summary of web design experience and skills

■ Website theme planning Be careful not to make yo...

CSS3 realizes the website product display effect diagram

This article introduces the effect of website pro...

Docker container time zone adjustment operation

How to check if the Docker container time zone is...

HTML table tag tutorial (26): cell tag

The attributes of the <TD> tag are used to ...

How to use environment variables in nginx configuration file

Preface Nginx is an HTTP server designed for perf...

mysql 5.7.18 winx64 free installation configuration method

1. Download 2. Decompression 3. Add the path envi...