next.config.mjs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. },
  30. };
  31. const CorsHeaders = [
  32. { key: "Access-Control-Allow-Credentials", value: "true" },
  33. { key: "Access-Control-Allow-Origin", value: "*" },
  34. {
  35. key: "Access-Control-Allow-Methods",
  36. value: "*",
  37. },
  38. {
  39. key: "Access-Control-Allow-Headers",
  40. value: "*",
  41. },
  42. {
  43. key: "Access-Control-Max-Age",
  44. value: "86400",
  45. },
  46. ];
  47. if (mode !== "export") {
  48. nextConfig.headers = async () => {
  49. return [
  50. {
  51. source: "/api/:path*",
  52. headers: CorsHeaders,
  53. },
  54. ];
  55. };
  56. nextConfig.rewrites = async () => {
  57. const ret = [
  58. // adjust for previous version directly using "/api/proxy/" as proxy base route
  59. // {
  60. // source: "/api/proxy/v1/:path*",
  61. // destination: "https://api.openai.com/v1/:path*",
  62. // },
  63. {
  64. // https://{resource_name}.openai.azure.com/openai/deployments/{deploy_name}/chat/completions
  65. source:
  66. "/api/proxy/azure/:resource_name/deployments/:deploy_name/:path*",
  67. destination:
  68. "https://:resource_name.openai.azure.com/openai/deployments/:deploy_name/:path*",
  69. },
  70. {
  71. source: "/api/proxy/google/:path*",
  72. destination: "https://generativelanguage.googleapis.com/:path*",
  73. },
  74. {
  75. source: "/api/proxy/openai/:path*",
  76. destination: "https://api.openai.com/:path*",
  77. },
  78. {
  79. source: "/api/proxy/anthropic/:path*",
  80. destination: "https://api.anthropic.com/:path*",
  81. },
  82. {
  83. source: "/google-fonts/:path*",
  84. destination: "https://fonts.googleapis.com/:path*",
  85. },
  86. {
  87. source: "/sharegpt",
  88. destination: "https://sharegpt.com/api/conversations",
  89. },
  90. {
  91. source: "/api/proxy/alibaba/:path*",
  92. destination: "https://dashscope.aliyuncs.com/api/:path*",
  93. },
  94. ];
  95. return {
  96. beforeFiles: ret,
  97. };
  98. };
  99. }
  100. export default nextConfig;