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