Skip to content
ProductionChapter 31 / 34Intermediate5 min read

next.config & Turbopack

next.config turbopack setup Next.js 16 mein, typed next.config.ts, redirects, headers, turbopack key aur --webpack opt out, Roman Urdu mein samjhein.

On this page

1. next.config & Turbopack kya hain?

next.config.ts mein aap images, redirects, headers, output mode, React Compiler aur bundler options jaisi settings rakhte hain. Turbopack wo engine hai jo aap ka code bundle kar ke browser aur server ke liye output banata hai.

2. Kyun zaroori hai?

Har project ki zaroorat alag hoti hai: kisi ko remote images chahiye, kisi ko purane URLs redirect karne hain, kisi ko SVG ko component bana kar import karna hai. Yeh sab ek jagah, typed config mein hota hai.

Purana setupNext.js 16
next.config.js bina typesnext.config.ts aur NextConfig type
Dev mein Turbopack ke liye --turbopack flagTurbopack dev aur build dono mein default
experimental.turbo keyTop level turbopack key
Webpack build slowTurbopack tez builds aur Fast Refresh
Webpack hi akela optionZaroorat ho to --webpack se opt out

3. Kahan use hota hai?

ZarooratConfig key
Remote images allow karnaimages.remotePatterns
Purane URLs ko naye par bhejnaredirects()
Security headers laganaheaders()
Docker ke liye chhota outputoutput: "standalone"
Cache Components on karnacacheComponents: true
SVG ko React component bananaturbopack.rules
Import alias badalnaturbopack.resolveAlias

4. Kaise kaam karta hai?

Config se bundle tak
next dev ya next build chalao
Next.js next.config.ts parhta hai
Turbopack config ke rules aur aliases lagata hai
Sirf zaroori modules compile, results cache
Browser aur server ke liye output

Turbopack incremental hai: jo file badli sirf wahi dobara compile hoti hai. Is liye bare projects mein bhi dev server jaldi start hota hai aur changes foran dikhte hain.

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 aur webpack opt out

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

Code kaise kaam kar raha hai?

CheezMatlab
NextConfig typeGalat key ya value par editor mein error
reactCompilerReact Compiler on (babel-plugin-react-compiler install chahiye)
redirects()permanent: true se 308 redirect
headers()Har matching route ke response par headers
turbopack.rulesFile pattern par webpack loader, as batata hai output kis type ka hai
turbopack.resolveAliasEk package ka import doosre package par map
--webpackIs command ke liye Turbopack ki jagah webpack

6. Common mistakes

Galati: Purani experimental.turbo key use karna

Why? Yeh purana naam hai, Next.js 15.3 se config top level turbopack key mein shift ho gayi.

Behtar: Settings ko turbopack key mein rakho, ya codemod next-experimental-turbo-to-turbopack chalao.

Galati: Custom webpack function rakh kar umeed karna ke Turbopack use parhega

Why? Turbopack webpack() config function nahi chalata, is liye aisi config ke saath build fail ho sakta hai ya behaviour alag hota hai.

Behtar: Logic ko turbopack.rules aur resolveAlias mein migrate karo, ya waqti taur par --webpack flag use karo.

Galati: Config change ke baad dev server restart na karna

Why? next.config.ts start par parhi jati hai, chalte server par naye redirects ya images settings apply nahi hote.

Behtar: Config badalne ke baad next dev dobara chalao.

Galati: Secrets next.config mein hardcode karna

Why? Config repo mein commit hoti hai aur kuch values client bundle tak bhi pohanch sakti hain.

Behtar: Secrets .env files aur process.env mein rakho.

Interview tip

"Turbopack webpack se tez kyun hai?" Teen wajah: Rust mein likha hai, function level par incremental caching karta hai, aur dev mein sirf woh routes compile karta hai jo request hue.

7. Pehle aur baad mein kya seekhein?

Final quick revision

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