Skip to the solution
MediumFrontend6 min read

What is next.config.js in Next.js and what are common configurations?

596 views

The problem

Question presented to candidate: "What goes into next.config.js, which options do you most often touch, and how do you catch mistakes in it?"

What a strong answer should cover:

  • next.config.ts (or .js / .mjs) exports an object or function that configures the build and the server; TypeScript config gets the NextConfig type.
  • Common options: images (remotePatterns, formats, qualities), redirects, rewrites, headers, output, serverExternalPackages, turbopack, cacheComponents and cacheLife, reactCompiler, experimental.
  • It is loaded by the Node.js server and build, not bundled for the browser; env values used there are read at build or start.
  • Next.js validates it: unknown or moved keys print warnings with the new name.
  • It can export a function that receives the phase (development, build, production server) to vary settings.

Clarifying questions expected:

  • "Which Next.js version?" β€” keys move between versions (for example serverComponentsExternalPackages).
  • "Is this for the build or runtime behaviour?" β€” some options only affect one of them.

Code / implementation expected: Yes β€” a typed config with the common options.

  • nextjs
  • next-config
  • configuration
  • build

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Read the code

A typed config with common options
// next.config.ts
import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  output: "standalone",
  images: { remotePatterns: [{ protocol: "https", hostname: "cdn.example.com" }] },
  serverExternalPackages: ["@acme/native-pdf"],
  cacheComponents: true,
  partialPrefetching: true, // 16.4 warns when cacheComponents is set without it
  async redirects() {
    return [{ source: "/old-blog/:slug", destination: "/blog/:slug", permanent: true }];
  },
  async headers() {
    return [{ source: "/(.*)", headers: [{ key: "X-Frame-Options", value: "DENY" }] }];
  },
};

export default nextConfig;

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 63 of 129 in Next.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Next.js questions
Up nextMedium

How do you show navigation loading feedback with useLinkStatus in Next.js?

Continue