Next.js Configuration

Overview

Optimizing your Next.js configuration can improve application performance, developer experience, and build efficiency.

next.config.js

Basic Configuration

// next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
  // 输出模式
  output: 'standalone',
  
  // 压缩
  compress: true,
  
  // 严格模式
  reactStrictMode: true,
  
  // SWC 压缩
  swcMinify: true,
}

module.exports = nextConfig

Image Optimization

module.exports = {
  images: {
    domains: ['example.com', 'cdn.example.com'],
    formats: ['image/avif', 'image/webp'],
    deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
    imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
    minimumCacheTTL: 60,
  },
}

Redirects and Rewrites

Redirects

module.exports = {
  async redirects() {
    return [
      {
        source: '/old-blog/:slug',
        destination: '/blog/:slug',
        permanent: true,
      },
      {
        source: '/docs',
        destination: '/docs/getting-started',
        permanent: false,
      },
    ]
  },
}

Rewrites

module.exports = {
  async rewrites() {
    return [
      {
        source: '/api/:path*',
        destination: 'https://api.example.com/:path*',
      },
    ]
  },
}

Header Configuration

module.exports = {
  async headers() {
    return [
      {
        source: '/:path*',
        headers: [
          {
            key: 'X-Frame-Options',
            value: 'DENY',
          },
          {
            key: 'X-Content-Type-Options',
            value: 'nosniff',
          },
          {
            key: 'X-XSS-Protection',
            value: '1; mode=block',
          },
        ],
      },
      {
        source: '/api/:path*',
        headers: [
          {
            key: 'Cache-Control',
            value: 'no-store, must-revalidate',
          },
        ],
      },
    ]
  },
}

Webpack Configuration

module.exports = {
  webpack: (config, { isServer }) => {
    // 自定义 webpack 配置
    if (!isServer) {
      config.resolve.fallback = {
        fs: false,
        net: false,
        tls: false,
      }
    }
    
    return config
  },
}

Experimental Features

module.exports = {
  experimental: {
    // 服务端操作
    serverActions: true,
    
    // 部分预渲染
    ppr: true,
    
    // 优化 CSS
    optimizeCss: true,
    
    // 优化包导入
    optimizePackageImports: ['lodash', 'date-fns'],
  },
}

Best Practices

1. Environment-Specific Configuration

const isProd = process.env.NODE_ENV === 'production'

module.exports = {
  compress: isProd,
  reactStrictMode: !isProd,
  swcMinify: isProd,
}

2. Modular Configuration

// next.config.js
const withBundleAnalyzer = require('@next/bundle-analyzer')({
  enabled: process.env.ANALYZE === 'true',
})

module.exports = withBundleAnalyzer({
  // 配置
})

3. TypeScript Configuration

module.exports = {
  typescript: {
    // 生产构建时忽略类型错误(不推荐)
    ignoreBuildErrors: false,
  },
}

Previous Chapter: Next.js Monitoring and Analytics | Next Chapter: Next.js Best Practices