A page pulls resources from six third-party origins. Which of them would you give a <link rel="preconnect">, which are better served by <link rel="dns-prefetch">, and why is preconnecting all six a bad idea?
answer
- one resolves, the other connects
- DNS, TCP, TLS in one line
- sockets are not free
- the idle connection does not wait forever
- fonts want the anonymous connection
basics
~20 sPreconnect the two or three origins that serve something needed early — it pays DNS, TCP and TLS up front. Use dns-prefetch for origins used later; it resolves DNS only and costs almost nothing. Preconnecting everything wastes connections and contends for bandwidth.
solid answer
~60 sI would rank the origins by how soon and how certainly the page needs them. The one or two that serve a first-paint resource — the font host, the image CDN carrying the LCP image — get `<link rel="preconnect">`, because opening a connection means a DNS lookup, a TCP handshake and a TLS negotiation, which on a mobile connection is easily two or three round trips saved. Origins whose resources come later or conditionally — analytics, a chat widget, a tag manager's downstream hosts — get `<link rel="dns-prefetch">`, which resolves the hostname only and is close to free. Preconnecting all six is counterproductive: each connection consumes a socket and CPU on both ends, the handshakes themselves compete for the same bandwidth as the critical resources, and a browser will drop an unused connection after a short idle window, so a preconnect for something requested ten seconds later has simply been thrown away. Also remember the `crossorigin` attribute — the connection you warm for a font host must be the anonymous one the font request will actually use.
code
html · 7 lines<!-- serves the LCP image and a first-paint font: worth a full connection -->
<link rel="preconnect" href="https://cdn.example.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<!-- used later or conditionally: resolve the name, skip the socket -->
<link rel="dns-prefetch" href="https://analytics.example.com">
<link rel="dns-prefetch" href="https://widget.example.com">go deeper
Know that dns-prefetch resolves a hostname while preconnect also opens the TCP connection and negotiates TLS, and that preconnect is reserved for origins the page needs early.
Explain the three round trips a connection costs and why that makes preconnect valuable but limited, including the crossorigin requirement when the origin serves fonts.
Show that you rank origins by how soon a first-paint resource comes from them, and that you account for the costs — socket, CPU, bandwidth and the short idle window before an unused connection is dropped.
Argue the origin count itself. Decide when a third-party host should be proxied, self-hosted or removed rather than papered over with hints, and set a policy so hint lists do not accumulate in shared templates.
## The two hints spend very different amounts Before a browser can request the first byte from a new origin it has to do three things: resolve the hostname (DNS), open a TCP connection, and negotiate TLS. On a fast desktop connection that might be 50 ms; on a mobile network with high latency it is routinely 200–300 ms, because each step costs at least one round trip. `<link rel="dns-prefetch" href="https://analytics.example.com">` does the **first** step only. It is cheap — a DNS query, no socket, no state to keep — and it can be applied fairly liberally. `<link rel="preconnect" href="https://cdn.example.com">` does **all three**, leaving a live, TLS-negotiated connection waiting. That is the bigger win and the bigger cost: an open socket on the client, a session on the server, and handshake traffic that shares the same pipe as everything else the page is loading. ## Ranking the origins The decision is not "which origins do we use" but "which origins block something the user is waiting for": 1. **Serves a first-paint or LCP resource, always.** Preconnect. The font host, the image CDN carrying the hero image, the API host the app calls immediately after boot. These are also the cases where the connection is needed *soon*, which is what makes preconnect pay. 2. **Used later or conditionally.** dns-prefetch. Analytics, a support widget that loads after interaction, a payment iframe on a page where most users never reach checkout. Resolving DNS ahead of time trims the tail without holding a connection open. 3. **Not used on this page at all.** Neither. A global list of hints copied into every template is how pages end up with a dozen of them. One origin that usually needs nothing: your **own**. The connection that delivered the HTML is already open and will be reused, so a same-origin preconnect buys nothing. A *different subdomain* of your site is a different origin and may well deserve one. ## The crossorigin trap A preconnect warms a specific kind of connection. Resources fetched anonymously in CORS mode — fonts, most notably — do not use a credentialed connection, so the hint must say so: ```html <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> ``` Drop the `crossorigin` and you have carefully warmed a connection the font request will not touch, then opened a second one anyway. If a single origin serves both anonymous and credentialed resources you genuinely need two preconnect lines, which is itself a reason to be sparing. ## Why more is worse Three effects compound: - **Bandwidth contention.** Handshakes are not free bytes. Six simultaneous TLS negotiations at the moment the page is trying to download CSS and the hero image take capacity from exactly the resources that determine when the user sees content. - **CPU and battery.** TLS is cryptographic work on the device, and on a low-end phone that work competes with rendering. - **Expiry.** A browser will not hold an unused connection indefinitely; Chromium closes an idle preconnected socket after roughly ten seconds. Preconnect to an origin whose resource is requested a minute later and the handshake happened twice, for nothing. The practical guidance that has survived is to keep preconnect to a handful of origins — realistically three or four — and let dns-prefetch cover the long tail. ## When neither hint is the answer Both hints exist to hide a cost, not to remove it. If a page talks to six third-party origins, the more durable fix is usually to talk to fewer: proxy an asset through your own origin so it reuses the existing connection, drop a vendor script nobody reads the data from, or self-host a font instead of reaching for another host. A hint that shaves 150 ms off a request the page did not need is a worse outcome than deleting the request. Finally, remember what these hints cannot do. Preconnect makes the *connection* ready; it does not fetch anything, so the resource still needs to be discovered before it is requested. If the real problem is that the URL is buried in a stylesheet or created by a script, warming the connection helps far less than making the resource discoverable earlier.
- Is preconnecting to your own origin ever useful?Rarely for the origin that served the HTML — that connection is already open and will be reused. It becomes useful for a *different* subdomain, since that is a separate origin with its own DNS, TCP and TLS cost. If you find yourself wanting a same-origin preconnect, the real question is usually why the resource is discovered so late.
- How long does a warmed connection stay useful?Not long. Chromium closes an unused preconnected socket after roughly ten seconds, and servers have their own idle timeouts. So a preconnect only pays when the origin is contacted shortly after the page starts loading; for a widget the user summons a minute later, the handshake will simply be repeated and the hint has cost more than it saved.
- What is the more durable alternative to a long list of connection hints?Reduce the number of origins. Self-host the font, proxy the asset through your own domain so it reuses the existing connection, or remove the third party entirely. Hints hide connection cost; they do not remove it, and a page that touches six hosts still pays six times over on a cold, high-latency connection.
saying these in an interview costs you the question
- Preconnects to every origin the page touches
- Thinks preconnect downloads the resource
- Omits crossorigin on a font-host preconnect
- Treats dns-prefetch as a slower preload
- Preconnects to the page's own origin