Capítulo 66 de 456
Analyze and shrink server/client bundles with the Turbopack Bundle Analyzer (next experimental-analyze) or @next/bundle-analyzer for Webpack, then fix large bundles via optimizePackageImports, moving heavy rendering work to Server Components, or serverExternalPackages.
next experimental-analyze: built-in Turbopack bundle analyzer (v16.1+) with import tracing, filterable by route/environment/type; --output writes a static report to .next/diagnostics/analyze.@next/bundle-analyzer: Webpack plugin producing a visual size report per package/dependency.optimizePackageImports: next.config.js option that loads only the modules actually used from packages with many named exports (icon/utility libraries), even with barrel-style imports. Some libraries are optimized automatically without needing the list.serverExternalPackages: opts specific packages out of automatic server-side bundling (for Server Components/Route Handlers, which are bundled by default).npx next experimental-analyze
npx next experimental-analyze --output
const nextConfig = {
experimental: {
optimizePackageImports: ['icon-library'],
},
}
module.exports = nextConfig
import { codeToHtml } from 'shiki'
export default async function Page() {
const code = `export function hello() { console.log("hi") }`
const highlightedHtml = await codeToHtml(code, { lang: 'tsx', theme: 'github-dark' })
return (
<article>
<pre><code dangerouslySetInnerHTML={{ __html: highlightedHtml }} /></pre>
</article>
)
}
'use client' com prism-react-renderer) para Server Component com Shiki, cliente recebe só markup estático.const nextConfig = {
serverExternalPackages: ['package-name'],
}
module.exports = nextConfig
optimizePackageImports resolve o problema de "import nomeado de lib gigante" sem mudar o jeito de importar.@next/bundle-analyzer exige ANALYZE=true npm run build; o novo analyzer do Turbopack roda direto via next experimental-analyze.