Next.js: Reducing Bundle Size When Using Third-Party Libraries

Next.js: Reducing Bundle Size When Using Third-Party Libraries

An example of how to improve bundle size with Next.js

I work at a startup called Parallax, where we’re building a resource planning and project accounting app with Next.js. I recently noticed some of the pages would have a large “First Load JS” after running the next build command in the terminal.

The approaches outlined in this article can be used with any third-party library to decrease the file size of your Next.js bundles. I will use the AnyChart library as an example — more specifically, the AnyChart-React library.

Below you will notice the index and about pages are 795 kB in the “First Load JS” column. Before I added AnyChart to these pages, they were both 70.8 kB.

Next.js build output

Next.js build output

According to the documentation, “The first load is colored green, yellow, or red. Aim for green for performant applications.” The docs also mention what the “Size” and “First Load JS” columns mean:

“Size — The number of assets downloaded when navigating to the page client-side. The size for each route only includes its dependencies.

First Load JS — The number of assets downloaded when visiting the page from the server. The amount of JS shared by all is shown as a separate metric.”

typescript javascript programming react nextjs

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

React With Typescript Crash Course - How To Use React With Typescript

I show how to use React with Typescript: - How to define a component How to specify props using types and interfaces - How to define state - How to handle events

A Quick Intro to TypeScript for JavaScript Programmers

If you are a newer JavaScript developer like me you may have come across the superset of JavaScript known as TypeScript. TypeScript is an open-source extension of JavaScript that changes JavaScript from a functional programming language into an object-oriented programming language.

Start your new React project with Next.js and TypeScript

Start your new React project with Next.js and TypeScript. How to Set Up Next.js With TypeScript

Make an App with React JS / JavaScript: React and TypeScript

Building a Web Application as a Front-End Developer using React JS and TypeScript! Learn how to use the “Thinking in React” method and apply it to your real web application. We will be using real live weather data to display in our application and make our components reusable and maintainable.