All posts

'Blocked by CORS policy' in your AI-built app: the real fix

Your app works in the editor. Then you open the live site, and nothing loads. The browser console shows a red error: "Access to fetch at 'https://…' from origin 'https://…' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource". It often happens only on the live domain, only after you moved off the builder's hosting, or only for one API. Lovable, Bolt, Cursor and Replit all produce apps that hit this. Vendor facts below come from each vendor's own docs, checked in November 2026.

Short answer: CORS is a browser rule. Your page is on one address. It asks a server on another address for data. The browser only lets the page read the answer if that server says "this page is allowed". The fix is almost never in the browser. Either call the other service from a server function, or make your own server send the right headers, including on the preflight request and on error responses. Do not disable CORS, and do not use a public proxy.

1. What CORS means in plain words

MDN's CORS guide describes it as an HTTP-header based mechanism that lets a server say which other origins a browser should permit to load resources. Without it, the same-origin policy applies: a web app "can only request resources from the same origin the application was loaded from unless the response from other origins includes the right CORS headers."

An origin is the scheme, the host and the port. Two addresses with any difference are different origins. All of these differ from each other:

  • https://example.com and https://www.example.com (www or not)
  • http://example.com and https://example.com (http or https)
  • http://localhost:5173 and your live domain (your laptop versus production)
  • A preview address and your live address. See staging copies and preview URLs.

This is why the app can work in the builder and break live. Your server may only know about the old address.

The check happens in the browser. A tool like curl or Postman does not run this check, so the same request can work there and fail on your page. The server often did answer. The browser refused to hand the answer to your page. MDN also notes that for security, the details of a CORS failure "are not available to JavaScript". The code only learns that an error happened. That is why the console message is vague and the real hint is in the Network tab (see section 6).

2. Preflight: the OPTIONS request before the real one

For some requests the browser first sends a test request. MDN says that for "preflighted" requests the browser first sends an HTTP request using the OPTIONS method to the other origin, "in order to determine if the actual request is safe to send."

When does this happen? MDN defines a "simple request" as one that meets all of its conditions: the method is GET, HEAD or POST, only a short list of safe headers is set, and the Content-Type is only application/x-www-form-urlencoded, multipart/form-data or text/plain. Most modern app calls break these rules. Sending JSON (application/json) or adding an Authorization header means a preflight. That is nearly every call in an AI-built app.

The preflight asks things like "may I use POST?" and "may I send these headers?". The server must answer with headers such as Access-Control-Allow-Methods and Access-Control-Allow-Headers. If the answer is missing or wrong, the browser never sends the real request.

A common cause in generated code: the function only handles POST. The OPTIONS request gets a 404 or 405 error, or an answer without the CORS headers. The browser then reports a CORS error, even though your POST code is fine.

3. The usual causes in AI-built apps

The app calls a third-party API straight from the browser

AI tools like to write fetch("https://api.some-service.com/...") in the front end. Many services were never meant to be called from a browser, so they send no CORS headers. MDN's page on the missing Allow-Origin header says it plainly: if the remote server is not under your control and does not include the header, "you cannot fix this error on the server side." One option it lists is to route the request through a proxy server you control.

That is the real fix: make the call from a server function that you own. The browser calls your function. Your function calls the service. This also keeps the API key out of the browser. If the call carries a key today, read the API key check first, because that key is already visible to anyone.

Your own backend function is missing headers or OPTIONS handling

Supabase Edge Functions, Netlify Functions and Vercel Functions do not add CORS headers for you. You write them in the function. Section 4 shows each platform.

The allowed origin list misses your new domain

You moved from the builder's address to your own domain, or from example.com to www.example.com. The function still allows the old origin only. Add every real address of your site, with the exact scheme. See moving a Lovable or Bolt app to your own hosting, which changes the origin.

Credentials with a wildcard origin

By default browsers do not send cookies on cross-origin calls. If your code asks for credentials, MDN says the server "must not" use the * wildcard for Access-Control-Allow-Origin, and must name an explicit origin instead. If a request has credentials and the response has Access-Control-Allow-Origin: *, "the browser will block access to the response, and report a CORS error." The MDN missing-header page adds that when the origin is chosen dynamically, the server must also set Vary: Origin.

A redirect or an error hiding the real problem

Two traps. First, a redirect after a preflight. MDN says not all browsers support following redirects after a preflighted request, and some report an error such as "The request was redirected to … which is disallowed for cross-origin requests that require preflight." Typical case: the code calls http:// or a URL without the trailing slash, and the server redirects. Use the final URL directly.

