next.config.mjs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 verison 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. source: "/api/proxy/google/:path*",
  65. destination: "https://generativelanguage.googleapis.com/:path*",
  66. },
  67. {
  68. source: "/api/proxy/openai/:path*",
  69. destination: "https://api.openai.com/:path*",
  70. },
  71. {
  72. source: "/google-fonts/:path*",
  73. destination: "https://fonts.googleapis.com/:path*",
  74. },
  75. {
  76. source: "/sharegpt",
  77. destination: "https://sharegpt.com/api/conversations",
  78. },
  79. ];
  80. return {
  81. beforeFiles: ret,
  82. };
  83. };
  84. }
  85. export default nextConfig;