Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A public website cannot reach your local dev server: local network request blocked

ErrorAccess to fetch at 'http://localhost:3000/api' from origin 'https://app.example.com' has been blocked by CORS policy

Short answer

Chrome treats a request from a public website to localhost or a private IP as a local network request. It needs the user's permission, a secure page or a targetAddressSpace hint, and, under the older Private Network Access rules, a preflight answer with Access-Control-Allow-Private-Network: true. Normal CORS headers must also be present.

  • Applies to Chrome 142 and later, Express 5, cors 2.8, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open DevTools on the public page and find the failed request in the Network tab. Look for an OPTIONS row before it: if it exists and fails, the preflight is the problem. If there is no row at all, Chrome stopped the call before sending it, usually because the permission was denied or mixed content blocked it.

    curl -s -o /dev/null -D - -X OPTIONS http://localhost:3000/api \
      -H "Origin: https://app.example.com" \
      -H "Access-Control-Request-Method: GET" \
      -H "Access-Control-Request-Private-Network: true"
  2. 2Match what you see, then jump to its fix

    1. 1No request leaves the page; the local network permission was refused or dismissed once.Chrome asked for local network permission and it was dismissed or blockedFix
    2. 2The OPTIONS row fails, or the Issues panel shows: ensure private network requests are only made to resources that allow them.The local server does not answer the private network preflightFix
    3. 3The console says the request was blocked as mixed content, and no OPTIONS row appears.An https page calls an http localhost addressFix

What does the error look like?

  • Access to fetch at 'http://localhost:3000/api' from origin 'https://app.example.com' has been blocked by CORS policy
  • Look for and connect to any device on your local network
  • ensure private network requests are only made to resources that allow them

Cause 1 of 3

Chrome asked for local network permission and it was dismissed or blocked

1Check

Chrome's Local Network Access check applies when a public page sends a request to a loopback or private address. The user must grant a permission, and it can only be asked for in a secure context (an https page).

fetch('http://localhost:3000/api').then(r => r.status, e => e.message)

2Fix

Reset or allow the local network permission for the public origin in Chrome's site settings, then reload. If the public page is plain http, serve it over https first, since the permission is only requested in secure contexts.

# Chrome site settings for https://app.example.com: allow the local network permission

3Verify

Reload the public page and repeat the call. The request should now show up in the Network tab.

fetch('http://localhost:3000/api').then(r => r.status)

Confirmed by developer.chrome.com

Did this fix it?

Cause 2 of 3

The local server does not answer the private network preflight

1Check

Under Private Network Access, the preflight carries Access-Control-Request-Private-Network: true. The server must reply with Access-Control-Allow-Private-Network: true next to the normal CORS headers.

curl -s -o /dev/null -D - -X OPTIONS http://localhost:3000/api \
  -H "Origin: https://app.example.com" \
  -H "Access-Control-Request-Method: GET" \
  -H "Access-Control-Request-Private-Network: true" | grep -i "^access-control"

2Fix

Answer that header from your dev server for the one origin you trust, in the same place that handles OPTIONS.

// Express 5, development only
import express from 'express'
import cors from 'cors'

const app = express()
app.use((req, res, next) => {
  if (req.headers['access-control-request-private-network']) {
    res.setHeader('Access-Control-Allow-Private-Network', 'true')
  }
  next()
})
app.use(cors({ origin: 'https://app.example.com' }))

3Verify

Repeat the preflight: the output should list Access-Control-Allow-Origin and Access-Control-Allow-Private-Network: true.

curl -s -o /dev/null -D - -X OPTIONS http://localhost:3000/api \
  -H "Origin: https://app.example.com" -H "Access-Control-Request-Method: GET" \
  -H "Access-Control-Request-Private-Network: true" | grep -i "^access-control"

Confirmed by developer.chrome.com

Did this fix it?

Cause 3 of 3

An https page calls an http localhost address

1Check

Chrome exempts a permission-gated local request from mixed content checks only when it can tell the target is local before resolving the name: a private IP literal, a .local name, or a targetAddressSpace hint. A public hostname that resolves to 127.0.0.1 is not exempt on its own.

grep -rn "fetch(" src | grep -i "http://"

2Fix

Call the local address directly, or add the targetAddressSpace option so Chrome knows the destination is local.

await fetch('http://localhost:3000/api', { targetAddressSpace: 'local' })

3Verify

Reload the page: the permission prompt should appear and, once allowed, the request should leave the page.

fetch('http://localhost:3000/api', { targetAddressSpace: 'local' }).then(r => r.status)

Confirmed by developer.chrome.com

Did this fix it?

How do I stop it coming back?

  • Test the public build against a tunnel or a staging API instead of calling localhost from a deployed page.
  • Allow exactly one origin in the dev server, never a wildcard, since the server sits on your own machine.
  • Keep the Access-Control-Allow-Private-Network answer out of production code.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

The browser blocks your API response: no Access-Control-Allow-Origin header

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.example.com/orders. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

The browser received the response but refused to hand it to your script, because it did not carry an Access-Control-Allow-Origin header naming the page's origin.

4 causes, each with a fix

Web apps

The cors-anywhere.herokuapp.com demo proxy stopped working

Missing required request header. Must specify one of: origin,x-requested-with

The public cors-anywhere.herokuapp.com server is only a demo, is rate limited, and since February 2021 requires an opt-in.

2 causes, each with a fix

Web apps

CORS fails when you open an HTML file straight from disk

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file:///

A page opened from disk has an opaque origin, so browsers treat every request it makes as cross-origin, and CORS only works over http or https.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.