Search code examples
node.jscorsvitevercel

Local Vercel serverless CORS issue


I read every single doc and post but no matter what I do I'm getting

Access to XMLHttpRequest at 'http://localhost:3000/api/test1' from origin
'http://localhost:5173' has been blocked by CORS policy: Response to preflight 
request doesn't pass access control check: It does not have HTTP ok status.

Vue SPA app with Vite and the following Axios request:

await axios.post('http://localhost:3000/api/test1', {})

The api/test1.js contains the exact code from Vercel CORS docs under Enabling CORS in a single Node.js Serverless Function except the last line which is export default allowCors(handler) in my code.

My vercel.json:

// vercel.json

{
  "buildCommand": "vite build --mode vercel",
  "devCommand": "vite --mode vercel --port=$PORT",
  "headers": [
    {
      "source": "/api/(.*)",
      "headers": [
        { "key": "Access-Control-Allow-Credentials", "value": "true" },
        { "key": "Access-Control-Allow-Origin", "value": "*" },
        { "key": "Access-Control-Allow-Methods", "value": "GET,OPTIONS,PATCH,DELETE,POST,PUT" },
        { "key": "Access-Control-Allow-Headers", "value": "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version" }
      ]
    }
  ]
}

And I run Vercel with vercel dev

I also have a test2.js:

// api/test2.js

console.log("1");
export default function handler(request, response) {
  console.log("2");
  return response.status(200).json({ text: 'I am a Serverless Function!' });
}
console.log("3");

which resulting the same error but interestingly the 2 not get logged in the console. Here is the debug log for that request:

> [debug] [2024-03-15T20:01:39.193Z] OPTIONS /api/test2
> [debug] [2024-03-15T20:01:39.194Z] Reading `package.json` file
> [debug] [2024-03-15T20:01:39.194Z] Reading `vercel.json` file
> [debug] [2024-03-15T20:01:39.196Z] Locating files /vercelproject
> [debug] [2024-03-15T20:01:39.197Z] Ignoring .git
> [debug] [2024-03-15T20:01:39.197Z] Ignoring .gitignore
> [debug] [2024-03-15T20:01:39.197Z] Ignoring .vercel
> [debug] [2024-03-15T20:01:39.197Z] Ignoring node_modules
> [debug] [2024-03-15T20:01:39.234Z] Locating files /vercelproject [38ms]
> [debug] [2024-03-15T20:01:39.248Z] Using local env: .env
> [debug] [2024-03-15T20:01:39.252Z] Imported Builder "@vercel/node" from "/pnpm/global/5/.pnpm/@vercel+node@3.0.23/node_modules/@vercel/node"
1
3
> [debug] [2024-03-15T20:01:39.971Z] Proxying to "@vercel/node" dev server (port=50404, pid=67875)
> [debug] [2024-03-15T20:01:40.008Z] Killing builder dev server with PID 67875
> [debug] [2024-03-15T20:01:40.008Z] Running shutdown callback for PID 67875

Vercel cli version: 33.6.1 (latest)


UPDATE 1:

If I change the POST request to a GET one (nothing else) the debug log changes and the 2 get appearing:

1
3
> [debug] [2024-03-15T20:49:19.067Z] Proxying to "@vercel/node" dev server (port=63950, pid=3861)
2
> [debug] [2024-03-15T20:49:19.111Z] Killing builder dev server with PID 3861
> [debug] [2024-03-15T20:49:19.111Z] Running shutdown callback for PID 3861

Solution

  • I just found an issue in vercel's repo with this problem.

    TLDR:
    Problem introduced in vercel version 32.0.2.
    Workarounds:

    • use vercel version 32.0.1
    • convert your POST request to GET