next.config.mjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import webpack from "webpack";
  2. const mode = process.env.BUILD_MODE ?? "standalone";
  3. console.log("[Next] build mode", mode);
  4. const disableChunk = !!process.env.DISABLE_CHUNK || mode === "export";
  5. console.log("[Next] build with chunk: ", !disableChunk);
  6. /** @type {import('next').NextConfig} */
  7. const nextConfig = {
  8. webpack(config) {
  9. config.module.rules.push({
  10. test: /\.svg$/,
  11. use: ["@svgr/webpack"],
  12. });
  13. if (disableChunk) {
  14. config.plugins.push(
  15. new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }),
  16. );
  17. }
  18. config.resolve.fallback = {
  19. child_process: false,
  20. };
  21. return config;
  22. },
  23. output: mode,
  24. images: {
  25. unoptimized: mode === "export",
  26. },
  27. experimental: {
  28. forceSwcTransforms: true,
  29. serverActions: true,
  30. },
  31. };
  32. const CorsHeaders = [
  33. { key: "Access-Control-Allow-Credentials", value: "true" },
  34. { key: "Access-Control-Allow-Origin", value: "*" },
  35. {
  36. key: "Access-Control-Allow-Methods",
  37. value: "*",
  38. },
  39. {
  40. key: "Access-Control-Allow-Headers",
  41. value: "*",
  42. },
  43. {
  44. key: "Access-Control-Max-Age",
  45. value: "86400",
  46. },
  47. ];
  48. if (mode !== "export") {
  49. nextConfig.headers = async () => {
  50. return [
  51. {
  52. source: "/api/:path*",
  53. headers: CorsHeaders,
  54. },
  55. ];
  56. };
  57. nextConfig.rewrites = async () => {
  58. const ret = [
  59. // adjust for previous version directly using "/api/proxy/" as proxy base route
  60. // {
  61. // source: "/api/proxy/v1/:path*",
  62. // destination: "https://api.openai.com/v1/:path*",
  63. // },
  64. {
  65. // https://{resource_name}.openai.azure.com/openai/deployments/{deploy_name}/chat/completions
  66. source:
  67. "/api/proxy/azure/:resource_name/deployments/:deploy_name/:path*",
  68. destination:
  69. "https://:resource_name.openai.azure.com/openai/deployments/:deploy_name/:path*",
  70. },
  71. {
  72. source: "/api/proxy/google/:path*",
  73. destination: "https://generativelanguage.googleapis.com/:path*",
  74. },
  75. {
  76. source: "/api/proxy/openai/:path*",
  77. destination: "https://api.openai.com/:path*",
  78. },
  79. {
  80. source: "/api/proxy/anthropic/:path*",
  81. destination: "https://api.anthropic.com/:path*",
  82. },
  83. {
  84. source: "/google-fonts/:path*",
  85. destination: "https://fonts.googleapis.com/:path*",
  86. },
  87. {
  88. source: "/sharegpt",
  89. destination: "https://sharegpt.com/api/conversations",
  90. },
  91. {
  92. source: "/api/proxy/alibaba/:path*",
  93. destination: "https://dashscope.aliyuncs.com/api/:path*",
  94. },
  95. ];
  96. return {
  97. beforeFiles: ret,
  98. };
  99. };
  100. }
  101. export default nextConfig;