BF50 Black Friday · 50% off all plans
from $5/mo · Auto-applied ---:--:-- Claim 50% off →
Skip to content

Search Seonio

Type a keyword, domain, or paste a URL

Start free with $1 to see full results. Powered by Seonio
Web Development

Preconnect: How to warm up third-party connections

Preconnect tells the browser to connect to another server before the page asks it for files. Used on one or two key origins, it can save 100 to 500 ms.

Monthly searches · preconnect
1.0k
Keyword research data
preconnect
Preconnect: How to warm up third-party connections
Click the image to view it full size

A preconnect is a one-line hint in your page's HTML that says, in effect: "you'll soon need files from this other server, so start connecting now." The browser then does the connection work in advance, so when the page actually asks for a file from that server, the download can start right away.

<link rel="preconnect" href="https://cdn.example.com">

It only helps for other origins, meaning other domains or subdomains such as a content delivery network, a font host or an image service. The connection to your own site is already open, because that's where the page came from.

What preconnect saves

Before a browser can download anything from a new server, it has to:

  1. look up the domain name and turn it into an IP address (DNS),
  2. open a connection to the server (TCP), and
  3. set up encryption (TLS), for an https address.

Each step means a message to the server and back. That can be up to three round trips before the first byte of the file moves, and on a slow mobile network each one hurts. Google's web.dev guide says early connections to important third-party origins can speed up loading by 100 to 500 ms. web.dev

When to use it

Preconnect fits when you know which server you'll need but not the exact file, or when the browser would discover the file late:

  • Web fonts, which the browser only finds after it has downloaded and read your CSS.
  • An image CDN, where the exact image URL depends on screen size or is picked by a script.
  • Versioned files on a CDN, where the path changes with each release.
  • Streaming media from another origin that your scripts start later.

If you know the exact file and the page needs it now, preload it instead: preload downloads the file, preconnect only prepares the line.

Preconnect compared with the other hints

Hint What the browser does Use it for
preconnect DNS + TCP + TLS to an origin One to a few critical third-party origins
dns-prefetch DNS lookup only Other third-party origins the page uses
preload Downloads a specific file now A critical file of this page
prefetch Downloads a file for a later page The likely next page

All four are resource hints in the broad sense: requests, not orders.

How to add it

Put the <link> early in the <head>, before the styles or scripts that will use the origin. Or send it as an HTTP header, which works before the HTML is even parsed:

Link: <https://cdn.example.com>; rel=preconnect

Add crossorigin for fonts. Fonts and other requests made in CORS (anonymous) mode use a separate connection from ordinary requests. A preconnect without crossorigin opens the ordinary kind, which the font can't reuse; web.dev says you then save only the DNS lookup. So a font host gets:

<link rel="preconnect" href="https://fonts.example.com" crossorigin>

It follows that an origin serving both kinds of requests, say fonts and images from one host, needs two preconnects: one with crossorigin and one without.

Add a fallback in a separate tag. A dns-prefetch for the same origin covers browsers that skip the preconnect. Write it as its own <link>; putting both values in one rel attribute triggers a Safari bug that cancels the preconnect.

Where preconnect goes wrong

  • Too many origins. Each connection costs CPU time and some bandwidth for the TLS certificates, and it competes with the requests the page needs most. Google's guides say to limit preconnect to the most critical origins and use dns-prefetch for the rest.
  • Unused connections. Browsers close a connection that isn't used within about 10 seconds, so a preconnect for something that loads much later, or never, is wasted work.
  • Expecting a guarantee. The HTML standard lets browsers do only part of the handshake, or skip it, when resources are tight.
  • Preconnecting to yourself. It adds nothing for your own origin.

If your server takes a while to build each page, it can send these hints even earlier with a 103 Early Hints response, before the page itself is ready.

Check whether it helps

Measure before and after on a slow connection. In your browser's developer tools, the network panel shows the connection time for each request; a preconnected origin should show little or none on its first file.

Lighthouse used to have a "Preconnect to required origins" audit. Since Lighthouse 13 it lives inside the Network dependency tree insight, which looks at how late the browser finds the files your page needs. For loading speed as Google measures it, see Largest Contentful Paint and Core Web Vitals.

Every current browser supports preconnect, and has since January 2020.

Seonio

Put it to work on your own site

Keyword research, traffic, backlinks and clustering in one place. Start with a free $1 to test, no card required.