Mendlark / Guides

CORS error after deploy (Supabase, API, Edge Functions): how to fix it

It worked in the preview. Now, on your live site, the console says Access to fetch at '…' from origin 'https://yourapp.com' has been blocked by CORS policy. CORS is the browser checking that the server you're calling agrees to talk to your domain. The fix is almost always on the server side, not in your fetch call. Work through these in order.

1. Read the full error

Open the browser console (right-click → Inspect → Console) and note two things: the URL being called and the reason. No 'Access-Control-Allow-Origin' header means the server didn't answer with CORS headers. Response to preflight request doesn't pass means the OPTIONS request failed. Then check the Network tab: a CORS error often hides a real 404 or 500 underneath.

2. Supabase Edge Functions: add CORS headers and handle OPTIONS

This is the most common case. Edge Functions don't add CORS headers for you. Every response, including errors, needs them, and the browser's OPTIONS preflight must get a reply:

const corsHeaders = {
  'Access-Control-Allow-Origin': 'https://yourapp.com',
  'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
  'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
}

Deno.serve(async (req) => {
  if (req.method === 'OPTIONS') {
    return new Response('ok', { headers: corsHeaders })
  }
  try {
    // ... your logic
    return new Response(JSON.stringify(data), {
      headers: { ...corsHeaders, 'Content-Type': 'application/json' },
    })
  } catch (e) {
    return new Response(JSON.stringify({ error: e.message }), {
      status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' },
    })
  }
})

Redeploy the function after editing (supabase functions deploy name). If the function crashes before returning, the browser reports it as CORS. Check the function logs in the Supabase dashboard.

3. Allow your live domain, not just localhost

Many generated backends hard-code http://localhost:5173 or the preview URL as the allowed origin. Add your live domain, with https:// and no trailing slash, and include both www and non-www if you use both. For an Express API with the cors package:

app.use(cors({ origin: ['https://yourapp.com', 'https://www.yourapp.com'] }))

4. Check you're calling the right URL

  • A fetch to http://localhost:3000/api works only on your machine. Use an environment variable for the API base URL and set it on your host.
  • Calling http:// from an https:// site is blocked as mixed content.
  • A wrong path that returns a 404 or a redirect often shows up as a CORS error.
  • Plain Supabase database calls via supabase-js don't need CORS setup. If those fail, check your VITE_SUPABASE_URL / anon key are set, not CORS.

5. Don't call third-party secret APIs from the browser

APIs like OpenAI or Stripe's secret endpoints block browser calls on purpose, and calling them there exposes your key. Move the call into a server route (Next.js API route, Vercel function or Edge Function) and fetch that from your app instead.

Quick checklist

  • Read the exact URL and reason in the console; check Network for a hidden 404/500
  • Edge Functions return CORS headers on every response and answer OPTIONS
  • Live domain (https, both www variants) in the allowed origins
  • No localhost or http:// URLs in production
  • Secret-key APIs called from the server, never the browser

Still stuck?

Mendlark fixes this kind of problem for a fixed €49 (launch price, normally €79). We agree the problem first, test the fix and send it as a pull request, so nothing changes in your app until you merge it. If we can't fix it, you don't pay. Get the €49 fix, start with a free scan, or read our guide to environment variables undefined in production on Vercel.

Not sure what else is wrong? Paste your app link into the free instant check and get a plain-English list of problems in seconds.