next.config.mjs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. const basePath = process.env.BASE_PATH ?? ''
  7. /** @type {import('next').NextConfig} */
  8. const nextConfig = {
  9. basePath: basePath,
  10. webpack(config) {
  11. config.module.rules.push({
  12. test: /\.svg$/,
  13. use: ["@svgr/webpack"],
  14. });
  15. if (disableChunk) {
  16. config.plugins.push(
  17. new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }),
  18. );
  19. }
  20. config.resolve.fallback = {
  21. child_process: false,
  22. };
  23. return config;
  24. },
  25. output: mode,
  26. images: {
  27. unoptimized: mode === "export",
  28. },
  29. experimental: {
  30. forceSwcTransforms: true,
  31. },
  32. };
  33. const CorsHeaders = [
  34. { key: "Access-Control-Allow-Credentials", value: "true" },
  35. { key: "Access-Control-Allow-Origin", value: "*" },
  36. {
  37. key: "Access-Control-Allow-Methods",
  38. value: "*",
  39. },
  40. {
  41. key: "Access-Control-Allow-Headers",
  42. value: "*",
  43. },
  44. {
  45. key: "Access-Control-Max-Age",
  46. value: "86400",
  47. },
  48. ];
  49. if (mode !== "export") {
  50. nextConfig.headers = async () => {
  51. return [
  52. {
  53. source: "/api/:path*",
  54. headers: CorsHeaders,
  55. },
  56. ];
  57. };
  58. nextConfig.rewrites = async () => {
  59. const ret = [
  60. {
  61. source: "/api/proxy/:path*",
  62. destination: "https://api.openai.com/:path*",
  63. },
  64. {
  65. source: "/google-fonts/:path*",
  66. destination: "https://fonts.googleapis.com/:path*",
  67. },
  68. {
  69. source: "/sharegpt",
  70. destination: "https://sharegpt.com/api/conversations",
  71. },
  72. ];
  73. return {
  74. beforeFiles: ret,
  75. };
  76. };
  77. }
  78. export default nextConfig;