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 setup | Next.js 16 |
|---|---|
| next.config.js without types | next.config.ts and the NextConfig type |
| The --turbopack flag for Turbopack in dev | Turbopack is the default in both dev and build |
| The experimental.turbo key | A top-level turbopack key |
| Slow webpack builds | Fast Turbopack builds and Fast Refresh |
| Webpack as the only option | Opt out with --webpack if you need to |
3. Where is it used?
| Need | Config key |
|---|---|
| Allow remote images | images.remotePatterns |
| Send old URLs to new ones | redirects() |
| Add security headers | headers() |
| A small output for Docker | output: "standalone" |
| Turn on Cache Components | cacheComponents: true |
| Turn SVGs into React components | turbopack.rules |
| Change an import alias | turbopack.resolveAlias |
4. How does it work?
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
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
{
"scripts": {
"dev": "next dev",
"build": "next build",
"build:webpack": "next build --webpack",
"start": "next start"
}
}How does the code work?
| Item | Meaning |
|---|---|
NextConfig type | The editor shows an error for a wrong key or value |
reactCompiler | Turns 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.rules | A webpack loader for a file pattern, and as says what type the output is |
turbopack.resolveAlias | Maps imports of one package to another package |
--webpack | Uses 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
| Stage | Chapters |
|---|---|
| Before | Installation & Project Structure, What's New in Next.js 16 |
| Alongside | Environment Variables, Image Optimization |
| After | Performance & Core Web Vitals, Deployment |
Final quick revision
| Point | Detail |
|---|---|
| Config file | next.config.ts and the NextConfig type |
| Default bundler | Turbopack, in both dev and build |
| Opt out | next dev --webpack, next build --webpack |
| Turbopack options | Top-level turbopack: rules, resolveAlias, resolveExtensions, root |
| Routing config | redirects(), rewrites(), headers() |