Symptom: CORS error calling Supabase Edge Function
The browser blocked the call because the function didn't answer the OPTIONS preflight or return CORS headers. Use Supabase's withSupabase wrapper, which handles CORS and preflight for you, or handle OPTIONS yourself and add CORS headers to every response, including errors.
Cause: The function doesn't answer the preflight or omits Access-Control-Allow-Origin / allowed headers.
How to fix it
- If available in your setup, wrap the handler with
withSupabase(per Supabase's CORS guide) so CORS is handled automatically. - Otherwise, return early for
OPTIONSrequests with the CORS headers. - Include the CORS headers on every response, including error responses.
- Allow the headers supabase-js sends (
authorization,x-client-info,apikey,content-type). - Restrict the allowed origin to your domain in production.
Example
Illustrative example, not from a customer project.
+ if (req.method === 'OPTIONS') return new Response('ok', { headers: corsHeaders })
- return new Response(JSON.stringify(data))
+ return new Response(JSON.stringify(data), { headers: { ...corsHeaders, 'Content-Type': 'application/json' } })
FAQ
Why does it work with curl?
CORS is enforced only by browsers.
Do errors need CORS headers too?
Yes, otherwise the browser hides the real error behind a CORS message.
Get a $149 diagnosis See pricing Still exposed or still logged out? Get an auth & database security fix (Supabase, Firebase).
- Fixed, or we keep working free until it is (within the written scope).
- A human reviews every fix before it ships.
- read-only access; fixes arrive as pull requests you merge
- Your $149 diagnosis is credited in full toward any paid job.