Prepare a component's resource host
A component's descendants will load fonts, scripts, styles, or other resources from a known external origin.
React 19+ · Function
preconnect hints that the browser should establish a connection to a server whose resources you expect to request. Calling it early can reduce resource-loading latency if the browser honors the hint.
Import preconnect from react-dom and call it with the origin of a server whose resources you expect to request. React gives the browser a connection hint; if honored, connection setup can begin earlier. The call can occur during browser rendering or in an event handler, while server rendering requires a rendering-related context to take effect.
preconnect(href);| Name | Type | Description |
|---|---|---|
href | string | The URL of the server you want to connect to. |
Nothing.
import { preconnect } from 'react-dom';
export default function AppRoot() {
preconnect('https://cdn.example.com');
return <main>Welcome</main>;
}The component hints that the browser should establish a connection to the CDN before descendants request resources from it.
A component's descendants will load fonts, scripts, styles, or other resources from a known external origin.
A click will navigate to a page or state that needs resources from a known server.
A server-rendered component tree knows an external host will be used during rendering.
preconnect do?preconnect is a react-dom API that hints to the browser that it should open a connection to a specified server. If the browser accepts the hint, resources from that server may load sooner.
Follow-up: How would you choose between preconnect and prefetchDNS? Use preconnect when you expect to request resources from the host; use DNS prefetch when you may not actually request anything and only want DNS resolution prepared.
Call it with the server URL, such as preconnect('https://cdn.example.com'). It is useful when you know a component's children will load external resources from that host.
Follow-up: When can an event handler use it? Call it before transitioning to a page or state that will need external resources, starting connection setup earlier than the destination render.
In the browser, it can be called while rendering, in an Effect, or in an event handler. For server rendering and Server Components, it only takes effect during component rendering or an async context originating from component rendering.
Follow-up: What happens if the same host is passed repeatedly? The repeated calls have the same effect as one call, so deduplicating them is not required for correctness.
prefetchDNSpreinitpreinitModulepreloadpreloadModule