Detailed explanation of lazy loading and preloading of webpack

Detailed explanation of lazy loading and preloading of webpack

Normal loading

For ease of viewing, the code in index.js is very simple

console.log('index.js is executed')
import { test } from './test.js'
document.getElementById('btn-wrap').onclick = function () {
    test()
}

test.js

console.log('test.js executed')
export function test() {
    const value = 'hello world'
    console.log('test value: ', value)
}

Add a button in index.html

    <button id='btn-wrap'>Click</button>

Execute the webpack command:

You can see that test.js is loaded when the button is not clicked. If test.js is large, loading it will consume performance. We hope to load it when we need it.

Lazy Loading

Modify the code in index.js

console.log('index.js is executed')
// import { test } from './test.js'
// document.getElementById('btn-wrap').onclick = function () {
//test()
// }
document.getElementById('btn-wrap').onclick = function () {
    console.log('==== Click the button')
    import(/*webpackChunkName:'test' */"./test")
        .then(({test}) => {
            console.log('test loaded successfully')
            test()
        })
        .catch(error => {
            console.log('test loading failed error:', error)
        })
}

Execute the webpack command again and view the log in the browser

Only index.js is loaded before clicking the button

Click the button:

You can see that test.js is executed after clicking the button.

Preloading

Lazy loading implements on-demand loading of js files, which means they are loaded only when they are needed. However, if the js file is very large and loads slowly, loading it when it is in use will cause the page to freeze. To optimize this problem, you can use Prefetch to preload first.

Not using preloading

test.js file is not loaded until the button is clicked

The test.js file will be loaded only after clicking the button

Using Preload

Set webpackPrefetch:true to use preloading

document.getElementById('btn-wrap').onclick = function () {
    console.log('==== Click the button')
    import(/*webpackChunkName:'test' ,webpackPrefetch:true*/"./test")
        .then(({test}) => {
            console.log('test loaded successfully')
            test()
        })
        .catch(error => {
            console.log('test loading failed error:', error)
        })
}

The test.js file is preloaded before clicking the button:

Click the button:

Summarize

Normal loading : many resources are loaded in parallel, and multiple files are loaded at the same time

Lazy loading : loading only when needed

Preloading : Wait until other resources are loaded and the browser is idle, then secretly load the resources set to be preloaded

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Vue+webpack implements lazy loading process analysis
  • Webpack4 SCSS extraction and lazy loading example
  • Detailed explanation of how webpack + react + react-router implements lazy loading
  • JavaScript to implement image preloading and lazy loading
  • Specific use of lazy loading and preloading in js
  • Detailed explanation of JS image preloading plug-in

<<:  What are the similarities between the development of web design and western architecture?

>>:  Detailed process of changing apt source to Alibaba Cloud source in Ubuntu 18.04

Recommend

Example of implementing todo application with Vue

background First of all, I would like to state th...

The implementation process of Linux process network traffic statistics

Preface Linux has corresponding open source tools...

How to submit a pure HTML page, pass parameters, and verify identity

Since the project requires a questionnaire, but th...

52 SQL statements to teach you performance optimization

1. To optimize the query, try to avoid full table...

How to import/save/load/delete images locally in Docker

1. Docker imports local images Sometimes we copy ...

Interpretation of syslogd and syslog.conf files under Linux

1: Introduction to syslog.conf For different type...

Instructions for using JSON operation functions in Mysql5.7

Preface JSON is a lightweight data exchange forma...

Installation, activation and configuration of ModSecurity under Apache

ModSecurity is a powerful packet filtering tool t...

MySql development of automatic synchronization table structure

Development Pain Points During the development pr...

Example of using CASE WHEN in MySQL sorting

Preface In a previous project, the CASE WHEN sort...

MySQL 8.0.11 compressed version installation tutorial

This article shares the installation tutorial of ...

How to introduce scss into react project

First download the dependencies yarn add sass-loa...