In this blog post I will explain:
- Why does code splitting exist?
- What is it?
- When to use Code splitting?
Let me give you a dummy example. Then I will give you a real world example.
Suppose, I give you 10 apples and ask you to eat them right now. But you only need to eat only 1 apple. 10 apples are too much to eat and unnecessary to eat. But you can eat them later when you are going to need them.
So why have 10 apples when you only need 1?
Code splitting is kinda a based on these concepts. The concept is :
So let’s take a real world example.
I am building a social media project. It has a hamburger menu icon. If I click on the menu button then a navigation menu will become visible. That menu has code and it has some size. When you build your code for production, the module bundler like webpack will include that code in the main bundle. Here is the main problem. Does that code need to be included in the main bundle? Just because the application has a hamburger menu icon in it that doesn’t mean a user going to click on that.
And this is just a simple case. There might be more cases like this.
So why include those unnecessary codes in the main bundle?
It gives you the following problem :
- The main bundle gets larger.
- Larger bundle takes more time to send.
- Bigger the bundle is more it will take time to parse in the browser.
This problem makes our website slow and users get a bad experience.
The solution is code splitting.
So in the hamburger menu example, just don’t include the code for the hamburger menu in the main bundle. Make a separate chunk. Or as the method name suggests, split the code into separate bundles. Then when a user going to click on that icon, another request will be sent to the server. Then the server will send the requested bundle.
This has these benefits:
- Main bundle will be small.
- Small bundle will take less time to send.
- It will take less time to parse.
It makes our website faster and users will have a good experience.
And Code splitting solves the problem for us.
I have made a video about how to build a carousel postcard with React, Material-UI, and Swiper.js.
If you are interested you can check the video.
You can also demo the application form here
Please like and subscribe to Cules Coding. It motivates me to create more content like this.
If you have any questions, please comment down below.
You can reach out to me on social media as @thatanjan
Stay safe. Goodbye.
The Internet has revolutionized our life. I want to make the internet more beautiful and useful.
I ended up being a full-stack software engineer.
I can develop complex full-stack web applications like social media applications or e-commerce sites.
I have developed a social media application called Confession. The goal of this application is to help people overcome their imposter syndrome by sharing our failure stories.
I also love to share my knowledge. So, I run a youtube channel called Cules Coding where I teach people full-stack web development, data structure algorithms, and many more. So, Subscribe to Cules Coding so that you don’t miss the cool stuff.
I am looking for a team where I can show my ambition and passion and produce great value for them.
Contact me through my email or any social media as @thatanjan
. I would be happy to have a touch with you.
Blogs you might want to read:
Videos might you might want to watch: