next.config.mjs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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: "/api/proxy/azure/:resource_name/deployments/:deploy_name/:path*",
  66. destination: "https://:resource_name.openai.azure.com/openai/deployments/:deploy_name/:path*",
  67. },
  68. {
  69. source: "/api/proxy/google/:path*",
  70. destination: "https://generativelanguage.googleapis.com/:path*",
  71. },
  72. {
  73. source: "/api/proxy/openai/:path*",
  74. destination: "https://api.openai.com/:path*",
  75. },
  76. {
  77. source: "/api/proxy/anthropic/:path*",
  78. destination: "https://api.anthropic.com/:path*",
  79. },
  80. {
  81. source: "/google-fonts/:path*",
  82. destination: "https://fonts.googleapis.com/:path*",
  83. },
  84. {
  85. source: "/sharegpt",
  86. destination: "https://sharegpt.com/api/conversations",
  87. },
  88. ];
  89. return {
  90. beforeFiles: ret,
  91. };
  92. };
  93. }
  94. export default nextConfig;