next.config.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import type { NextConfig } from "next";
  2. import createNextIntlPlugin from "next-intl/plugin";
  3. /** 开发时代理真实后端,避免浏览器直连局域网 IP 时的 CORS。与 .env.development 中 API_PROXY_TARGET 一致。 */
  4. const apiProxyTarget =
  5. process.env.API_PROXY_TARGET?.replace(/\/$/, "") ?? "";
  6. const remittanceProxyTarget =
  7. process.env.API_PROXY_TARGET_REMITTANCE?.replace(/\/$/, "") ?? "";
  8. const nextConfig: NextConfig = {
  9. /**
  10. * 放在 Nginx/Caddy 等反代后面时开启:用客户端的 Host / 转发头构造 URL,
  11. * 避免 Next 把内部监听端口(如 `next start` 默认的 3000)写进重定向,
  12. * 否则浏览器会跳到 `jinclab.com:3000` 且公网 3000 通常不可达。
  13. * 反代需传:`Host`、`X-Forwarded-Proto`(及按需 `X-Forwarded-Host`),且不要把上游端口写进 Host。
  14. */
  15. // Next 16 类型里未声明,运行时仍读取(见 node_modules/next/.../resolve-routes.js)
  16. experimental: {
  17. trustHostHeader: true,
  18. } as NextConfig["experimental"],
  19. async rewrites() {
  20. const rewrites = [];
  21. if (apiProxyTarget && /^https?:\/\//i.test(apiProxyTarget)) {
  22. rewrites.push({
  23. source: "/api-backend/:path*",
  24. destination: `${apiProxyTarget}/:path*`,
  25. });
  26. }
  27. if (remittanceProxyTarget && /^https?:\/\//i.test(remittanceProxyTarget)) {
  28. rewrites.push({
  29. source: "/api-backend-remittance/:path*",
  30. destination: `${remittanceProxyTarget}/:path*`,
  31. });
  32. }
  33. return rewrites;
  34. },
  35. };
  36. const withNextIntl = createNextIntlPlugin("./src/i18n/request.ts");
  37. export default withNextIntl(nextConfig);