App Finishers

Fix guide · updated 2026-11-16

Supabase Edge Function "blocked by CORS policy": every message, fixed

Access to fetch at '…/functions/v1/…' has been blocked by CORS policy · Response to preflight request doesn't pass access control check · FunctionsFetchError

Your app calls a Supabase Edge Function and Chrome's console says "has been blocked by CORS policy", maybe only since you deployed. The end of that line names the mistake. We ran Supabase's real Edge runtime with 13 small functions, most with one CORS mistake each, and called them from Chrome with supabase-js 2.119 in two ways: straight to the function, the way a deployed project works, and through the gateway that supabase start runs on your machine. The surprise: all 16 calls that Chrome blocked went through on the local gateway, because it answers the browser's CORS check itself. Your function's CORS code never gets tested on your machine.

What the error means

supabase-js adds its own headers to each call, like apikey, authorization and x-client-info. Because of them, Chrome first asks your function for permission with an OPTIONS request, called the preflight. Your function has to answer it with status 200 and headers that allow your site, those headers and the method. After that, every real answer, errors included, needs an Access-Control-Allow-Origin header, or Chrome hides it from your app.

When Chrome blocks a call, your app gets nothing to read. In our test, every blocked call gave the same error in supabase-js, whatever the cause:

FunctionsFetchError: Failed to send a request to the Edge Function
error.context: TypeError: Failed to fetch

The cause is in the browser's console, at the end of a line like this one:

Access to fetch at 'https://….supabase.co/functions/v1/hello' from origin 'https://myapp.com' has been blocked by CORS policy: …

The line also tells you whether your function ran. When it says "preflight" or names a header or a method, the real request never reached it: in our test, those functions only ever saw the OPTIONS request. When it doesn't, your function ran, so whatever it did before answering (sending an email, writing a row) has happened, even though your app shows an error.

Why it worked on localhost

With the Supabase CLI (supabase start, supabase functions serve), your app calls functions at http://127.0.0.1:54321/functions/v1/…. That address is a gateway, Kong, and the CLI's own Kong config turns on Kong's CORS plugin for /functions/v1/. With its default settings, the plugin answers every preflight itself, allows any header and any method, and puts Access-Control-Allow-Origin: * on every answer, replacing the one your function sent.

We ran Kong 2.8.1, the version the CLI uses, with that config in front of the same functions. All 16 calls that Chrome blocked went through, and no function ever saw an OPTIONS request. Even the function that allows only http://localhost:5173 worked from another address. A deployed project has no such gateway in the way: Supabase's docs say your function has to handle the preflight itself. So test CORS against the deployed function, from the browser or with the terminal check further down.

1. "No 'Access-Control-Allow-Origin' header" on the preflight: answer OPTIONS

Your function doesn't answer the OPTIONS request, so the runtime's own error goes back, without CORS headers. In our test, a function with no CORS code read the body first, crashed on the empty OPTIONS request (Unexpected end of JSON input in its logs), and Chrome got a 500 with no headers.

Answer OPTIONS before anything else: before reading the body, checking the user or checking the method. This is Supabase's current example. In our test, it passed Chrome's checks for POST and DELETE calls, and its 400 answer reached the app with the error message:

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

export default {
  fetch: async (req) => {
    // Handle the CORS preflight request.
    if (req.method === 'OPTIONS') {
      return Response.json({ ok: true }, { headers: corsHeaders })
    }

    try {
      const { name } = await req.json()
      return Response.json({ message: `Hello ${name}!` }, { headers: corsHeaders })
    } catch (error) {
      const message = error instanceof Error ? error.message : String(error)
      return Response.json({ error: message }, { status: 400, headers: corsHeaders })
    }
  },
}

