next.config.mjs 1.8 KB

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