import type { NextConfig } from "next"; import createNextIntlPlugin from "next-intl/plugin"; /** 开发时代理真实后端,避免浏览器直连局域网 IP 时的 CORS。与 .env.development 中 API_PROXY_TARGET 一致。 */ const apiProxyTarget = process.env.API_PROXY_TARGET?.replace(/\/$/, "") ?? ""; const remittanceProxyTarget = process.env.API_PROXY_TARGET_REMITTANCE?.replace(/\/$/, "") ?? ""; const nextConfig: NextConfig = { /** * 开发模式:用局域网 IP(如 http://192.168.0.28:3000)访问时,Next 16 默认只允许 localhost * 加载 /_next/*,否则 JS/CSS/RSC 会 403,页面只剩 HTML 壳子。局域网段可按需增减。 */ allowedDevOrigins: ["192.168.*.*", "10.*.*.*", "172.*.*.*"], /** * 放在 Nginx/Caddy 等反代后面时开启:用客户端的 Host / 转发头构造 URL, * 避免 Next 把内部监听端口(如 `next start` 默认的 3000)写进重定向, * 否则浏览器会跳到 `jinclab.com:3000` 且公网 3000 通常不可达。 * 反代需传:`Host`、`X-Forwarded-Proto`(及按需 `X-Forwarded-Host`),且不要把上游端口写进 Host。 */ // Next 16 类型里未声明,运行时仍读取(见 node_modules/next/.../resolve-routes.js) experimental: { trustHostHeader: true, } as NextConfig["experimental"], /** * 必须在配置层做根路径重定向:`app/page.tsx` + permanentRedirect 会被预渲染为静态 ○ /, * 线上容易出现「必须手输 /zh」;redirects 在路由层优先匹配,且 destination 为相对路径,不会错写成 :3000。 */ async redirects() { return [ { source: "/", destination: "/zh", permanent: true, }, ]; }, async rewrites() { const rewrites = []; if (apiProxyTarget && /^https?:\/\//i.test(apiProxyTarget)) { rewrites.push({ source: "/api-backend/:path*", destination: `${apiProxyTarget}/:path*`, }); } if (remittanceProxyTarget && /^https?:\/\//i.test(remittanceProxyTarget)) { rewrites.push({ source: "/api-backend-remittance/:path*", destination: `${remittanceProxyTarget}/:path*`, }); } return rewrites; }, }; const withNextIntl = createNextIntlPlugin("./src/i18n/request.ts"); export default withNextIntl(nextConfig);