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

Mysql anonymous login cannot create a database problem solution

Frequently asked questions Access denied for user...

Docker exec executes multiple commands

The docker exec command can execute commands in a...

CSS draw a lollipop example code

Background: Make a little progress every day, acc...

How to view the type of mounted file system in Linux

Preface As you know, Linux supports many file sys...

How to add fields and comments to a table in sql

1. Add fields: alter table table name ADD field n...

MySQL 5.7.23 version installation tutorial and configuration method

It took me three hours to install MySQL myself. E...

Three useful codes to make visitors remember your website

Three useful codes to help visitors remember your...

Use Javascript to develop sliding-nav navigation plug-in with sliding bar effect

Table of contents 1. Introduction 2. Usage 3. Dev...

Four practical tips for JavaScript string operations

Table of contents Preface 1. Split a string 2. JS...

Vue implements tab navigation bar and supports left and right sliding function

This article mainly introduces: using Vue to impl...

Example code for CSS to achieve horizontal lines on both sides of the text

This article introduces the sample code of CSS to...

How to run tomcat source code in maven mode

Preface Recently, I was analyzing the startup pro...

Detailed explanation of the use of Linux time command

1. Command Introduction time is used to count the...

Mysql Sql statement comments

You can add comments to MySQL SQL statements. Her...