Rewrites not working on Vercel (in production) NextJS

Viewed 2269

I have been trying to get Rewrites working in NextJS for my API Paths. It was to avoid CORS issues.

I followed the solution from: NextJs CORS issue.

It is working on localhost but does not work in a production environment (I was deploying on Vercel itself).

I basically tried with all the types of rewrites:

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,
        },
      ],
    };
  },

This rewrite works on localhost but on production, the rewrite stops working and the API calls go to /api/:path* itself.

1 Answers

The /api path is reserved for their Serverless Functions. Changing the source path to something else would resolve the issue.

Related