next.config.mjs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. };
  28. if (mode !== "export") {
  29. nextConfig.headers = async () => {
  30. return [
  31. {
  32. source: "/api/:path*",
  33. headers: [
  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. },
  50. ];
  51. };
  52. nextConfig.rewrites = async () => {
  53. const ret = [
  54. {
  55. source: "/api/proxy/:path*",
  56. destination: "https://api.openai.com/:path*",
  57. },
  58. {
  59. source: "/google-fonts/:path*",
  60. destination: "https://fonts.googleapis.com/:path*",
  61. },
  62. {
  63. source: "/sharegpt",
  64. destination: "https://sharegpt.com/api/conversations",
  65. },
  66. ];
  67. const apiUrl = process.env.API_URL;
  68. if (apiUrl) {
  69. console.log("[Next] using api url ", apiUrl);
  70. ret.push({
  71. source: "/api/:path*",
  72. destination: `${apiUrl}/:path*`,
  73. });
  74. }
  75. return {
  76. beforeFiles: ret,
  77. };
  78. };
  79. }
  80. export default nextConfig;