Next.js Discord

Discord Forum

Vercel Cors error

Unanswered
Short-tailed Hawk posted this in #help-forum
Open in Discord
Short-tailed HawkOP
this cors error only started happening after i deployed my projects on vercel, because they worked fine in production and ive tried a lot of stuff to fix this error such as configuring my vercel.json file, and configuring cors and i also tried adding a proxy and even that didnt work, it still gives me the same error, you can check some of the codes below

// (vercel.json)
"headers":
{ "Access-Control-Allow-Origin": "https://valton-frontend.vercel.app",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Methods": "GET,OPTIONS,PATCH,DELETE,POST,PUT",
"Access-Control-Allow-Headers": "Content-Type, Authorization"
}

// (index.js)
app.use(cors({
origin: 'https://valton-frontend.vercel.app',
allowedHeaders: ['Content-Type', 'Authorization'],
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
credentials: true,
}))

app.options('*', (req, res) => {
res.header('Access-Control-Allow-Origin', 'https://valton-frontend.vercel.app');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.sendStatus(204); // No Content
});

36 Replies

@B33fb0n3 please provide the actual request (with all information) from the network tab and the full result incl. status code
Short-tailed HawkOP
Short-tailed HawkOP
i sent the errors, and i sent the cors codes, and i specified that the error comes from vercel because it works fine in production @B33fb0n3
Short-tailed HawkOP
what else do i need to send
@Short-tailed Hawk what else do i need to send
please provide the actual request (with all information) from the network tab and the full result incl. status code
I can see the route and the status inside your screenshot.

I don't see the actual request, nor the information's that are sent with it like headers. I also don't see the full result.
@Short-tailed Hawk but those are the only things that are showing in my networks tab
If you don't want to show it, it's fine. However then I can't solve your issue. All the best 👍
@B33fb0n3 If you don't want to show it, it's fine. However then I can't solve your issue. All the best 👍
Short-tailed HawkOP
bro i said those are the only things that are showing? where do i find the things youre asking for?
i dont see the actual request in my networks tab
@Short-tailed Hawk bro i said those are the only things that are showing? where do i find the things youre asking for?
you can click on any network request to see the details that I mean. For example the headers (for request (orange box) and response (green box)) and also the response itself and some other stuff (red box)
@B33fb0n3 it looks like you can see them: https://discord.com/channels/752553802359505017/1324234916766158869/1324294288942563410 <--- click
Short-tailed HawkOP
Thanks for sharing the details. Make sure you don't leak anything when sharing details.

The way how you index your app looks pretty uncommon for me. Where inside the nextjs docs did you found that pattern?
https://nextjs.org/docs
@Short-tailed Hawk so how do i fix the error
we are currently on it. The error is a 404 error. So whereever you sent the OPTIONs to was not found. So I currently trying to find out how you initialize everything and asked you this: https://nextjs-forum.com/post/1324234916766158869#message-1324310380893442101 <--- click
So please answer that message to get your issue resolved.

btw. when you reply faster, it will be solved faster ;)
@B33fb0n3 Thanks for sharing the details. Make sure you don't leak anything when sharing details. The way how you index your app looks pretty uncommon for me. Where inside the nextjs docs did you found that pattern? https://nextjs.org/docs
Short-tailed HawkOP
i didn’t find the pattern in nextjs docs, im not using nextjs, at first i only had app.use(cors()) but then i saw somewhere that using app.options can fix it so i tried that too and i tried everything online
Short-tailed HawkOP
you can ask me for any code ill send it
im also using prisma with mongodb maybe vercel doesn’t support them idk?
@B33fb0n3 Did you followed this guide to deploy your app, as your code looks like it's express? https://vercel.com/guides/using-express-with-vercel
Short-tailed HawkOP
yes ive done everything that is on there, i’ve configured my vercel.json file, and again everything works perfectly when im running it locally
at this point if nothing works ill try to find smth else where i can host it for free
but i tried render and its so bad bcuz the free version has really long cold starts which take like 5 minutes
@Short-tailed Hawk but i tried render and its so bad bcuz the free version has really long cold starts which take like 5 minutes
yea, keep in mind, that vercel is a serverless env and you might need to change some things, when you using express.

If you just want to have api routes, just build a normal nextjs application. There will start fast and you should have nearly no issues (at least no cors issues)
I am not experienced with express so there may be something that you can do, but I guess I am not the correct person to solve this thread
Short-tailed HawkOP
damn aight i dont think anyone else is gonna come here
i think fly.io has a free version
ima try that
@Short-tailed Hawk i think fly.io has a free version
yea make sure to keep this thread updated, so others get a solution in the future
Short-tailed HawkOP
yea ive looked everywhere for a solution, i think the only thing you can do is host it somewhere else
Short-tailed HawkOP
aight ill do it tomorrow, cuz ima work on the project first
Short-tailed HawkOP
i deployed with render and it works but render has another problem where it can’t send cookies on ios devices SO GUYS theres only one solution… basically just use a postgresql database and then it will work with vercel. 😀
Short-tailed HawkOP
update after trying it with postgresql it didn’t work again… so ig the problem is with the cookies… idk why it doesn’t work with cookies