Skip to content
FirstUser

For builders shipping this week

"Blocked by CORS policy" when your live app calls a Supabase Edge Function? 5 checks

Check it like a stranger would. FirstUser opens your live URL in a fresh browser, signs up with a new email, and logs in. If a step fails, you get a screenshot, a plain-English diagnosis and a fix prompt you can paste into Lovable, Bolt, Cursor or Replit. Run a free signup test.

The Upgrade button or the sign-up form worked in the preview. On your live domain it does nothing, and the browser console says something like:

Access to fetch at 'https://<ref>.supabase.co/functions/v1/create-checkout' from origin 'https://yourapp.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

create-checkout in that line is only an example function name. Your app runs on one domain and the function on <ref>.supabase.co, so every call is cross-origin. Before the real request the browser sends an OPTIONS "preflight" and only continues if the answer carries the right headers (MDN). Supabase's CORS guide says that to invoke Edge Functions from the browser "you need to handle CORS Preflight requests".

A CORS error is often a messenger, not the cause. Work through these checks in order. If the function answers and the client shows "Edge Function returned a non-2xx status code", that is a different failure: Lovable Stripe checkout not working. The shorter German note is „Blocked by CORS policy“: Wenn deine Edge Function nur die Preview-Domain kennt.

1. Read the function's logs first

In the Supabase Dashboard open Edge Functions → your function → Logs. Supabase lists one Invocations row per request with its status code, plus your console output and uncaught exceptions (Edge Functions logging).

  • No row at all: the request never reached your code. Check the function name and project ref in the URL (check 5).
  • An OPTIONS row that failed, or a POST with status 500: go to checks 2 and 3.

2. Answer the OPTIONS preflight before anything else

The preflight has no JSON body. If the function calls await req.json() first, it crashes on the preflight and the browser reports CORS. Supabase's example handles it at the very top:

import { corsHeaders } from 'npm:@supabase/supabase-js@^2/cors'

if (req.method === 'OPTIONS') {
  return Response.json({ ok: true }, { headers: corsHeaders })
}

Supabase also offers a withSupabase wrapper that handles CORS and preflight for you.

3. Send the CORS headers on errors too

This is the one generated code misses most. Supabase's example returns corsHeaders on success and in the catch block. If your function throws (say STRIPE_SECRET_KEY is missing on the live project) and returns a 500 without those headers, the browser hides the real error behind a CORS message. Fix the headers first, then the logs or the response show the real error.

4. Allow every header your client sends

The allowed headers must cover what supabase-js sends: authorization, apikey, x-client-info, content-type and, on retries, x-retry-count. Supabase recommends importing corsHeaders from npm:@supabase/supabase-js@^2/cors (v2.95.0 and later) so the list stays in sync; with older versions you keep a hard-coded list in _shared/cors.ts. If someone hard-coded Access-Control-Allow-Origin to the preview address, the live domain is not on it.

5. Make sure the live project has the function and its secrets

  • Is the function deployed to the same Supabase project your live site points at? A typo in the function name or an old project ref also fails without CORS headers.
  • Are its secrets set on that project? Locally functions read supabase/functions/.env; on the hosted project you set them with supabase secrets set or in the Dashboard. Supabase says functions read a new secret immediately, without a redeploy.

Then test like a new visitor

Open the live domain in a private window, create a new account and click the button that calls the function. Keep the console and the Network tab open and look at the OPTIONS and POST rows. Or let FirstUser do it on every deploy.

FAQ

Why does the preview work when the live domain says "blocked by CORS policy"?

The preview and the live site are different origins. A function that hard-codes Access-Control-Allow-Origin to the preview address does not allow the live domain. The function and its secrets also have to exist on the Supabase project the live site calls.

The log shows a 500, but the browser only says CORS. What failed?

The CORS message is what the browser shows when that error response has no Access-Control-Allow-Origin header. Return the CORS headers on the error response too. Then the log, or the response, shows the real error, such as a missing STRIPE_SECRET_KEY.

Is this the same as "Edge Function returned a non-2xx status code"?

No. That message means the function answered and the client could read the status. A CORS failure means the browser blocked the response before your page could read it, usually because the OPTIONS preflight was not answered or the error response left the headers off. The non-2xx case is covered in Lovable Stripe checkout not working.

Can FirstUser catch a buy button that does nothing?

FirstUser opens the live URL, signs up with a new email, and logs in. The checkout step looks for a link named pricing, checkout, upgrade, subscribe, or billing. It passes when a price is visible, and the saved message is "Opened the pricing or checkout page and found a price." It does not click a button that calls an Edge Function, and it does not read the browser console, so a buy click that does nothing while a price is already on the page is still a pass. If no pricing page is found, the step fails with "No pricing or checkout page was found." If the page opens and no price is visible, it fails with "A pricing page opened, but no price was visible." If opening the page throws, it fails with "Could not open pricing or checkout." A step that runs saves a screenshot, and a failed step includes a diagnosis and a fix prompt. Run a free signup test.

One fix like this per email

A short tip each day for Lovable, Bolt and Cursor apps. Unsubscribe anytime.

We'll email that address. Privacy policy

Sources