next.config.mjs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. if (mode !== "export") {
  32. nextConfig.headers = async () => {
  33. return [
  34. {
  35. source: "/api/:path*",
  36. headers: [
  37. { key: "Access-Control-Allow-Credentials", value: "true" },
  38. { key: "Access-Control-Allow-Origin", value: "*" },
  39. {
  40. key: "Access-Control-Allow-Methods",
  41. value: "*",
  42. },
  43. {
  44. key: "Access-Control-Allow-Headers",
  45. value: "*",
  46. },
  47. {
  48. key: "Access-Control-Max-Age",
  49. value: "86400",
  50. },
  51. ],
  52. },
  53. ];
  54. };
  55. nextConfig.rewrites = async () => {
  56. const ret = [
  57. {
  58. source: "/api/proxy/:path*",
  59. destination: "https://api.openai.com/:path*",
  60. },
  61. {
  62. source: "/google-fonts/:path*",
  63. destination: "https://fonts.googleapis.com/:path*",
  64. },
  65. {
  66. source: "/sharegpt",
  67. destination: "https://sharegpt.com/api/conversations",
  68. },
  69. ];
  70. const apiUrl = process.env.API_URL;
  71. if (apiUrl) {
  72. console.log("[Next] using api url ", apiUrl);
  73. ret.push({
  74. source: "/api/:path*",
  75. destination: `${apiUrl}/:path*`,
  76. });
  77. }
  78. return {
  79. beforeFiles: ret,
  80. };
  81. };
  82. }
  83. export default nextConfig;