LeakPatrol › Guides

Supabase Edge Function: "blocked by CORS policy" fixed

By LeakPatrol · Updated 20 November 2026

Your app calls supabase.functions.invoke('my-function') and the browser console says Access to fetch at 'https://xyz.supabase.co/functions/v1/my-function' from origin 'https://myapp.lovable.app' has been blocked by CORS policy: Response to preflight request doesn't pass access control check. The function may even work when you test it in the dashboard. The browser asks the function for permission first, with an OPTIONS request, and the function doesn't answer it the way the browser expects.

1. The function doesn't answer the OPTIONS preflight

Before a request with an Authorization or Content-Type: application/json header, the browser sends OPTIONS. Your function has to return CORS headers for it, before any other code runs:

const corsHeaders = {
  'Access-Control-Allow-Origin': 'https://myapp.lovable.app',
  'Access-Control-Allow-Headers':
    'authorization, x-client-info, apikey, content-type',
  'Access-Control-Allow-Methods': 'POST, OPTIONS',
};

Deno.serve(async (req) => {
  if (req.method === 'OPTIONS') {
    return new Response('ok', { headers: corsHeaders });
  }
  // ... your code
  return new Response(JSON.stringify(data), {
    headers: { ...corsHeaders, 'Content-Type': 'application/json' },
  });
});

2. The headers are missing on the real response or on errors

The preflight passes, then the real response comes back without Access-Control-Allow-Origin, and the browser hides it. This often happens on the error path: a throw or an early return new Response('Unauthorized', { status: 401 }) without the headers. Add corsHeaders to every response, including errors, so you can see the real error message instead of a CORS error.

3. The function crashed or doesn't exist

If the function name is wrong, it isn't deployed, or it crashes on start, Supabase returns an error without your headers and the browser reports CORS. Check Edge Functions → Logs in the Supabase dashboard. A CORS error with a 404 or 500 behind it is not a CORS problem.

4. A header your app sends isn't allowed

If your code adds a custom header (for example x-user-id), it must be listed in Access-Control-Allow-Headers, or the preflight fails.

Is Access-Control-Allow-Origin: * safe?

CORS only controls which websites can read the response in a browser. It is not access control: anyone can still call your function with curl or a script. So:

A prompt to paste into Lovable, Bolt or Cursor

Calling my Supabase Edge Function from the browser fails with
"blocked by CORS policy". Fix it safely:
1. Handle OPTIONS first and return CORS headers.
2. Add the same CORS headers to every response, including errors.
3. Allow only my app's origins, not "*".
4. Allow the headers supabase-js sends: authorization, x-client-info,
   apikey, content-type.
5. Keep JWT verification on and check the user with auth.getUser()
   before doing work. Do not put any secret keys in responses.

Want to check nothing else is exposed? LeakPatrol's free scan checks your live app for leaked keys, public files and other common mistakes in about a minute.

Check your app in a minute

Free, read-only, no sign-up. Every problem comes with a plain-English fix and a prompt for your AI tool.

Get the 10-point checklist

Every check we run on AI-built apps, with how to do each one yourself. Straight to your inbox.

Questions

Why does my Supabase Edge Function say blocked by CORS policy?

The browser sends an OPTIONS preflight first, and the function did not answer it with Access-Control-Allow-Origin and Access-Control-Allow-Headers. Handle OPTIONS at the top of the function and add the CORS headers to every response.

Why does the function work in the dashboard but not in my app?

The dashboard and curl do not enforce CORS; only browsers do. The function runs, but the browser blocks your app from reading the response.

Is Access-Control-Allow-Origin * safe for an Edge Function?

CORS is not access control: anyone can call the function outside a browser. Prefer your own origins, keep JWT verification on, and check the user inside the function.

Can a CORS error hide a different error?

Yes. If the function crashes, is not deployed or returns an error without CORS headers, the browser reports CORS. Check the Edge Function logs for the real status.

More guides