50 lines
1.9 KiB
TypeScript
50 lines
1.9 KiB
TypeScript
import type { NextConfig } from "next";
|
||
import { readFileSync, existsSync } from "fs";
|
||
import { join } from "path";
|
||
|
||
// 构建时自动加载 Server Actions 加密密钥,避免 "Failed to find Server Action" 部署错误
|
||
if (!process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY) {
|
||
const keyPath = join(__dirname, "config", "server-actions.key");
|
||
if (existsSync(keyPath)) {
|
||
process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY = readFileSync(keyPath, "utf8").trim();
|
||
}
|
||
}
|
||
|
||
/** 低配服务器构建:限制并行与 Turbopack 目标内存,避免 pnpm build 把机器拖死 / OOM */
|
||
const buildCpuCount = Math.max(
|
||
1,
|
||
Number.parseInt(process.env.NEXT_BUILD_CPUS ?? "1", 10) || 1,
|
||
);
|
||
const turbopackMemoryMb = Math.max(
|
||
256,
|
||
Number.parseInt(process.env.NEXT_BUILD_TURBOPACK_MEMORY_MB ?? "1536", 10) ||
|
||
1536,
|
||
);
|
||
|
||
const nextConfig: NextConfig = {
|
||
// 预渲染阶段关闭 source map,降低静态生成时内存峰值(需要时可设 NEXT_BUILD_PRERENDER_SOURCE_MAPS=1)
|
||
enablePrerenderSourceMaps:
|
||
process.env.NEXT_BUILD_PRERENDER_SOURCE_MAPS === "1",
|
||
// 局域网访问(如手机 192.168.x.x)时允许跨域请求 _next 资源
|
||
// 需完全重启 dev 服务(Ctrl+C 后 pnpm dev)后生效
|
||
allowedDevOrigins: [
|
||
"192.168.41.222",
|
||
"192.168.41.222:3000",
|
||
"http://192.168.41.222",
|
||
"http://192.168.41.222:3000",
|
||
"http://192.168.41.222:3001",
|
||
],
|
||
experimental: {
|
||
cpus: buildCpuCount,
|
||
memoryBasedWorkersCount: true,
|
||
turbopackMemoryLimit: turbopackMemoryMb * 1024 * 1024,
|
||
// 生产 bundle 默认不生成 Turbopack source map,省内存;需要 Sentry 等可设 NEXT_BUILD_TURBOPACK_SOURCE_MAPS=1
|
||
turbopackSourceMaps:
|
||
process.env.NEXT_BUILD_TURBOPACK_SOURCE_MAPS === "1",
|
||
// 静态页面导出时一次只跑少量页面,降低峰值内存
|
||
staticGenerationMaxConcurrency: 1,
|
||
},
|
||
};
|
||
|
||
export default nextConfig;
|