重写不适用于 Vercel(生产中)NextJS

Rewrites not working on Vercel (in production) NextJS

我一直在尝试让 Rewrites 在 NextJS 中为我​​的 API 路径工作。这是为了避免 CORS 问题。

我遵循了以下解决方案:

它在本地主机上工作,但在生产环境中不工作(我在 Vercel 本身上部署)。

我基本上尝试了所有类型的重写:

async rewrites() {
    return {
      beforeFiles: [
        {
          source: "/api/:path*",
          destination: `https://example.com/api/v1/:path*`,
          basePath: false,
        },
      ],
      afterFiles: [
        {
          source: "/api/:path*",
          destination: `https://example.com/api/v1/:path*`,
          basePath: false,
        },
      ],
      fallback: [
        {
          source: "/api/:path*",
          destination: `https://example.com/api/v1/:path*`,
          basePath: false,
        },
      ],
    };
  },

此重写在本地主机上有效,但在生产环境中,重写停止工作并且 API 调用转到 /api/:path* 本身。

/api 路径是为它们的无服务器函数保留的。将源路径更改为其他路径可以解决问题。