How do you analyze and fix slow build times in Next.js?

Measure first (build logs show per-phase timings, plus --experimental-cpu-prof and Turbopack traces), then fix the slowest phase: enable and persist Turbopack's filesystem cache in CI, avoid prerendering more pages than needed, speed up data fetching during static generation, separate type checking, and trim heavy dependencies and plugins.

Measure

  • next build prints timings for compilation, type checking, collecting page data and generating static pages (with the worker count, 16.1+).
  • next build --experimental-cpu-prof writes CPU profiles to .next-profiles/.
  • Turbopack tracing (see the local development guide) for compiler hot spots.
  • --debug-build-paths="app/blog/**" builds a subset to iterate quickly.

Common fixes

  1. Use Turbopack (default in 16). A custom webpack() config forces --webpack; port it to turbopack options.
  2. Cache between CI runs: persist .next/cache. Turbopack's filesystem cache for builds is on by default since 16.3 and made CI builds up to 5.5x faster in Vercel's measurements.
  3. Prerender fewer pages at build: return only popular paths from generateStaticParams and let the rest generate on demand (ISR or App Shell).
  4. Faster data during prerender: batch or cache CMS requests with "use cache"; slow APIs multiply across thousands of pages. Tune staticGeneration options (concurrency, retries).
  5. Type checking: run tsc in a parallel CI job, or try TypeScript 7 (native, much faster) with useTypeScriptCli support (16.3).
  6. Linting no longer runs in next build since 16; run ESLint or Biome separately.
  7. Babel and the React Compiler: the Babel-based compiler slows builds; try experimental.turbopackRustReactCompiler (16.3+).
  8. Dependencies: optimizePackageImports for barrel packages, serverExternalPackages for heavy server-only libraries.
  9. Monorepos: Turborepo remote caching skips unchanged apps entirely.

Code example

tsx
1// next.config.ts
2import type { NextConfig } from "next";
3
4const nextConfig: NextConfig = {
5 // Persistent Turbopack caches (defaults in recent 16.x, shown for clarity)
6 experimental: {
7 turbopackFileSystemCacheForBuild: true,
8 optimizePackageImports: ["@acme/icons"],
9 },
10 serverExternalPackages: ["@prisma/client", "sharp"],
11};
12export default nextConfig;
13
14// app/blog/[slug]/page.tsx - prerender only the top pages at build time
15// export async function generateStaticParams() {
16// const top = await getPosts({ orderBy: "views", take: 100 });
17// return top.map((p) => ({ slug: p.slug }));
18// }
19
20// CI (GitHub Actions) - keep the cache between runs
21// - uses: actions/cache@v4
22// with: { path: .next/cache, key: next-${{ hashFiles('package-lock.json') }}-${{ github.sha }},
23// restore-keys: next-${{ hashFiles('package-lock.json') }}- }

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app