Dynamic Import (2024)

Performance Pattern

In our chat application, we have four key components: UserInfo, ChatList, ChatInput and EmojiPicker. However, only three of these components are used instantly on the initial page load: UserInfo, ChatList and ChatInput. The EmojiPicker isn’t directly visible, and may not even be rendered at all if the user won’t even click on the Emoji in order to toggle the EmojiPicker. This would mean that we unnecessarily added the EmojiPicker module to our initial bundle, which potentially increased the loading time!

In order to solve this, we can dynamically import the EmojiPicker component. Instead of statically importing it, we’ll only import it when we want to show the EmojiPicker. An easy way to dynamically import components in React is by using React Suspense. The React.Suspense component receives the component that should be dynamically loaded, which makes it possible for the App component can render its contents faster by suspending the import of the EmojiPicker module! When the user clicks on the emoji, the EmojiPicker component gets rendered for the first time. The EmojiPicker component renders a Suspense component, which receives the lazily imported module: the EmojiPicker in this case. The Suspense component accepts a fallback prop, which receives the component that should get rendered while the suspended component is still loading!

Instead of unnecessarily adding EmojiPicker to the initial bundle, we can split it up into its own bundle and reduce the size of the initial bundle!

A smaller initial bundle size means a faster initial load: the user doesn’t have to stare at a blank loading screen for as long. The fallback component lets the user know that our application hasn’t frozen: they simply need to wait a little while for the module to be processed and executed.

Asset Size Chunks Chunk Namesemoji-picker.bundle.js 1.48 KiB 1 [emitted] emoji-pickermain.bundle.js 1.33 MiB main [emitted] mainvendors~emoji-picker.bundle.js 171 KiB 2 [emitted] vendors~emoji-picker

Whereas previously the initial bundle was 1.5MiB, we’ve been able to reduce it to 1.33 MiB by suspending the import of the EmojiPicker!

In the console, you can see that the EmojiPicker doesn’t get executed until we’ve toggled the EmojiPicker!

ChatInput.js

1import React, { Suspense, lazy } from "react";

2 // import Send from "./icons/Send";

3 // import Emoji from "./icons/Emoji";

4 const Send = lazy(() =>

5 import(/*webpackChunkName: "send-icon" */ "./icons/Send")

6 );

7 const Emoji = lazy(() =>

8 import(/*webpackChunkName: "emoji-icon" */ "./icons/Emoji")

9 );

10 // Lazy load EmojiPicker when <EmojiPicker /> renders

11 const Picker = lazy(() =>

12 import(/*webpackChunkName: "emoji-picker" */ "./EmojiPicker")

13 );

14

15 const ChatInput = () => {

16 const [pickerOpen, togglePicker] = React.useReducer(state => !state, false);

17

18 return (

19 <Suspense fallback={<p id="loading">Loading...</p>}>

20 <div className="chat-input-container">

21 <input type="text" placeholder="Type a message..." />

22 <Emoji onClick={togglePicker} />

23 {pickerOpen && <Picker />}

24 <Send />

25 </div>

26 </Suspense>

27 );

28 };

29

30 console.log("ChatInput loaded", Date.now());

31

32 export default ChatInput;

When building the application, we can see the different bundles that Webpack created.

By dynamically importing the EmojiPicker component, we managed to reduce the initial bundle size from 1.5MiB to 1.33 MiB! Although the user may still have to wait a while until the EmojiPicker has been fully loaded, we have improved the user experience by making sure the application is rendered and interactive while the user waits for the component to load.

Loadable Components

Server-side rendering doesn’t support React Suspense (yet). A good alternative to React Suspense is the loadable-components library, which can be used in SSR applications.

ChatInput.js

1import React from "react";

2import loadable from "@loadable/component";

3

4import Send from "./icons/Send";

5import Emoji from "./icons/Emoji";

6

7const EmojiPicker = loadable(() => import("./EmojiPicker"), {

8 fallback: <div id="loading">Loading...</div>

9});

10

11const ChatInput = () => {

12 const [pickerOpen, togglePicker] = React.useReducer(state => !state, false);

13

14 return (

15 <div className="chat-input-container">

16 <input type="text" placeholder="Type a message..." />

17 <Emoji onClick={togglePicker} />

18 {pickerOpen && <EmojiPicker />}

19 <Send />

20 </div>

21 );

22};