Second, an error response without headers. The Supabase example attaches the CORS headers to the error response as well as the success response. If your function crashes or returns a 400 or 500 without them, the browser shows a CORS message instead of your real error. Check the Network tab for the status code. If it is 500, fix the 500 first.

4. The fix per platform

Supabase Edge Functions

Supabase's CORS page says: "To invoke edge functions from the browser, you need to handle CORS Preflight requests." Its example imports the headers from the SDK (version 2.95.0 or newer):

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

The function then answers the preflight first:

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

And it passes { headers: corsHeaders } on every other response too, errors included. The page notes that before v2.95.0 you hardcode the CORS headers yourself. Check which version your project uses. Note that the docs' shared headers use Access-Control-Allow-Origin: '*'. For a function that serves logged-in users, ask your AI tool to limit that to your own domains (see section 5).

Netlify Functions

Netlify's functions page says a function "returns a Response that is delivered back to the client." So the headers go on the Response your function returns. Handle the OPTIONS method in the function as well.

One trap: the custom headers page says custom headers (_headers or [[headers]] in netlify.toml) "apply only to files Netlify serves from our own backing store." For a URL handled by a function, "custom headers won't be applied to that content." The function should return any required headers itself. So adding CORS rules to netlify.toml will not fix a function. AI tools often try exactly that.

Vercel Functions

Vercel's guide How to enable CORS on Vercel says: "Vercel Functions don't add CORS headers for you." It shows three places to set them: a headers block in vercel.json for fixed values, an exported OPTIONS function in a route handler, and Routing Middleware, which "runs on Vercel's network before the request reaches your function, so it can answer a preflight OPTIONS before your function starts." For several allowed origins with credentials, the guide says to check the incoming Origin against a list and send back the matching one.

Another Vercel trap from the same guide: with Deployment Protection on, an unauthenticated preflight gets a 401 from Vercel before your function runs. The browser never sees your headers. The guide's fix is the OPTIONS Allowlist.

Storage buckets

If the error appears when loading or uploading files, the bucket itself may have its own CORS settings. See file upload checks.

5. What NOT to do

  • Do not use a browser extension that disables CORS. It only changes your own browser. Your visitors still see the error. Worse, it hides real problems and turns off a protection while you browse other sites.
  • Do not put Access-Control-Allow-Origin: * on endpoints that use cookies or logins. MDN says the wildcard should be used only for public APIs: "Private APIs should never use *, and should instead have a specific domain or domains set." And with credentials the browser rejects it anyway.
  • Do not send your calls through a public CORS proxy. Every request, including any key or token in it, passes through a stranger's server. MDN's advice is a proxy "you control". Make that your own function.

6. Prompts and how to check the fix

Example prompts for your AI tool, adapted to your stack:

  • "Add an OPTIONS handler and CORS headers for https://www.example.com and https://example.com to this function. Return the same headers on error responses. Do not use a wildcard."
  • "The browser calls https://api.thirdparty.com directly and gets a CORS error. Move that call into a server function. Read the API key from an environment variable. The front end should call my function instead."
  • "List every origin this app can run on (live, www, preview, localhost) and show where each backend function checks the origin."

Then check it in the browser:

  1. Open the live site. Open developer tools, then the Network tab, and reload.
  2. Find the failing request. If there is an OPTIONS request before it, click it. It should return a 200 or 204 status, not 404, 405 or 500.
  3. Look at the response headers. You want Access-Control-Allow-Origin with your exact origin, and Access-Control-Allow-Headers listing the headers your app sends, such as authorization and content-type.
  4. Click the real request. Check that it has the same CORS header and a normal status code.
  5. Test on both example.com and www.example.com, and once while logged in.

7. Checklist

  • The error text and the failing URL are copied from the console.
  • No third-party API is called directly from the browser if it was not made for that.
  • The function answers OPTIONS with a success status and the right headers.
  • CORS headers are on error responses too.
  • Every real origin is allowed: www, non-www, https, and previews if needed.
  • No wildcard origin on endpoints that use cookies or logins.
  • Netlify: headers are set in the function, not only in netlify.toml.
  • Vercel: Deployment Protection is not blocking OPTIONS.
  • No redirect between the call and the final URL.
  • Checked in the Network tab on the live domain.

Want this fixed for you?

I finish and fix apps built with Lovable, Bolt, Cursor or Replit, including CORS errors that only show up on the live site. Send me the details and I'll reply within 24 hours.