Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrites not working on Vercel (in production) NextJS

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.

like image 764
Y M Avatar asked Nov 15 '25 17:11

Y M


1 Answers

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

like image 94
KenRoda Avatar answered Nov 17 '25 07:11

KenRoda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!