Lighthouse audit errors-in-console · Best Practices

"No 'Access-Control-Allow-Origin' header is present on the requested resource": what it means and how to fix it

Quick answer: "No 'Access-Control-Allow-Origin' header is present on the requested resource" means the browser blocked your script from reading a cross-origin response because the server did not allow your origin. Fix it on that server: send Access-Control-Allow-Origin with your origin on every response, including errors and the OPTIONS preflight. If you do not control it, call the API from your own backend.
View raw .md for LLMs / your notes

This console message means the browser made a request to another origin, got a response, and then refused to hand that response to your JavaScript because the server never said your origin was allowed to read it. The full line in Chrome looks like this:

Access to fetch at 'https://api.example.com/items' from origin 'https://www.example.com'
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on
the requested resource.

The fix lives on the server that answers the request, not in the page that makes it. Lighthouse surfaces the message through the audit "Browser errors were logged to the console" (errors-in-console), and one row fails it. This page covers what the browser checked, why the header is often missing only some of the time, how to test it with curl, and the server configuration that fixes it.

TL;DR

What does "No 'Access-Control-Allow-Origin' header is present on the requested resource" mean?

Browsers apply the same-origin policy: a script on https://www.example.com can send a request anywhere, but it can only read the response if the response comes from the same origin, or if the other server opts in. An origin is the scheme, host and port together, so https://example.com, https://www.example.com and http://example.com are three different origins.

The opt-in is one response header:

Access-Control-Allow-Origin: https://www.example.com

When the header is absent, Chrome drops the response and prints this message. Three details help when reading it:

Part of the messageWhat it tells you
Access to fetch at, XMLHttpRequest at, font at, script atWhat kind of request was blocked
at '<URL>'The server that has to change. The fix goes there.
from origin '<origin>'The exact value that server has to allow
(redirected from '<URL>')The request was redirected and the final URL is the one without the header
Response to preflight request doesn't pass access control check:The OPTIONS request failed, before the real request was sent

Chrome usually logs two more lines with it: Failed to load resource: net::ERR_FAILED and, if the promise is not caught, Uncaught (in promise) TypeError: Failed to fetch. They are the same failure seen from the network layer and from your code.

The request itself often did reach the server. CORS does not stop a request from being sent or processed. It stops the page from reading the answer. Do not treat it as protection for your API.

Why is the header missing?

The header is missing for one of these reasons, roughly in order of how often they turn up:

  1. The server has no CORS configuration at all. It was built to be called from its own pages or from other servers, and nobody told it to allow a browser on another origin.
  2. The header is only added to successful responses. The API allows your origin on a 200, then returns a 401, 404, 500 or 502 without the header. The console shows the CORS message, and the real problem is the error behind it. nginx does this by default: add_header skips error responses unless you add always.
  3. The preflight is not handled. For requests with a JSON body, an Authorization header or a method such as PUT or DELETE, the browser first sends an OPTIONS request. If the server answers it with a 404, a 405, a redirect or a login page, the message carries the preflight wording.
  4. A redirect. The URL redirects (to add a trailing slash, to www, to a login page) and the final response has no header.
  5. The origin does not match the allow list. The server allows https://example.com and the page runs on https://www.example.com, or on a preview or staging domain. Servers that check an allow list usually send no header at all for an origin they do not know.
  6. A cache served a response made for someone else. A CDN cached the response to a request that had no Origin header, so the cached copy has no CORS headers. This is common for fonts and images.
  7. A crossorigin attribute on a tag. <script crossorigin>, <img crossorigin> and <link rel="preload" as="font" crossorigin> turn an ordinary request into a CORS request. If the file server does not send the header, a file that used to load now fails.

How do I test for the header with curl?

Send the request with the Origin header your page would send and read the response headers:

curl -s -o /dev/null -D - -H "Origin: https://www.example.com" https://api.example.com/items

Look for access-control-allow-origin in the output and check the status line. A 4xx or 5xx status means the header is missing because the request failed. Fix that error first.

For a request that needs a preflight, send the OPTIONS request the browser would send:

curl -s -o /dev/null -D - -X OPTIONS \
  -H "Origin: https://www.example.com" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: content-type, authorization" \
  https://api.example.com/items

A working preflight returns a 2xx status with access-control-allow-origin, access-control-allow-methods and access-control-allow-headers.

In DevTools, open the Network panel, reload, and click the failed row. A second row of type preflight next to it means the OPTIONS request is the one to inspect.

How do I fix it on the server?

The server that owns the URL in the message has to send the header. Pick the narrowest value that works:

SituationHeader to send
Public data, no cookies or auth (fonts, open JSON, images)Access-Control-Allow-Origin: *
One known site calls the APIAccess-Control-Allow-Origin: https://www.example.com
Several known sites call itEcho the request Origin if it is on your allow list, and add Vary: Origin
The request sends cookies or uses credentials: 'include'The exact origin (never *), plus Access-Control-Allow-Credentials: true

nginx

location /api/ {
    add_header Access-Control-Allow-Origin "https://www.example.com" always;
    add_header Vary "Origin" always;

    if ($request_method = OPTIONS) {
        add_header Access-Control-Allow-Origin "https://www.example.com" always;
        add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS" always;
        add_header Access-Control-Allow-Headers "Content-Type, Authorization" always;
        add_header Access-Control-Max-Age 86400 always;
        return 204;
    }

    proxy_pass http://backend;
}

The always flag is the part most configurations miss. Without it the header vanishes on error responses.

Apache

<IfModule mod_headers.c>
    Header always set Access-Control-Allow-Origin "https://www.example.com"
    Header always set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
    Header always set Access-Control-Allow-Headers "Content-Type, Authorization"
</IfModule>

Node.js with Express

import cors from 'cors';

app.use(cors({
  origin: ['https://www.example.com', 'https://example.com'],
  credentials: true,
}));

Register it before the routes and before the auth middleware, so that preflights and 401 responses carry the headers too.

Static files, fonts and CDNs

For fonts and other public files, send Access-Control-Allow-Origin: * from the storage bucket or the CDN. If the origin server varies the header by origin, also send Vary: Origin, and purge the cache after the change so that old header-less copies are not served.

How do I fix it when I do not control the server?

You cannot add a header to someone else's response from the browser. The options are:

Why does mode: 'no-cors' not fix it?

Setting fetch(url, { mode: 'no-cors' }) makes the error go away and gives you an opaque response: status 0, no headers, and a body your code cannot read. It only helps when you do not need the answer, such as firing a tracking beacon. For data you want to use, it changes nothing.

The same goes for browser extensions that turn CORS off and for launching Chrome with web security disabled. They change your browser and nobody else's. Public CORS proxy services do work, but they put a third party in the middle of your traffic, rate-limit you and disappear without notice. Keep them out of production.

How do I fix it on WordPress, Shopify, or Next.js?

How do I verify the fix?

  1. Run the curl commands again. Both the real request and the OPTIONS request should return access-control-allow-origin with your origin.
  2. Reload the page with DevTools open. The red row and the three console lines should be gone.
  3. Test an error path too: request a URL that returns 404 or send a bad token, and confirm the header is still there. Then you will see the real error instead of a CORS message.
  4. Re-run Lighthouse. The row should be gone from "Browser errors were logged to the console". If it was the only error, the audit passes.

What mistakes should I avoid?


Audit your URL at https://lighthouse-md.com.

Audit your page now

Paste your URL, get scores plus a CLAUDE.md plan for Claude Code.

Run audit →
Add us as a preferred source on Google

One click, and Google shows more of lighthouse-md.com in your search results.