next.config.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import type { NextConfig } from "next";
  2. // 从环境变量读取后端端口,默认 8080(与后端 main.go 保持一致)
  3. // 如果设置了 NEXT_PUBLIC_BACKEND_PORT,优先使用(用于 Docker 部署等场景)
  4. const backendPort = process.env.NEXT_PUBLIC_BACKEND_PORT || "8080";
  5. const backendHost = process.env.NEXT_PUBLIC_BACKEND_HOST || "localhost";
  6. const nextConfig: NextConfig = {
  7. eslint: {
  8. ignoreDuringBuilds: true, // 临时禁用构建时的 ESLint 检查
  9. },
  10. // 开发环境:代理 API 请求到后端
  11. // 生产环境:由 Nginx 处理,这个配置不会生效(因为生产环境是静态构建)
  12. async rewrites() {
  13. // 只在开发环境启用代理
  14. if (process.env.NODE_ENV === "development") {
  15. return [
  16. // 优先匹配后端 API 路径(这些需要代理到后端)
  17. {
  18. source: "/agent/profile/:path*",
  19. destination: `http://${backendHost}:${backendPort}/agent/profile/:path*`,
  20. },
  21. {
  22. source: "/agent/avatar/:path*",
  23. destination: `http://${backendHost}:${backendPort}/agent/avatar/:path*`,
  24. },
  25. {
  26. source: "/agent/embedding-config",
  27. destination: `http://${backendHost}:${backendPort}/agent/embedding-config`,
  28. },
  29. {
  30. source: "/agent/ai-config/:path*",
  31. destination: `http://${backendHost}:${backendPort}/agent/ai-config/:path*`,
  32. },
  33. // 匹配其他 API 路径(不以 /_next、/agent、/chat 开头的路径)
  34. // 例如:/login, /conversations, /messages 等
  35. {
  36. source: "/:path((?!_next|agent|chat|favicon.ico).*)",
  37. destination: `http://${backendHost}:${backendPort}/:path*`,
  38. },
  39. ];
  40. }
  41. // 生产环境返回空数组,使用相对路径(由 Nginx 处理)
  42. return [];
  43. },
  44. images: {
  45. remotePatterns: [
  46. {
  47. protocol: "http",
  48. hostname: "192.168.124.9",
  49. port: backendPort,
  50. pathname: "/uploads/**",
  51. },
  52. {
  53. protocol: "http",
  54. hostname: "localhost",
  55. port: backendPort,
  56. pathname: "/uploads/**",
  57. },
  58. {
  59. protocol: "http",
  60. hostname: "127.0.0.1",
  61. port: backendPort,
  62. pathname: "/uploads/**",
  63. },
  64. ],
  65. },
  66. };
  67. export default nextConfig;