DEV Community

Cover image for 🍿 Lazy loading
Jorjis Hasan
Jorjis Hasan

Posted on

24 3 3 4 6

🍿 Lazy loading

An essential task of bundler(vite, webpack, parcel) is bundling. What does that mean? This means it takes all the files and bundles all of them into one big ass file. What's the problem then? The problem is that the size of that one file will significantly increase. The app will be very slow as the development build is involved.

So, how to tackle this issue? We need to break down our app into smaller logical chunks. How to make smaller bundles and when to make them? What should be there is a small bundle? If I do logical separation, Over here in makemytrip, it is very clear that I can make components just for my flights, Homestays, Hotels, and Holidays.

A bundle should have enough features for the central part of our app. How to do that? There are different names for this concept. This (Lazy loading) process is also known as

  • Chunking
  • Lazy Loading
  • Code Splitting
  • Dynamic import
  • Dynamic bundling
  • On-demand Loading

These are the same thing.


Problem: Suppose our app has home, about, and contact components. The home component loads when the initial render happens. Nothing special! But, the app requires a particular functionality: the about and contact components will be rendered only when requested.

Solution: Lazy Loading 🍿

Problem Visualization

See the full code β†’


Steps: Implement Lazy loading

  1. lazy()
  2. < Suspense >

Step 1:
We don't use the traditional import approach when we import those two components(about contact). Rather, we'll do something like this:

//app.js
import React, { lazy} from "react";

❌import About from "./components/About";
❌import Contact from "./components/Contact";

βœ…const About = lazy(() => import("./components/About"));
βœ…const Contact = lazy(() => import("./components/Contact"));
Enter fullscreen mode Exit fullscreen mode

Step 2:
<Suspense> lets you display a fallback until its children have finished loading. If you don't use it then react will throw you an error. Wherever we invoke a lazy loading component, we must wrap that into <Suspense> Just like this:

<Suspense fallback={"Loading..."}>
  <About />
</Suspense>
Enter fullscreen mode Exit fullscreen mode

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (8)

Collapse
 
maya_walk profile image
Maya Walker β€’

Lazy loading it's vacation

Collapse
 
jorjishasan profile image
Jorjis Hasan β€’

Enjoy buddy 🍿

Collapse
 
get_pieces profile image
Pieces 🌟 β€’

Great article! πŸ”₯

Collapse
 
_ndeyefatoudiop profile image
Ndeye Fatou Diop β€’

Very nice ! What I also like to do is pass in a webpackChunkName like in here codemzy.com/blog/how-to-name-webpa... so the chunks are easier to find

Collapse
 
jorjishasan profile image
Jorjis Hasan β€’

Well, that's a nicer approach along with Webpack πŸ‘

Collapse
 
iamprahlad profile image
imprahld β€’

awesome share

Collapse
 
jitendrachoudhary profile image
Jitendra Choudhary β€’

Clean Explanation

Collapse
 
jorjishasan profile image
Jorjis Hasan β€’

Glad You liked it !

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay