Exploring the NextJS bundle analyzer

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Did you know that Next.js gives us an option to analyze our output bundle size?
While creating our application, it's hard to determine what will be included in the final build output.
But no worries, I'll show you how you can add the bundle analyzer to analyze the build output in this article.
First, let's take an existing Next.js project to work on. I will use my Next markdown blog for this.
The first thing we want to do is install the analyzer with the following command.
npm install @next/bundle-analyzer
The next part is to create/modify our next.config.js file.
The first element we need is a definition of the analyzer, which we can import like this.
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
});
The following step might depend on the fact that you already have some configuration.
If not, you can do the following export.
module.exports = withBundleAnalyzer({});
Otherwise, you have to wrap your existing export with the bundle analyzer.
module.exports = withBundleAnalyzer({
reactStrictMode: true,
});
To run the analyzer, we have to use the environment variable defined above.
The command would look like this:
ANALYZE=true npm run build
When you run this command, it will automatically open two pages in your browser.

You can quickly inspect what takes up the most space, or when using a Monorepos, which packages might have been included twice unintentionally.
We can also create a quick command, so we don't have to worry about setting this environment variable on every call.
Head over to your package.json file and add a new script like this.
"scripts": {
...
"analyze": "ANALYZE=true next build"
},
Now you can quickly run the analyzer with the following command.
npm run analyze
I've also uploaded the changes to the project so you can view them on GitHub.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter