← All errors · Supabase

TypeError: Failed to fetch (Supabase Edge Function / CORS)

Exact error

Access to fetch at 'https://<ref>.supabase.co/functions/v1/...' has been blocked by CORS policy

What it means

The browser blocked the response because the Edge Function did not return CORS headers, often on the OPTIONS preflight request.

Likely causes

How to fix it

  1. Handle preflight and add headers to every response:

    const corsHeaders = {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
    };
    
    Deno.serve(async (req) => {
      if (req.method === 'OPTIONS') return new Response('ok', { headers: corsHeaders });
      const data = { ok: true };
      return new Response(JSON.stringify(data), {
        headers: { ...corsHeaders, 'Content-Type': 'application/json' },
      });
    });
  2. Redeploy the function: `supabase functions deploy <name>`.

Related errors

Get new fixes weekly

No spam. Unsubscribe anytime. Privacy