Skip to content
>_VibeUnstuck

Response to preflight request doesn't pass

Last verified:

The fix

Response to preflight request doesn't pass access control check on a Supabase Edge Function. Answer OPTIONS and send CORS headers on errors too.

  1. 1

    Prefer the wrapper, or answer OPTIONS yourself

    If the function can use withSupabase from npm:@supabase/server@^1, the CORS guide says that wrapper handles CORS and the OPTIONS preflight, so you do not add headers by hand. If the function does not use it, import corsHeaders from npm:@supabase/supabase-js@^2/cors and return them on OPTIONS before any other work. On @supabase/supabase-js before v2.95.0, the guide says to hardcode the map below instead of that import. Do not shorten the header list. The browser rejects a preflight when apikey or authorization is missing from Access-Control-Allow-Headers.

    import { corsHeaders } from "npm:@supabase/supabase-js@^2/cors";
    
    if (req.method === "OPTIONS") {
      return Response.json({ ok: true }, { headers: corsHeaders });
    }
  2. 2

    Put the same headers on success and on errors

    Return corsHeaders from the success response and from every error response, including the catch path. Do not replace a thrown error with an empty catch. The guide's manual example returns the error message as JSON with status 400 and the same headers. For the pre-v2.95.0 map, copy the documented allow-list. Do not add Access-Control-Allow-Credentials: true next to Access-Control-Allow-Origin: *.

    export const corsHeaders = {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Headers":
        "authorization, x-client-info, apikey, content-type, x-retry-count, traceparent, tracestate, baggage",
      "Access-Control-Allow-Methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS",
    };
  3. 3

    Call it with functions.invoke

    The JavaScript reference shows supabase.functions.invoke(name, { body }). That client sends authorization, apikey, and x-client-info for you. A hand-written fetch to /functions/v1/... has to send those headers itself, and each one has to be in the allow-list. Keep one Supabase client. Do not put the secret key in a NEXT_PUBLIC_ or VITE_ variable to "get past" CORS.

  4. 4

    Redeploy, then read the function logs

    The deploy guide uses supabase functions deploy for every function in the folder, or supabase functions deploy hello-world for one name. A saved file does nothing until that deploy. Then open the Functions section of the dashboard, select the function, and read Invocations (status codes) and Logs (uncaught exceptions and console output). Locally, the logging guide says those lines print in the terminal that is serving the function.

Why it happens

A browser call to another origin sends an OPTIONS preflight when the request has a JSON body or headers such as authorization, apikey, or content-type. Supabase's Edge Functions CORS guide says the function has to answer that preflight. If it does not, the browser reports that the preflight did not pass the access control check, and the POST never runs.

The same guide says every response the browser will read, including the error response, needs the CORS headers. A try that returns headers and a catch that returns a bare 500 fails the same way once the function throws. The guide's current fix is the withSupabase wrapper, which answers OPTIONS for you, or importing corsHeaders from npm:@supabase/supabase-js@^2/cors (that import exists from @supabase/supabase-js v2.95.0). As of v2.112.3 that list includes authorization, x-client-info, apikey, content-type, plus x-retry-count and the trace headers traceparent, tracestate, and baggage. The hardcoded example for clients before v2.95.0 uses Access-Control-Allow-Origin: * and that same header list. * in that example is not paired with Access-Control-Allow-Credentials. Adding credentials next to * is rejected by the browser. A generic CORS failure on an API you do not own is a different page.

The next error you'll probably hit

Once this is fixed, the usual follow-up is has been blocked by CORS policy. This request has been blocked by CORS policy. The API sent no Access-Control-Allow-Origin for this page. Allow that origin on your server, or proxy it.

Prompt to paste back into your AI tool

Paste this above the error.

The browser says the preflight does not pass the access control check when the app calls a Supabase Edge Function. The page origin is <origin>. The function name is <function>.

Do not set Access-Control-Allow-Origin to * and Access-Control-Allow-Credentials to true together. Do not empty-catch the error. Do not disable RLS. Do not put the secret or service role key in a NEXT_PUBLIC_ or VITE_ variable. Do not "fix" this from the React component with mode no-cors.

1. If this function does not already use withSupabase from npm:@supabase/server, handle OPTIONS before any other work and return cors headers. Prefer importing corsHeaders from npm:@supabase/supabase-js@^2/cors. The documented allow-list includes authorization, x-client-info, apikey, content-type, and also x-retry-count, traceparent, tracestate, and baggage.
2. Attach those headers to the success response and to every error response, including catch. Show the real error.
3. Call the function with supabase.functions.invoke.
4. Tell me to redeploy with supabase functions deploy <function> and to read Invocations and Logs for that function.
5. Change only this function and the call site.

Common questions

+curl works and the browser does not. Why?

curl does not enforce CORS and does not send the OPTIONS preflight. The function is reachable and still has to answer OPTIONS with the allow-headers the browser asked to send, then repeat those headers on the real response.

+I set Access-Control-Allow-Origin to * and it still fails. Why?

The preflight also needs Access-Control-Allow-Headers to include every header the client sends, including apikey and authorization, and the error response needs the same headers. * together with Access-Control-Allow-Credentials: true is a separate failure. The Supabase example uses * and does not set the credentials header.

Related errors