Files
gitlab-instance-0a899031_no…/next.config.ts
2026-03-27 18:20:36 -05:00

52 lines
2.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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,
// 仅 next devNODE_ENV=development建议开启避免栈帧过滤读到 null source map 控制台报错
// 生产 build 默认关闭以省内存;需要 Sentry 等可设 NEXT_BUILD_TURBOPACK_SOURCE_MAPS=1
turbopackSourceMaps:
process.env.NODE_ENV === "development" ||
process.env.NEXT_BUILD_TURBOPACK_SOURCE_MAPS === "1",
// 静态页面导出时一次只跑少量页面,降低峰值内存
staticGenerationMaxConcurrency: 1,
},
};
export default nextConfig;