This content originally appeared on DEV Community and was authored by Anjan Shomooder
In this blog post I will explain:
- Why does code splitting exist?
- What is it?
- When to use Code splitting?
So,
Why does code splitting exist?
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 :
Why have more when you only need less?
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.
So what is the solution?
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.
So, when to use Code splitting?
- This is especially useful for Client side rendering applications. We know that in CSR applications content is rendered by javascript. So definitely the JavaScript bundle will be large. Depends on the application size, the bundle can be huge. And we have known that what happens when bundles are huge. Another problem is blank page flickering. Because there is no content on the html itself, so the browser has to wait for javascript to load and parse. Until everything is done, the user sees a blank page for a long time. By the way, I have already created video about Client side rendering and Server side rendering. you can check them out.
And Code splitting solves the problem for us.
Shameless Plug
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.
About me
Why do I do what I do?
The Internet has revolutionized our life. I want to make the internet more beautiful and useful.
What do I do?
I ended up being a full-stack software engineer.
What can I do?
I can develop complex full-stack web applications like social media applications or e-commerce sites.
What have I done?
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.
Want to work with me?
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.
Contacts
- Email: thatanjan@gmail.com
- linkedin: @thatanjan
- portfolio: anjan
- Github: @thatanjan
- Instagram (personal): @thatanjan
- Instagram (youtube channel): @thatanjan
- Twitter: @thatanjan
- Facebook: @thatanjan
Blogs you might want to read:
- Eslint, prettier setup with TypeScript and react
- What is Client-Side Rendering?
- What is Server Side Rendering?
- Everything you need to know about tree data structure
- 13 reasons why you should use Nextjs
Videos might you might want to watch:
This content originally appeared on DEV Community and was authored by Anjan Shomooder
Anjan Shomooder | Sciencx (2021-10-14T18:04:39+00:00) What is Code Splitting?. Retrieved from https://www.scien.cx/2021/10/14/what-is-code-splitting/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.