I'm using GraphQL to communicate between two domains client and server. I have enabled CORS on my API website following the vercel documentation, but it seems to throw a blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
error. I have this code in GraphQL:
function createApolloClient() {
return new ApolloClient({
ssrMode: typeof window === "undefined",
link: new HttpLink({
uri: <link>,
credentials: "include",
fetchOptions: {
mode: "cors",
},
}),
}),
...
}
And in the API website, the next.config.js
file has this:
module.exports = {
async headers() {
return [
{
// matching all API routes
source: "/api/:path*",
headers: [
{ key: "Access-Control-Allow-Credentials", value: "true" },
{
key: "Access-Control-Allow-Origin",
value: <link>,
},
{
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, Access-Control-Allow-Origin",
},
],
},
];
},
};
Is there something wrong with how I configured my next.config.js
file? Or is it in the Apollo Client that is the problem? I don't use Express.
next.config.js
code is apparently not sufficient for handling OPTIONS requests. The Enabling CORS in a single Node.js Serverless Function section of the Vercel docs at vercel.com/support/articles/… shows how special handling for OPTIONS requests needs to be included, if you set things up that way. So it seems like something similar probably needs to be included if you instead do the Enabling CORS in a Next.js App thing. – Tyree