next.config.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. * 开发模式:用局域网 IP(如 http://192.168.0.28:3000)访问时,Next 16 默认只允许 localhost
  11. * 加载 /_next/*,否则 JS/CSS/RSC 会 403,页面只剩 HTML 壳子。局域网段可按需增减。
  12. */
  13. allowedDevOrigins: ["192.168.*.*", "10.*.*.*", "172.*.*.*"],
  14. /**
  15. * 放在 Nginx/Caddy 等反代后面时开启:用客户端的 Host / 转发头构造 URL,
  16. * 避免 Next 把内部监听端口(如 `next start` 默认的 3000)写进重定向,
  17. * 否则浏览器会跳到 `jinclab.com:3000` 且公网 3000 通常不可达。
  18. * 反代需传:`Host`、`X-Forwarded-Proto`(及按需 `X-Forwarded-Host`),且不要把上游端口写进 Host。
  19. */
  20. // Next 16 类型里未声明,运行时仍读取(见 node_modules/next/.../resolve-routes.js)
  21. experimental: {
  22. trustHostHeader: true,
  23. } as NextConfig["experimental"],
  24. /**
  25. * 必须在配置层做根路径重定向:`app/page.tsx` + permanentRedirect 会被预渲染为静态 ○ /,
  26. * 线上容易出现「必须手输 /zh」;redirects 在路由层优先匹配,且 destination 为相对路径,不会错写成 :3000。
  27. */
  28. async redirects() {
  29. return [
  30. {
  31. source: "/",
  32. destination: "/zh",
  33. permanent: true,
  34. },
  35. ];
  36. },
  37. async rewrites() {
  38. const rewrites = [];
  39. if (apiProxyTarget && /^https?:\/\//i.test(apiProxyTarget)) {
  40. rewrites.push({
  41. source: "/api-backend/:path*",
  42. destination: `${apiProxyTarget}/:path*`,
  43. });
  44. }
  45. if (remittanceProxyTarget && /^https?:\/\//i.test(remittanceProxyTarget)) {
  46. rewrites.push({
  47. source: "/api-backend-remittance/:path*",
  48. destination: `${remittanceProxyTarget}/:path*`,
  49. });
  50. }
  51. return rewrites;
  52. },
  53. };
  54. const withNextIntl = createNextIntlPlugin("./src/i18n/request.ts");
  55. export default withNextIntl(nextConfig);