next.config.mjs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. {
  59. source: "/api/proxy/:path*",
  60. destination: "https://api.openai.com/:path*",
  61. },
  62. {
  63. source: "/google-fonts/:path*",
  64. destination: "https://fonts.googleapis.com/:path*",
  65. },
  66. {
  67. source: "/sharegpt",
  68. destination: "https://sharegpt.com/api/conversations",
  69. },
  70. ];
  71. return {
  72. beforeFiles: ret,
  73. };
  74. };
  75. }
  76. export default nextConfig;