配置与环境变量 next.config
next.config.js
常见配置示例:
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
images: {
remotePatterns: [
{ protocol: "https", hostname: "images.example.com" },
],
},
redirects: async () => [
{ source: "/old", destination: "/new", permanent: true },
],
rewrites: async () => [
{ source: "/docs/:path*", destination: "https://docs.example.com/:path*" },
],
experimental: {
// 根据需要开启实验特性
},
};
module.exports = nextConfig;
环境变量
- .env.local:本地开发
- .env.production:生产环境
- 约定:
- NEXT_PUBLIC_ 前缀的变量可在客户端读取。
- 其他变量仅在服务端可用。
NEXT_PUBLIC_API_BASE=https://api.example.com
SECRET_KEY=xxxx
构建输出与自托管
- 输出独立可运行的构建:
// next.config.js
module.exports = {
output: "standalone",
};