Skip to content
ProductionChapter 31 / 34Intermediate5 min read

next.config & Turbopack

Set up next.config and Turbopack in Next.js 16 - typed next.config.ts, redirects, headers, the turbopack key and how to opt out with --webpack.

On this page

1. What are next.config and Turbopack?

In next.config.ts you put settings like images, redirects, headers, output mode, the React Compiler and bundler options. Turbopack is the engine that bundles your code and produces the output for the browser and the server.

2. Why does it matter?

Every project has different needs: some need remote images, some need to redirect old URLs, some want to import SVGs as components. All of this lives in one place, in a typed config.

Old setupNext.js 16
next.config.js without typesnext.config.ts and the NextConfig type
The --turbopack flag for Turbopack in devTurbopack is the default in both dev and build
The experimental.turbo keyA top-level turbopack key
Slow webpack buildsFast Turbopack builds and Fast Refresh
Webpack as the only optionOpt out with --webpack if you need to

3. Where is it used?

NeedConfig key
Allow remote imagesimages.remotePatterns
Send old URLs to new onesredirects()
Add security headersheaders()
A small output for Dockeroutput: "standalone"
Turn on Cache ComponentscacheComponents: true
Turn SVGs into React componentsturbopack.rules
Change an import aliasturbopack.resolveAlias

4. How does it work?

From config to bundle
Run next dev or next build
Next.js reads next.config.ts
Turbopack applies the config's rules and aliases
Only the needed modules are compiled, and results are cached
Output for the browser and the server

Turbopack is incremental: only the file that changed is compiled again. That is why the dev server starts quickly even in big projects and changes show up right away.

5. Code example

Typed next.config.ts

ts
import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  reactCompiler: true,
  images: {
    remotePatterns: [new URL("https://cdn.example.com/**")],
  },
  async redirects() {
    return [
      { source: "/old-blog/:slug", destination: "/blog/:slug", permanent: true },
    ];
  },
  async headers() {
    return [
      {
        source: "/:path*",
        headers: [{ key: "X-Frame-Options", value: "DENY" }],
      },
    ];
  },
  turbopack: {
    rules: {
      "*.svg": {
        loaders: ["@svgr/webpack"],
        as: "*.js",
      },
    },
    resolveAlias: {
      underscore: "lodash",
    },
  },
};

export default nextConfig;

Scripts and the webpack opt out

json
{
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "build:webpack": "next build --webpack",
    "start": "next start"
  }
}

How does the code work?

ItemMeaning
NextConfig typeThe editor shows an error for a wrong key or value
reactCompilerTurns on the React Compiler (needs babel-plugin-react-compiler installed)
redirects()A 308 redirect with permanent: true
headers()Headers on the response of every matching route
turbopack.rulesA webpack loader for a file pattern, and as says what type the output is
turbopack.resolveAliasMaps imports of one package to another package
--webpackUses webpack instead of Turbopack for this command

6. Common mistakes

Mistake: Using the old experimental.turbo key

Why? This is the old name, and since Next.js 15.3 the config has moved to the top-level turbopack key.

Better: Put the settings in the turbopack key, or run the next-experimental-turbo-to-turbopack codemod.

Mistake: Keeping a custom webpack function and expecting Turbopack to read it

Why? Turbopack does not run the webpack() config function, so a build with such a config can fail or behave differently.

Better: Migrate the logic to turbopack.rules and resolveAlias, or temporarily use the --webpack flag.

Mistake: Not restarting the dev server after a config change

Why? next.config.ts is read at startup, so new redirects or images settings do not apply to a running server.

Better: Run next dev again after changing the config.

Mistake: Hardcoding secrets in next.config

Why? The config is committed to the repo and some values can even reach the client bundle.

Better: Keep secrets in .env files and process.env.

Interview tip

"Why is Turbopack faster than webpack?" Three reasons: it is written in Rust, it does incremental caching at the function level, and in dev it compiles only the routes that were requested.

7. What to learn before and after

Final quick revision

PointDetail
Config filenext.config.ts and the NextConfig type
Default bundlerTurbopack, in both dev and build
Opt outnext dev --webpack, next build --webpack
Turbopack optionsTop-level turbopack: rules, resolveAlias, resolveExtensions, root
Routing configredirects(), rewrites(), headers()