If your function starts with Deno.serve(async (req) => { instead, the same lines go inside it. corsHeaders comes with supabase-js 2.95.0 and later. If your function pins an older version, Supabase's docs give the list to paste into supabase/functions/_shared/cors.ts, the same one supabase-js 2.119 sends:

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',
}

2. "It does not have HTTP ok status": the OPTIONS answer is an error

Your function answers the preflight with the CORS headers, but with an error status. Chrome refused all three of ours:

if (req.method !== 'POST') {
  return new Response('Method not allowed', { status: 405, headers: corsHeaders })
}
if (req.method === 'OPTIONS') { … }   // never reached

If that error status is a 404, your code isn't the problem: Supabase doesn't know that function name, so the request never reached it. See Edge Function 404 "Requested function was not found".

Move the OPTIONS branch to the very top of the function and answer it with 200, as in section 1. Your method and token checks go after it.

3. "Request header field … is not allowed": add the header to your list

The call sends a header that your Access-Control-Allow-Headers list doesn't name. Chrome names one header at a time. In our test, it named one of these:

Add the missing names to the list. This passed in our test with a region and a custom header:

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

const corsHeaders = {
  ...supabaseCors,
  'Access-Control-Allow-Headers': supabaseCors['Access-Control-Allow-Headers'] + ', x-region, x-app-version',
}

4. "Method … is not allowed by Access-Control-Allow-Methods": list your methods

supabase-js calls functions with POST, unless you pass a method. The list from Supabase's older docs has no Access-Control-Allow-Methods line:

export const corsHeaders = {
  'Access-Control-Allow-Origin': '*',
  'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
}

With it, POST and GET calls worked in our test, because browsers allow those two without the line. PUT, PATCH and DELETE calls, like invoke('hello', { method: 'DELETE' }), were refused. Add the line, or switch to the corsHeaders from supabase-js, which has it:

'Access-Control-Allow-Methods': 'GET, POST, PUT, PATCH, DELETE, OPTIONS',

5. "No 'Access-Control-Allow-Origin' header" without "preflight": your answer has no headers

The preflight passed and your function ran, but its answer had no CORS headers, so Chrome hid it from your app. In our test, two functions did this, and both worked when the request was fine:

// the error answer forgets the headers
if (!name) return Response.json({ error: 'name is required' }, { status: 400 })

// or the function throws: the runtime answers "500 Internal Server Error", without your headers
if (!name) throw new Error('name is required')

So the CORS error only shows up when something else goes wrong, and it hides what. The function's logs still show it: Error: name is required in our test. In your Supabase dashboard, that's Edge Functions, then your function, then Logs.

Put the CORS headers on every answer, and catch errors so a crash also answers with them, like the try/catch in section 1. With the headers on its error, Supabase's example came back as FunctionsHttpError with status 400 and the error message in the body. To read that message in your app, see Edge Function returned a non-2xx status code.

6. "not equal to the supplied origin" or "contains multiple values": allow your site

The function allows a different site than the one calling it. In our test:

'Access-Control-Allow-Origin': 'http://localhost:5173'
→ The 'Access-Control-Allow-Origin' header has a value 'http://localhost:5173' that is not equal to the supplied origin.

'Access-Control-Allow-Origin': 'http://localhost:3000, https://myapp.com'
→ The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:3000, https://myapp.com', but only one is allowed.

The first pins the address where the function was first tested, a dev server or a preview link, and fails everywhere else. The second can't work: the header takes one origin, or *. Supabase's own corsHeaders sends *, which suits a function your app calls with supabase-js, since it sends the user's token in a header, not in a cookie. To allow only your own sites, send back the caller's origin when it's on your list:

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

const allowed = ['https://myapp.com', 'http://localhost:3000']

function cors(req: Request) {
  const origin = req.headers.get('Origin') ?? ''
  return {
    ...supabaseCors,
    'Access-Control-Allow-Origin': allowed.includes(origin) ? origin : allowed[0],
    'Vary': 'Origin',
  }
}

Then use cors(req) wherever the example uses corsHeaders. In our test, this worked from a listed address and was refused from one that wasn't. An origin is the scheme, the host and the port, with no path or trailing slash: https://myapp.com, not https://myapp.com/. Your site with and without www. counts as two origins.

Check a deployed function from a terminal

curl doesn't enforce CORS, but it shows what your function answers to a preflight. Put in your project's address, the function's name and your site:

curl -i -X OPTIONS 'https://YOUR-PROJECT.supabase.co/functions/v1/hello' \
  -H 'Origin: https://myapp.com' \
  -H 'Access-Control-Request-Method: POST' \
  -H 'Access-Control-Request-Headers: authorization, x-client-info, apikey, content-type'

In our test, Supabase's example answered with these lines:

HTTP/1.1 200 OK
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

A status other than 200 is section 2, or section 1 when the access-control lines are missing too. A header your app sends but the list doesn't name is section 3. Don't run this check against 127.0.0.1:54321: there the local gateway answers instead of your function, and in our test it answered 200 for the function with no CORS code at all.

If an AI tool built your app

Give your AI tool this prompt, with the full line from the console:

Calling a Supabase Edge Function from the browser fails with this CORS error: [paste the full console line]. In every Edge Function the app calls from the browser: import corsHeaders from 'npm:@supabase/supabase-js@^2/cors'. Answer OPTIONS requests first, before reading the body, checking the user's token or checking the method, with status 200 and corsHeaders. Add corsHeaders to every other response, errors included, and wrap the handler in try/catch so a thrown error also returns JSON with corsHeaders. If the app calls a function with a region or with custom headers, add those header names to Access-Control-Allow-Headers. Don't pin Access-Control-Allow-Origin to one address. Redeploy the functions afterwards. Don't change anything else.

Not quite your error?

Still stuck?

Open the Network tab in your browser's developer tools, call the function again and look at its two requests: the preflight (method OPTIONS) and the real one. The preflight's status and response headers tell you which of sections 1 to 4 applies. If the real request went out too, your function ran: its logs in the Supabase dashboard, at the time of the call, name the error your app couldn't read.

Other Supabase problems? See Supabase fixes.