Skip to content
Tutorials Center
My learning

Topics

React Reference ยท react-dom Resource and Form APIs

React prefetchDNS() function

Reference for react-dom prefetchDNS(): resolve a host name before you request anything from it, how it differs from preconnect, and when it is worth calling.

prefetchDNS(href) asks the browser to look up the IP address of a host ahead of time. DNS resolution is the first step of reaching a new server, and on slow networks it can take tens or hundreds of milliseconds. Once the name is in the browser's cache, the later request skips that wait.

It is the lightest of the connection hints: no TCP handshake, no TLS, no data. React renders it as <link rel="dns-prefetch" href="..."> in the head and removes duplicates. Use it for hosts you might use; use preconnect for hosts you are sure to use.

JSX
import { prefetchDNS } from 'react-dom';

prefetchDNS('https://analytics.example.com');

Try it

DNS hints for third-party hosts

Three calls, two links: the repeated host is listed once.

Warm up a host when the user shows interest

Syntax

JSX
prefetchDNS(href)
TypeScript
function prefetchDNS(href: string): void;
Name Description
href A URL or a protocol-relative URL whose host should be resolved. Must be a non-empty string. Only the host matters.

Returns: nothing (undefined).

Errors / edge cases: a second argument is rejected in development with a console error ("reserved for future options"), and an empty or non-string href is reported too. In both cases no link is added.

Usage notes

  • There is no options object. Unlike preconnect, prefetchDNS takes exactly one argument. Calling it with two is flagged as a mistake.
  • Deduplication is by the exact string. https://a.example.com/x and https://a.example.com/y are two links for one host. Pass a consistent origin.
  • Small gain, tiny cost. A DNS lookup is cheap, so it is safe to use for many third-party hosts, such as an analytics script, a map tile server or an ads provider that may load later.
  • Escalate when sure. If the first request to that host is certain and early, preconnect saves more (it also does TCP and TLS).
  • Browsers decide. Hints are advisory. A browser may skip resolution on a metered connection or when it considers the hint wasteful.
  • Same-origin is pointless, since the host is already resolved.
  • Server rendering: in streaming SSR the hint is placed near the top of the document, so resolution starts while the rest of the page is still arriving.