FE5: Loading less, loading later

Lesson 1 of 5 ยท reading 15โ€“25 minutes; practice and project time is on the module page

What's in your bundle, and splitting it

๐ŸŽฏYou'll be able to

Read a bundle report and split code so each page ships only what it needs.

๐Ÿ The everyday picture

Packing for a two-day trip, you don't take your whole wardrobe. You pack what you'll need for those two days and leave the rest at home, ready if needed later.

๐Ÿ“˜What it really is

๐Ÿ“A worked example

Kirana's analyser output for the product page:

Item Compressed size Action
Charting library (used only on the admin dashboard) 95 KB Moved to the admin route only
Full icon set 60 KB Imported 12 icons individually: 4 KB
Moment.js with all locales 70 KB Replaced with Intl.DateTimeFormat and a small helper
Map for "store near you" 110 KB Loaded only when the customer taps "Change store"

Product page JavaScript fell from 420 KB to 140 KB compressed, under the 170 KB budget.

๐ŸŒWhere it's used

๐Ÿ“Case scenario

A team adds a markdown editor for product reviews, and the product page's JavaScript grows by 180 KB, although only a few customers write reviews. Loading the editor with a dynamic import when "Write a review" is tapped returns the page to its previous size, and the editor still opens within half a second on 4G.

๐Ÿ–ฑ๏ธTry it

Run a bundle analyser on your Kirana app. List the five largest items, decide for each whether to remove it, import it more specifically, or load it on demand, and measure the page's JavaScript before and after.

โš ๏ธCommon mistakes

๐Ÿ› ๏ธFor your project

The starter deals page imports lib/deals-analytics.ts (about 420 KB). Project 3's JavaScript budget is 170 KB.

โœ…Quick check

What does a bundle analyser show?

Show the answer

Which code and libraries take up the most space in each bundle.

When should a component be loaded with a dynamic import?

Show the answer

When it's heavy and only needed after a user action or on some visits, such as a map or editor.

โ† BackFE5
ProLearners Assist