Normal loadingFor 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() } 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 LoadingModify 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. PreloadingLazy 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 preloadingtest.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 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: SummarizeNormal 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:
|
<<: 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
background First of all, I would like to state th...
Preface Linux has corresponding open source tools...
Since the project requires a questionnaire, but th...
1. To optimize the query, try to avoid full table...
1. Docker imports local images Sometimes we copy ...
1: Introduction to syslog.conf For different type...
Preface JSON is a lightweight data exchange forma...
ModSecurity is a powerful packet filtering tool t...
1. To optimize the query, try to avoid full table...
Development Pain Points During the development pr...
Preface In a previous project, the CASE WHEN sort...
Table of contents Implementation ideas There are ...
This article shares the installation tutorial of ...
1. Introduction MySQL is used in the project. I i...
First download the dependencies yarn add sass-loa...