Reduce cross-origin startup time
A page loads fonts, scripts, or other assets from another origin and connection setup contributes to latency.
Next.js 16+ · Function
Preconnect is a resource hint that begins connecting to an origin before its resources are requested. In Next.js App Router client components, use ReactDOM.preconnect to emit the hint.
In a client component, ReactDOM.preconnect emits a resource hint that asks the browser to establish an early connection to the specified origin. The optional crossOrigin setting controls whether the generated link includes cross-origin credentials behavior. For Google Fonts, Next.js may add this hint automatically, so manual use is mainly for other origins or explicit control.
ReactDOM.preconnect(href: string, options?: { crossOrigin?: string })| Name | Type | Description |
|---|---|---|
href | string | The origin to connect to early. |
options | { crossOrigin?: string } | Optional settings; the documented property is crossOrigin, a string controlling cross-origin behavior. |
Emits a preconnect resource hint represented as a <link rel="preconnect"> element; the documentation does not specify a return value.
crossOrigin.preconnect, with an optional crossorigin attribute.import ReactDOM from 'react-dom'
export function PreloadResources() {
ReactDOM.preconnect('https://fonts.gstatic.com', {
crossOrigin: 'anonymous',
})
return null
}Call ReactDOM.preconnect from a client component to emit a preconnect hint for an origin before its resources are requested.
A page loads fonts, scripts, or other assets from another origin and connection setup contributes to latency.
An application uses a third-party origin that is not covered by Next.js's automatic Google Fonts behavior.
A manually authored document needs the HTML form of the browser resource hint.
Preconnect is a resource hint that proactively starts a connection to an origin. In the App Router documentation, the React API is ReactDOM.preconnect(href, options?), and it produces a <link rel="preconnect"> hint.
Follow-up: How would you express the same hint in HTML? Use <link rel="preconnect" href="..." crossorigin />, with the cross-origin attribute when appropriate.
The required argument is the target origin as a string. The optional options object can contain crossOrigin, which controls the cross-origin mode of the generated hint.
Follow-up: What happens if the target is not cross-origin? The hint can still identify the origin, but crossOrigin should match how the eventual resource request is made.
Google Fonts commonly benefits from preconnect because the browser can begin connecting before font resources are requested. Next.js automatically adds the Google Fonts preconnect hint in versions after 12.0.1, so manually adding another one can be redundant.
Follow-up: What fallback can support older browsers? Add a separate dns-prefetch link; the documentation says this is optional rather than required.
ReactDOM.preloadReactDOM.prefetchDNS<link rel="preconnect">crossOriginGoogle Fonts