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 = { /** * 放在 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"], 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);