Lovable + Supabase Edge Function CORS error: why the browser says failed to fetch

By AI App Medic · Updated · Lovable · Supabase · CORS · Edge Functions

A very common Lovable + Supabase symptom: a button that calls an Edge Function works in some cases and then, after a deploy, the browser shows Failed to fetch or Access-Control-Allow-Origin is missing. The Network tab may show a red OPTIONS request, or a real request that looks like it never returned a body. This is not always a CORS misconfiguration. In Lovable apps the same message often hides a function crash, a missing secret, or a typo in the function name. If login and normal queries fail too, not just one function, the cause is not CORS; see Supabase TypeError: Failed to fetch.

Why the browser asks first

Your app runs on your-app.lovable.app or a custom domain. The function runs at https://<project>.supabase.co/functions/v1/<name>. Those are different origins. The official Supabase client sends headers the browser treats as non-simple: authorization, apikey, x-client-info, and often content-type: application/json. Before the real call, the browser sends an OPTIONS preflight and asks which headers and methods are allowed.

The function has to do two things, every time:

If you await req.json() before the OPTIONS check, the preflight has no body, the function throws, and the preflight fails. That is a frequent AI-generated bug.

Step 1: see which request is actually failing

  1. Open the live app (not only the Lovable preview), open DevTools, go to Network, and trigger the action.
  2. Filter by functions/v1. You should see an OPTIONS request and then a POST (or GET).
  3. Click the OPTIONS request. If it is red, or the response is missing Access-Control-Allow-Origin and Access-Control-Allow-Headers, the function is not answering the preflight.
  4. Compare Access-Control-Request-Headers on the request with Access-Control-Allow-Headers on the response. Every requested header must be allowed. The usual set is authorization, x-client-info, apikey, content-type. If you added a custom header, add it to that list.
  5. If OPTIONS is 200 with the right headers, but the real request still shows a CORS error, the function probably crashed or returned an error without CORS headers. Open Supabase > Edge Functions > your function > Logs (or the equivalent Cloud view in Lovable).

Also confirm the request URL. ERR_NAME_NOT_RESOLVED or a call to undefined/functions/v1/... means the live build is missing VITE_SUPABASE_URL or the anon key, which is a different problem (see how Vite env values get into the bundle).

Step 2: use a function template that cannot lose its headers

For @supabase/supabase-js v2.95.0 and later, import the official header map so it stays in sync with what the client sends:

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

Deno.serve(async (req) => {
  if (req.method === "OPTIONS") {
    return new Response("ok", { headers: corsHeaders });
  }

  try {
    const body = await req.json();
    // ... your logic
    return Response.json({ ok: true }, { headers: corsHeaders });
  } catch (err) {
    console.error("function failed", err);
    return Response.json(
      { error: err instanceof Error ? err.message : "Something went wrong" },
      { status: 500, headers: corsHeaders },
    );
  }
});

On older clients, define the same headers yourself in a shared file:

export const corsHeaders = {
  "Access-Control-Allow-Origin": "*",
  "Access-Control-Allow-Headers":
    "authorization, x-client-info, apikey, content-type",
  "Access-Control-Allow-Methods": "POST, GET, OPTIONS",
};

Redeploy the function after the change. In the Supabase CLI that is npx supabase functions deploy your-function-name. If Lovable manages the function, ask it to update and publish that file, then confirm the new version appears in the dashboard.

Step 3: if CORS is fixed and the call still fails

Now the browser will show the real status and JSON. Work through these in order:

A webhook like Stripe should not be called from the browser at all. That path needs a secret and a raw body, and is covered in Stripe webhooks on a Supabase Edge Function.

Step 4: test without the browser

Once you can see status codes, confirm the function itself with a curl from your machine (replace the URL, the anon key, and optionally a user access token):

curl -i -X OPTIONS \
  "https://YOUR_PROJECT.supabase.co/functions/v1/YOUR_FN" \
  -H "apikey: YOUR_ANON_KEY" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: authorization, apikey, content-type, x-client-info"

curl -i -X POST \
  "https://YOUR_PROJECT.supabase.co/functions/v1/YOUR_FN" \
  -H "Authorization: Bearer USER_OR_ANON_JWT" \
  -H "apikey: YOUR_ANON_KEY" \
  -H "Content-Type: application/json" \
  -d '{"ok":true}'

The OPTIONS response should include the CORS headers. The POST response should be readable JSON, not an empty 500. If curl works and the browser does not, you are still missing a requested header on the preflight.

After you add a custom domain

CORS itself usually keeps working with *. What often breaks next is auth: the new host is missing from Supabase redirect URLs. Fix that separately if login fails after the function starts working. And if the error only appears in production, compare the function version and secrets between the preview project and the live project. They are not always the same.

FAQ

Why does the console say CORS when my function is clearly crashing?

Browsers hide the real status when the error response has no Access-Control-Allow-Origin header. The runtime's bare 500 never includes those headers, so the console reports CORS instead of 500.

Do I need to allow my lovable.app origin instead of *?

For most browser calls through supabase-js, the official corsHeaders (which allow any origin) are enough. If you restrict Allow-Origin to one host, remember to update it when you add a custom domain, and still return it on the OPTIONS preflight.

Can I call the Edge Function from the browser without CORS if I use the anon key?

No. The function still lives on a different origin (.supabase.co). The anon key is a header, which is exactly why the browser sends a preflight.

Related guides