23

24export default ChatInput;

Similar to React Suspense, we can pass the lazily imported module to the loadable, which will only import the module once the EmojiPicker module is being requested! While the module is being loaded, we can render a fallback component.

Although loadable components are a great alternative to React Suspense for SSR applications, they’re also useful in CSR applications in order to suspend the import of modules.

ChatInput.js

1import React from "react";

2 import Send from "./icons/Send";

3 import Emoji from "./icons/Emoji";

4 import loadable from "@loadable/component";

5

6 const EmojiPicker = loadable(() => import("./components/EmojiPicker"), {

7 fallback: <p id="loading">Loading...</p>

8 });

9

10 const ChatInput = () => {

11 const [pickerOpen, togglePicker] = React.useReducer(state => !state, false);

12

13 return (

14 <div className="chat-input-container">

15 <input type="text" placeholder="Type a message..." />

16 <Emoji onClick={togglePicker} />

17 {pickerOpen && <EmojiPicker />}

18 <Send />

19 </div>

20 );

21 };

22

23 console.log("ChatInput loaded", Date.now());

24

25 export default ChatInput;

Dynamic Import (2024)
Top Articles
60 Scriptures About Prosperity and Success
What Data Recovery Software Do Police Use? - GadgetMates
Kostner Wingback Bed
Swimgs Yuzzle Wuzzle Yups Wits Sadie Plant Tune 3 Tabs Winnie The Pooh Halloween Bob The Builder Christmas Autumns Cow Dog Pig Tim Cook’s Birthday Buff Work It Out Wombats Pineview Playtime Chronicles Day Of The Dead The Alpha Baa Baa Twinkle
Brady Hughes Justified
Identifont Upload
Hk Jockey Club Result
Ashlyn Peaks Bio
27 Places With The Absolute Best Pizza In NYC
Flat Twist Near Me
2024 Non-Homestead Millage - Clarkston Community Schools
Wizard Build Season 28
065106619
Voy Boards Miss America
Craigslist Red Wing Mn
Mychart Anmed Health Login
Craigslist Pet Phoenix
Project, Time & Expense Tracking Software for Business
Atdhe Net
Conan Exiles Sorcery Guide – How To Learn, Cast & Unlock Spells
Never Give Up Quotes to Keep You Going
Brazos Valley Busted Newspaper
Jobs Hiring Near Me Part Time For 15 Year Olds
Weldmotor Vehicle.com
Boxer Puppies For Sale In Amish Country Ohio
Sorrento Gourmet Pizza Goshen Photos
Villano Antillano Desnuda
Play It Again Sports Forsyth Photos
Isablove
Dairy Queen Lobby Hours
+18886727547
Mrstryst
Fridley Tsa Precheck
2012 Street Glide Blue Book Value
A Man Called Otto Showtimes Near Amc Muncie 12
Aliciabibs
Ise-Vm-K9 Eol
Directions To Advance Auto
Letter of Credit: What It Is, Examples, and How One Is Used
sacramento for sale by owner "boats" - craigslist
The Angel Next Door Spoils Me Rotten Gogoanime
Www.craigslist.com Waco
Kutty Movie Net
Ehome America Coupon Code
Enr 2100
Okta Login Nordstrom
18443168434
Game Akin To Bingo Nyt
Ics 400 Test Answers 2022
Southwind Village, Southend Village, Southwood Village, Supervision Of Alcohol Sales In Church And Village Halls
Latest Posts
Article information

Author: Terrell Hackett

Last Updated:

Views: 6298

Rating: 4.1 / 5 (52 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Terrell Hackett

Birthday: 1992-03-17

Address: Suite 453 459 Gibson Squares, East Adriane, AK 71925-5692

Phone: +21811810803470

Job: Chief Representative

Hobby: Board games, Rock climbing, Ghost hunting, Origami, Kabaddi, Mushroom hunting, Gaming

Introduction: My name is Terrell Hackett, I am a gleaming, brainy, courageous, helpful, healthy, cooperative, graceful person who loves writing and wants to share my knowledge and understanding with you.