Next.js Reducing Bundle Size
Last Updated :
06 Aug, 2024
Reducing the bundle size in a Next.js application is essential for improving performance and load times. A smaller bundle size means quicker page rendering and a better user experience. Here are strategies to help you minimize your Next.js bundle size.
Prerequisites:
To Reduce the Bundle size in Next.js we can use multiple approaches mentioned below.
- Code Splitting: Code splitting breaks down large JavaScript files into smaller modules, allowing developers to load only the necessary code for a specific page or feature, which improves performance by reducing the amount of data that needs to be processed by the browser.
- Tree Shaking: Removes unused code from your bundles. Write modular code and avoid importing entire libraries when only specific parts are needed.
- Minification: We can reduce the size of JavaScript files by removing whitespace and shortening variable names. Next.js uses SWC for minification. Ensure it's enabled in next.config.js.
- Analyze Bundle Size: Analyze the bundle size with the bundle analyzer. install it from npm and configure it to use in next.js.
Code Splitting
Next.js offers built-in code splitting, allowing developers to easily break down and load smaller portions of code on demand. This can enhance app speed and user responsiveness, especially on slower networks and older devices, resulting in a better user experience.
Steps to Create the Next Application
Step 1: Create the next application by using this command
npx create-next-app code-splitting
Step 2: After creating your project folder, i.e. code-splitting, use the following command to navigate to it:
cd code-splitting
Project Structure:
It will look as follows:
Example: In this example, We use code splitting and the next/link module's client-side navigation to load the Hello component dynamically when the user clicks a link without reloading the whole page. Using code splitting to dynamically import the Hello component reduces the JavaScript bundle size and improves app performance.
Step 3: Create index.js and about.js in the pages directory.
CSS
/* global.css*/
html,
body {
padding: 0;
margin: 0;
font-family: -apple-system,
BlinkMacSystemFont, Segoe UI,
Roboto, Oxygen, Ubuntu,
Cantarell, Fira Sans, Droid Sans,
Helvetica Neue, sans-serif;
}
a {
color: inherit;
text-decoration: none;
}
* {
box-sizing: border-box;
}
@media (prefers-color-scheme: dark) {
html {
color-scheme: dark;
}
body {
color: white;
background: black;
}
}
JavaScript
// pages/index.js
import Link from "next/link";
export default function Index() {
const linkStyle = {
backgroundColor: "green",
color: "white",
fontSize: "1rem",
padding: "0.5rem 1rem",
borderRadius: "4px",
border: "none",
cursor: "pointer",
};
// Showing styled link component.
return (
<div style={{ margin: "2rem" }}>
{/* Create a link to the "hello"
page using Link component. */}
<Link href="/hello" style={linkStyle}>
Click Me!
</Link>
</div>
);
}
JavaScript
// pages/about.js
import Link from "next/link";
export default function About() {
const linkStyle = {
backgroundColor: "green",
color: "white",
fontSize: "1rem",
padding: "0.5rem 1rem",
borderRadius: "4px",
border: "none",
cursor: "pointer",
};
// Showing styled link component.
return (
<div style={{ margin: "2rem" }}>
{/* Create a link to the "hello"
page using Link component. */}
<Link href="/hello" style={linkStyle}>
Click This!
</Link>
</div>
);
}
Step 4: Create a "components/Hello.js" module exporting a React component.
JavaScript
// components/Hello.js
import React from 'react';
export default function Hello({ name }) {
// Render component with "name" prop message
return <div> <h1>Welcome To {name}! </h1> </div>;
}
Step 5: Create a "pages/hello.js" page with the dynamic import of the Hello component.
JavaScript
// pages/hello.js
import dynamic from "next/dynamic";
// Import Hello component dynamically
const Hello = dynamic(
() => import("../components/Hello")
.then((mod) => mod.default), {
ssr: false,
}
);
export default function HelloPage() {
// Show Hello component with name prop
return <Hello name="GeeksforGeeks" />;
}
Step 6: To run the application, open the Terminal and enter the command listed below. Go to http://localhost:3000/ to see the output.
npm run dev
Output:
Conclusion
In conclusion, Next.js offers an easy and efficient way to reduce the size of your JavaScript bundle by using code splitting. By implementing this feature, you can improve the performance and loading speed of your web application, resulting in a better user experience.
Similar Reads
Next.js Bundle Analyzer Next.js is currently the most widely adopted React framework, known for its strong capabilities but often criticized for its significant JavaScript payload to the client. In this article, we will be learning techniques for analyzing bundle size, very important for making websites work better and mak
4 min read
Next.js src Directory The NextJS src directory is a project structure that is optional but is widely recommended. It helps to organize the project in a well-defined structure.Organizing a Next.js project with a well-planned folder structure is important for readability, scalability, and maintainability. A clear structure
4 min read
How to truncate a file using Node.js ? In this article, we are going to explore How to truncate the complete file using Node. There are two methods to truncate the file of all its data. We can either use the fs.truncate() method or the fs.writeFile() method to replace all the file content. Method 1: The fs.truncate() method in Node.js ca
3 min read
Bulma Hero Sizes Bulma is a free and open-source CSS framework based on flexbox which enables web developers to make beautiful and responsive websites for all screen sizes. In this article, we will be seeing the different sizes of the Hero component provided by Bulma. The Hero component is available in 5 different s
2 min read
Blaze UI Container sizes Blaze UI is a framework-free open source UI toolkit that uses JavaScript components to build great user interfaces. It is the same as a bootstrap for use and has excellent different elements to use to make your website look more amazing. This framework allows us to use various of its styles and prop
6 min read
Benefits of Reducing the initial bundle size using code splitting in React In recent web development, optimizing performance is important for delivering fast and responsive user experiences. One effective technique for achieving this goal is code splitting. In this article, we'll delve into the concept of code splitting, and explore its benefits, techniques, performance op
4 min read