next.config.mjs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const mode = process.env.BUILD_MODE ?? "standalone";
  2. console.log("[Next] build mode", mode);
  3. /** @type {import('next').NextConfig} */
  4. const nextConfig = {
  5. webpack(config) {
  6. config.module.rules.push({
  7. test: /\.svg$/,
  8. use: ["@svgr/webpack"],
  9. });
  10. return config;
  11. },
  12. output: mode,
  13. images: {
  14. unoptimized: mode === "export",
  15. },
  16. };
  17. if (mode !== "export") {
  18. nextConfig.headers = async () => {
  19. return [
  20. {
  21. source: "/api/:path*",
  22. headers: [
  23. { key: "Access-Control-Allow-Credentials", value: "true" },
  24. { key: "Access-Control-Allow-Origin", value: "*" },
  25. {
  26. key: "Access-Control-Allow-Methods",
  27. value: "*",
  28. },
  29. {
  30. key: "Access-Control-Allow-Headers",
  31. value: "*",
  32. },
  33. {
  34. key: "Access-Control-Max-Age",
  35. value: "86400",
  36. },
  37. ],
  38. },
  39. ];
  40. };
  41. nextConfig.rewrites = async () => {
  42. const ret = [
  43. {
  44. source: "/api/proxy/:path*",
  45. destination: "https://api.openai.com/:path*",
  46. },
  47. {
  48. source: "/google-fonts/:path*",
  49. destination: "https://fonts.googleapis.com/:path*",
  50. },
  51. {
  52. source: "/sharegpt",
  53. destination: "https://sharegpt.com/api/conversations",
  54. },
  55. ];
  56. const apiUrl = process.env.API_URL;
  57. if (apiUrl) {
  58. console.log("[Next] using api url ", apiUrl);
  59. ret.push({
  60. source: "/api/:path*",
  61. destination: `${apiUrl}/:path*`,
  62. });
  63. }
  64. return {
  65. beforeFiles: ret,
  66. };
  67. };
  68. }
  69. export default nextConfig;