Unverified Commit bfeedb07 authored by Delba de Oliveira's avatar Delba de Oliveira Committed by GitHub

Guide: Update `Optimizing Package Bundling` to include new Bundle Analyzer (#87246)

This PR adds instructions on how to run and navigate the new turbopack
bundle analyzer to the existing [Optimizing package bundling guide
](https://nextjs.org/docs/app/guides/package-bundling).
Todo: 
- [x] Embed video
- [x] Add image for import chain

---------
Co-authored-by: 's avatarLuke Sandberg <lukesandberg@users.noreply.github.com>
Co-authored-by: 's avatargraphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
Co-authored-by: 's avatarJoseph <joseph.chamochumbi@vercel.com>
Co-authored-by: 's avatarRich Haines <hello@richardhaines.dev>
Co-authored-by: 's avatarvercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
parent c02a04d9
......@@ -328,7 +328,7 @@ function HoverPrefetchLink({ href, children }) {
React mitigates this with Selective Hydration and you can further improve this by:
- Using the [`@next/bundle-analyzer`](/docs/app/guides/package-bundling#analyzing-javascript-bundles) plugin to identify and reduce bundle size by removing large dependencies.
- Using the [`@next/bundle-analyzer`](/docs/app/guides/package-bundling#nextbundle-analyzer-for-webpack) plugin to identify and reduce bundle size by removing large dependencies.
- Moving logic from the client to the server where possible. See the [Server and Client Components](/docs/app/getting-started/server-and-client-components) docs for guidance.
## Examples
......
---
title: How to optimize package bundling
title: Optimizing package bundling
nav_title: Package Bundling
description: Learn how to optimize your application's server and client bundles.
description: Learn how to analyze and optimize your application's server and client bundles with the Next.js Bundle Analyzer for Turbopack, and the `@next/bundle-analyzer` plugin for Webpack.
related:
description: Learn more about optimizing your application for production.
links:
- app/guides/production-checklist
---
Bundling external packages can significantly improve the performance of your application. <AppOnly>By default, packages imported inside Server Components and Route Handlers are automatically bundled by Next.js. This page will guide you through how to analyze and further optimize package bundling.</AppOnly> <PagesOnly>By default, packages imported into your application are not bundled. This can impact performance or might not work if external packages are not pre-bundled, for example, if imported from a monorepo or `node_modules`. This page will guide you through how to analyze and configure package bundling.</PagesOnly>
Bundling is the process of combining your application code and its dependencies into optimized output files for the client and server. Smaller bundles load faster, reduce JavaScript execution time, improve [Core Web Vitals](https://web.dev/articles/vitals), and lower server cold start times.
## Analyzing JavaScript bundles
Next.js automatically optimizes bundles by code splitting, tree-shaking, and other techniques. However, there are some cases where you may need to optimize your bundles manually.
[`@next/bundle-analyzer`](https://www.npmjs.com/package/@next/bundle-analyzer) is a plugin for Next.js that helps you manage the size of your application bundles. It generates a visual report of the size of each package and their dependencies. You can use the information to remove large dependencies, split, or [lazy-load](/docs/app/guides/lazy-loading) your code.
There are two tools for analyzing your application's bundles:
### Installation
- [Next.js Bundle Analyzer for Turbopack (experimental)](#nextjs-bundle-analyzer-experimental)
- [`@next/bundle-analyzer` plugin for Webpack](#nextbundle-analyzer-for-webpack)
This guide will walk you through how to use each tool and how to [optimize large bundles](#optimizing-large-bundles).
## Next.js Bundle Analyzer (Experimental)
> Available in v16.1 and later. You can share feedback in the [dedicated GitHub discussion](https://github.com/vercel/next.js/discussions/86731) and view the demo at [turbopack-bundle-analyzer-demo.vercel.sh](https://turbopack-bundle-analyzer-demo.vercel.sh/).
The Next.js Bundle Analyzer is integrated with Turbopack's module graph. You can inspect server and client modules with precise import tracing, making it easier to find large dependencies. Open the interactive Bundle Analyzer demo to explore the module graph.
### Step 1: Run the Turbopack Bundle Analyzer
To get started, run the following command and open up the interactive view in your browser.
```bash filename="Terminal"
npx next experimental-analyze
```
### Step 2: Filter and inspect modules
Within the UI, you can filter by route, environment (client or server), and type (JavaScript, CSS, JSON), or search by file:
<Video
caption="Next.js bundle analyzer UI walkthrough"
src="/videos/bundle-analyzer.mp4"
width={1708}
height={1080}
/>
### Step 3: Trace modules with import chains
The treemap shows each module as a rectangle. Where the size of the module is represented by the area of the rectangle.
Click a module to see its size, inspect its full import chain and see exactly where it’s used in your application:
<Image
caption="Next.js Bundle Analyzer import chain view"
srcLight="/docs/light/bundle-analyzer.png"
srcDark="/docs/dark/bundle-analyzer.png"
width={1600}
height={874}
/>
### Step 4: Write output to disk for sharing or diffing
If you want to share the analysis with teammates or compare bundle sizes before/after optimizations, you can skip the interactive view and save the analysis as a static file with the `--output` flag:
```bash filename="Terminal"
npx next experimental-analyze --output
```
This command writes the output to `.next/diagnostics/analyze`. You can copy this directory elsewhere to compare results:
```bash filename="Terminal"
cp -r .next/diagnostics/analyze ./analyze-before-refactor
```
> More options are available for the Bundle Analyzer, see Next.js CLI reference docs for the full list.
## `@next/bundle-analyzer` for Webpack
The [`@next/bundle-analyzer`](https://www.npmjs.com/package/@next/bundle-analyzer) is a plugin that helps you manage the size of your application bundles. It generates a visual report of the size of each package and their dependencies. You can use the information to remove large dependencies, split, or [lazy-load](/docs/app/guides/lazy-loading) your code.
### Step 1: Installation
Install the plugin by running the following command:
......@@ -39,7 +103,7 @@ const withBundleAnalyzer = require('@next/bundle-analyzer')({
module.exports = withBundleAnalyzer(nextConfig)
```
### Generating a report
### Step 2: Generating a report
Run the following command to analyze your bundles:
......@@ -51,13 +115,15 @@ ANALYZE=true yarn build
ANALYZE=true pnpm build
```
The report will open three new tabs in your browser, which you can inspect. Periodically evaluating your application's bundles can help you maintain application performance over time.
The report will open three new tabs in your browser, which you can inspect.
## Optimizing large bundles
## Optimizing package imports
Once you've identified a large module, the solution will depend on your use case. Below are common causes and how to fix them:
Some packages, such as icon libraries, can export hundreds of modules, which can cause performance issues in development and production.
### Packages with many exports
You can optimize how these packages are imported by adding the [`optimizePackageImports`](/docs/app/api-reference/config/next-config-js/optimizePackageImports) option to your `next.config.js`. This option will only load the modules you _actually_ use, while still giving you the convenience of writing import statements with many named exports.
If you're using a package that exports hundreds of modules (such as icon and utility libraries), you can optimize how those imports are resolved using the [`optimizePackageImports`](/docs/app/api-reference/config/next-config-js/optimizePackageImports) option in your `next.config.js` file. This option will only load the modules you _actually_ use, while still giving you the convenience of writing import statements with many named exports.
```js filename="next.config.js"
/** @type {import('next').NextConfig} */
......@@ -70,69 +136,144 @@ const nextConfig = {
module.exports = nextConfig
```
Next.js also optimizes some libraries automatically, thus they do not need to be included in the optimizePackageImports list. See the [full list](/docs/app/api-reference/config/next-config-js/optimizePackageImports).
> **Good to know:** Next.js also optimizes some libraries automatically, thus they do not need to be included in the `optimizePackageImports` list. See the [full list](/docs/app/api-reference/config/next-config-js/optimizePackageImports) of supported packages.
### Heavy client workloads
A common cause of large client bundles is doing expensive rendering work in Client Components. This often happens with libraries that exist only to transform data into UI, such as syntax highlighting, chart rendering, or markdown parsing.
If that work does not require browser APIs or user interaction, it can be run in a Server Component.
In this example, a prism based highlighter runs in a Client Component. Even though the final output is just a `<code>` block, the entire highlighting library is bundled into the client JavaScript bundle:
```tsx filename="app/blog/[slug]/page.tsx"
'use client'
import Highlight from 'prism-react-renderer'
import theme from 'prism-react-renderer/themes/github'
export default function Page() {
const code = `export function hello() {
console.log("hi")
}`
return (
<article>
<h1>Blog Post Title</h1>
{/* The prism package and its tokenization logic are shipped to the client */}
<Highlight code={code} language="tsx" theme={theme}>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<pre className={className} style={style}>
<code>
{tokens.map((line, i) => (
<div key={i} {...getLineProps({ line })}>
{line.map((token, key) => (
<span key={key} {...getTokenProps({ token })} />
))}
</div>
))}
</code>
</pre>
)}
</Highlight>
</article>
)
}
```
<PagesOnly>
This increases bundle size because the client must download and execute the highlighting library, even though the result is static HTML.
## Bundling specific packages
Instead, move the highlighting logic to a Server Component and render the final HTML on the server. The client will only receive the rendered markup.
To bundle specific packages, you can use the [`transpilePackages`](/docs/app/api-reference/config/next-config-js/transpilePackages) option in your `next.config.js`. This option is useful for bundling external packages that are not pre-bundled, for example, in a monorepo or imported from `node_modules`.
```tsx filename="app/blog/[slug]/page.tsx"
import { codeToHtml } from 'shiki'
export default async function Page() {
const code = `export function hello() {
console.log("hi")
}`
// The Shiki package runs on the server and is never bundled for the client.
const highlightedHtml = await codeToHtml(code, {
lang: 'tsx',
theme: 'github-dark',
})
return (
<article>
<h1>Blog Post Title</h1>
{/* Client receives plain markup */}
<pre>
<code dangerouslySetInnerHTML={{ __html: highlightedHtml }} />
</pre>
</article>
)
}
```
<AppOnly>
### Opting specific packages out of bundling
Packages imported inside Server Components and Route Handlers are automatically bundled by Next.js.
You can opt specific packages out of bundling using the [`serverExternalPackages`](/docs/app/api-reference/config/next-config-js/serverExternalPackages) option in your `next.config.js`.
```js filename="next.config.js"
/** @type {import('next').NextConfig} */
const nextConfig = {
transpilePackages: ['package-name'],
serverExternalPackages: ['package-name'],
}
module.exports = nextConfig
```
## Bundling all packages
</AppOnly>
<PagesOnly>
### External packages that aren't pre-bundled
To automatically bundle all packages (default behavior in the App Router), you can use the [`bundlePagesRouterDependencies`](/docs/pages/api-reference/config/next-config-js/bundlePagesRouterDependencies) option in your `next.config.js`.
By default, packages imported into your application are not bundled. This can impact performance if external packages are not pre-bundled, for example, if imported from a monorepo or `node_modules`.
To bundle specific packages, you can use the [`transpilePackages`](/docs/app/api-reference/config/next-config-js/transpilePackages) option in your `next.config.js`.
```js filename="next.config.js"
/** @type {import('next').NextConfig} */
const nextConfig = {
bundlePagesRouterDependencies: true,
transpilePackages: ['package-name'],
}
module.exports = nextConfig
```
## Opting specific packages out of bundling
If you have the [`bundlePagesRouterDependencies`](/docs/pages/api-reference/config/next-config-js/bundlePagesRouterDependencies) option enabled, you can opt specific packages out of automatic bundling using the [`serverExternalPackages`](/docs/pages/api-reference/config/next-config-js/serverExternalPackages) option in your `next.config.js`:
To automatically bundle all packages, you can use the [`bundlePagesRouterDependencies`](/docs/pages/api-reference/config/next-config-js/bundlePagesRouterDependencies) option in your `next.config.js`.
```js filename="next.config.js"
/** @type {import('next').NextConfig} */
const nextConfig = {
// Automatically bundle external packages in the Pages Router:
bundlePagesRouterDependencies: true,
// Opt specific packages out of bundling for both App and Pages Router:
serverExternalPackages: ['package-name'],
}
module.exports = nextConfig
```
</PagesOnly>
### Opting specific packages out of bundling
<AppOnly>
## Opting specific packages out of bundling
Since packages imported inside Server Components and Route Handlers are automatically bundled by Next.js, you can opt specific packages out of bundling using the [`serverExternalPackages`](/docs/app/api-reference/config/next-config-js/serverExternalPackages) option in your `next.config.js`.
If you identify packages that shouldn't be in the bundle, you can opt specific packages out of automatic bundling using the [`serverExternalPackages`](/docs/pages/api-reference/config/next-config-js/serverExternalPackages) option in your `next.config.js`:
```js filename="next.config.js"
/** @type {import('next').NextConfig} */
const nextConfig = {
// Automatically bundle external packages:
bundlePagesRouterDependencies: true,
// Opt specific packages out of bundling:
serverExternalPackages: ['package-name'],
}
module.exports = nextConfig
```
Next.js includes a list of popular packages that currently are working on compatibility and automatically opt-ed out. See the [full list](/docs/app/api-reference/config/next-config-js/serverExternalPackages).
</AppOnly>
</PagesOnly>
......@@ -142,7 +142,7 @@ Before going to production, you can run `next build` to build your application l
### Analyzing bundles
Use the [`@next/bundle-analyzer` plugin](/docs/app/guides/package-bundling#analyzing-javascript-bundles) to analyze the size of your JavaScript bundles and identify large modules and dependencies that might be impacting your application's performance.
Use the [`@next/bundle-analyzer` plugin](/docs/app/guides/package-bundling#nextbundle-analyzer-for-webpack) to analyze the size of your JavaScript bundles and identify large modules and dependencies that might be impacting your application's performance.
Additionally, the following tools can help you understand the impact of adding new dependencies to your application:
......
---
title: optimizePackageImports
description: API Reference for optimizePackageImports Next.js Config Option
version: experimental
---
{/* The content of this doc is shared between the app and pages router. You can use the `<PagesOnly>Content</PagesOnly>` component to add content that is specific to the Pages Router. Any shared content should not be wrapped in a component. */}
......
......@@ -215,7 +215,7 @@ By default, the command starts a local server after analysis completes, allowing
- View the full import chain showing why a module is included
- Trace imports across server-to-client component boundaries and dynamic imports
See [Package Bundling](/docs/app/guides/package-bundling) for optimization strategies.
See [Package Bundling](/docs/app/guides/package-bundling#optimizing-large-bundles) for optimization strategies.
To write the analysis output to disk without starting the server, use the `--output` flag. The output is written to `.next/diagnostics/analyze` and contains static files that can be copied elsewhere or shared with others:
......
......@@ -2,6 +2,7 @@
title: optimizePackageImports
description: API Reference for optimizePackageImports Next.js Config Option
source: app/api-reference/config/next-config-js/optimizePackageImports
version: experimental
---
{/* DO NOT EDIT. The content of this doc is generated from the source above. To edit the content of this page, navigate to the source page in your editor. You can use the `<PagesOnly>Content</PagesOnly>` component to add content that is specific to the Pages Router. Any shared content should not be wrapped in a component. */}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment