# preconnect()

> The `preconnect()` API in React DOM eagerly connects to a server from which resources are expected to be loaded.

## How do I preconnect to a third-party origin in React?

The API that opens a connection to another origin in advance, when the exact resources are not yet known. Equivalent to `<link rel="preconnect">`.

- Topic: APIs [React DOM]
- Type: API

## Example

Wrong:

```jsx
function App() {
  preconnect('https://fonts.gstatic.com');
  return <Page />;
}
```

Right:

```jsx
function App() {
  preconnect('https://fonts.gstatic.com', { crossOrigin: 'anonymous' });
  return <Page />;
}
```

Font files load with CORS, so the early connection must use the same `crossOrigin` mode or the browser opens a new one.

## Common mistakes

- Preconnecting to many origins, which wastes connections and slows the critical ones.

## Key points

- `preconnect` is imported from 'react-dom' and called in render or an event handler.
- It targets origins the page will request soon, not every possible third-party domain.
- Origins loaded with CORS, such as fonts, pass `crossOrigin` matching the later request.

## Interview questions

### preconnect vs prefetchDNS: which should I use?

`preconnect` resolves DNS and opens the TCP and TLS connection. `prefetchDNS` only resolves DNS, which is cheaper for less certain origins.

### Does preconnect help for my own origin?

No. The browser is already connected to the page's origin, so `preconnect` only helps for other origins.

## Related cards

- [\[rel="preconnect"\]](https://wonderium.dev/html/rel-preconnect.md)
- [prefetchDNS()](https://wonderium.dev/react/prefetch-dns.md)
- [preload()](https://wonderium.dev/react/preload.md)
- [preinit()](https://wonderium.dev/react/preinit.md)
- [TLS](https://wonderium.dev/web/tls.md)

---

- Level: [React](https://wonderium.dev/react.md)
- HTML version: https://wonderium.dev/react/preconnect
- Last updated: 2026-11-25
