Issue with Cors
I'm trying to add an http endpoint to convex which any application is able to send a request to, but it seems like maybe using * doesn't work?
this is my current handler for options, but it fails when my other domain tries to hit it
12 Replies
ALLOW_ORIGIN is *
I'm guessing I need to use the request object and get the origin on the request somehow
maybe request.referrer is what I'm looking for.. let me try it
interesting, request.referrer crashes my httpAction
Not implemented: get referrer for Request
yeah, stuck on this for nowBefore the referrer change, what was happening when a request came in? What was the initial error?
@ian any idea on this one?
A couple things come to mind:
- Avoid setting cache headers off the bat until you get it working to avoid debugging against cached results
- Add a "Vary: Origin" header if you're returning the origin in the response, so requests from different origins don't have a cached response of the other origin
- The
*
should work when there aren't credentials being sent up - but needs a value when passing up with credentials. you can set credentials to "omit" if you don't need them
- Start by hard-coding the origins - have you checked the ORIGIN header? It'll be set for preflight requests I believethe options request was failing with cors when it was set to *
trying some of those now
k... it's working now.

although I'm still trying to understand why
I read Vary: 'Origin' prevents the browser from caching headers
but does both the options and post endpoint need it?
is it because I have Vary: 'origin' lower case?
And what about
'Access-Control-Allow-Origin': headers.get('Origin'),
when the origin matches some list of allowed ones?I'm not sure
Vary: 'Origin'
is playing a big part, just noticed it while going through https://httptoolkit.com/will-it-cors which I use when I forget these thingsWill It CORS? - a CORS debugging tool that actually works.
Literally nobody understands CORS, except this one magic web page
I've been meaning to make a
cors
helper that would do something like:
with defaults that don't allow credentials, accept any origin and headers, cache for 86400, and allow timing.
It'd register each of the methods separately, then register OPTIONS for the same path that sends the right response. If credentials are passed up and no origins are allowed, it'll send a helpful error of how to either add origins or not send credentials from the client
But I haven't been making an app that needs it, so I haven't invested in it. If you want to do a video on CORS and make the helper, by all means! Or lmk if you want me to take a stab at the helper if it'd be useful for ya