What is a skeleton screen

A skeleton screen shows the user the rough structure of the page before it loads, until the page requests data and renders the page. Compared with loading, skeleton screen has better user experience.

Implement skeleton screen

1. Install the plug-in

npm install vue-skeleton-webpack-plugin --save
Copy the code

2. Create skeleton.vue in the SRC directory

<template>
    <div class="skeleton-wrapper">
        <section class="skeleton-block">
            <img src="data:image/svg+xml; base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB2a WV3Qm94PSIwIDAgMTA4MCAyNjEiPjxkZWZzPjxwYXRoIGlkPSJiIiBkPSJNMCAwaDEwODB2MjYwSDB6Ii8+PGZpbHRlciBpZD0iYSIgd2lkdGg9IjIwMCUiI GhlaWdodD0iMjAwJSIgeD0iLTUwJSIgeT0iLTUwJSIgZmlsdGVyVW5pdHM9Im9iamVjdEJvdW5kaW5nQm94Ij48ZmVPZmZzZXQgZHk9Ii0xIiBpbj0iU291c mNlQWxwaGEiIHJlc3VsdD0ic2hhZG93T2Zmc2V0T3V0ZXIxIi8+PGZlQ29sb3JNYXRyaXggaW49InNoYWRvd09mZnNldE91dGVyMSIgdmFsdWVzPSIwIDAgM CAwIDAuOTMzMzMzMzMzIDAgMCAwIDAgMC45MzMzMzMzMzMgMCAwIDAgMCAwLjkzMzMzMzMzMyAwIDAgMCAxIDAiLz48L2ZpbHRlcj48L2RlZnM+PGcgZmlsb D0ibm9uZSIgZmlsbC1ydWxlPSJldmVub2RkIiB0cmFuc2Zvcm09InRyYW5zbGF0ZSgwIDEpIj48dXNlIGZpbGw9IiMwMDAiIGZpbHRlcj0idXJsKCNhKSIge Gxpbms6aHJlZj0iI2IiLz48dXNlIGZpbGw9IiNGRkYiIHhsaW5rOmhyZWY9IiNiIi8+PHBhdGggZmlsbD0iI0Y2RjZGNiIgZD0iTTIzMCA0NGg1MzN2NDZIM jMweiIvPjxyZWN0IHdpZHRoPSIxNzIiI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8+PHBhdGggZmlsbD0iI0Y2RjZGNiIgZD0iTTIzMCA0NGg1MzN2NDZIM jMweiIvPjxyZWN0IHdpZHRoPSIxNzIiIGhlaWdodD0iMTcyIiB4PSIzMCIgeT0iNDQiIGZpbGw9IiNGNkY2RjYiIHJ4PSI0Ii8+PHBhdGggZmlsbD0iI0Y2R jZGNiIgZD0iTTIzMCAxMThoMzY5djMwSDIzMHpNMjMwIDE4MmgzMjN2MzBIMjMwek04MTIgMTE1aDIzOHYzOUg4MTJ6TTgwOCAxODRoMjQydjMwSDgwOHpNO TE3IDQ4aDEzM3YzN0g5MTd6Ii8+PC9nPjwvc3ZnPg==">
        </section>
    </div>
</template>

<script>
    export default {
        name: 'skeleton'
    };
</script>

<style scoped>
    .skeleton-block {
        display: flex;
        flex-direction: column;
        padding-top: 8px;
    }
</style>
Copy the code

3. Create the entry-skeletal. js file in the SRC directory

import Vue from 'vue'// Create a skeleton screen Vue instance import skeleton from'./skeleton';
export default new Vue({
    components: {
        skeleton
    },
    template: '<skeleton />'
});
Copy the code

4. Create webpack.skeleton.conf.js in the build directory

'use strict';
 
const path = require('path')
const merge = require('webpack-merge')
const baseWebpackConfig = require('./webpack.base.conf')
const nodeExternals = require('webpack-node-externals')
 
function resolve(dir) {
  return path.join(__dirname, dir)
}
 
module.exports = merge(baseWebpackConfig, {
  target: 'node',
  devtool: false,
  entry: {
    app: resolve('.. /src/entry-skeleton.js')
  },
  output: Object.assign({}, baseWebpackConfig.output, {
    libraryTarget: 'commonjs2'
  }),
  externals: nodeExternals({
    whitelist: /\.css$/
  }),
  plugins: []
})
Copy the code

5. Introduce webpack.skeleton.conf.js in webpack.dev.conf.js and webpack.prod.conf.js

const SkeletonWebpackPlugin = require('vue-skeleton-webpack-plugin')

new SkeletonWebpackPlugin({
      webpackConfig: require('./webpack.skeleton.conf'),
      quiet: true
    })
Copy the code

Complete the above steps can achieve the skeleton screen, but the page structure changes need UI with the production of page structure picture, is not very ideal

The solution is to change extract and usePostCSS to false in the webpack.prod.conf.js file

 rules: utils.styleLoaders({
      sourceMap: config.build.productionSourceMap,
      extract: false,
      usePostCSS: false
    })
Copy the code

Automatically build the skeleton screen

Using ele. me team skeleton screen solution page-skeleton-webpack-plugin

Installing a plug-in

npm install page-skeleton-webpack-plugin --save
Copy the code

How to use the plug-in: Plug-in usage documentation

Current problems: The skeleton screen was successfully built, but not reflected in the project,(possibly a bug) need to be fixed by the Team. I would love to use this solution to implement skeleton screens if I can fix it.

Reference article: VuE-CLI project adds skeleton screen