Next.js Discord

Discord Forum

Getting Cors error on feature bracnhes

Unanswered
Smaller yellow ant posted this in #help-forum
Open in Discord
Smaller yellow antOP
so we have next 14 in the company and it has 2 apis , everything i do with them for not getting cores error keeps getting the error, i tried middleware, next js config, api config, none of them could solve this cores issue, the issue is we cant add every feature branch we have to the list of allowed orgin so i tried using " * " none of them work , pls any one knows the solution?

const handleCORS = (req: NextRequest, res: NextResponse) => {
  // Add CORS headers
  res.headers.set("Access-Control-Allow-Origin", "*");
  res.headers.set("Access-Control-Allow-Methods", "GET,DELETE,PATCH,POST,PUT");
  res.headers.set(
    "Access-Control-Allow-Headers",
    "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version",
  );

  // Handle preflight OPTIONS request
  if (req.method === "OPTIONS") {
    return new NextResponse(null, { status: 200 });
  }

  return res;
};

4 Replies