Course contentsShow
← Web Development
Lesson 1963 of 2,98236. Performance Optimization TechniquesFree lesson

Preconnect for Early Connections

Establish early connections to important third-party origins using preconnect to save round-trip time.

Preconnect for Early Connections

What you'll learn: How to use the preconnect resource hint to establish early connections to critical third-party domains and reduce latency.

When your page loads resources from third-party origins (fonts, analytics, CDNs), the browser must perform three time-consuming steps before downloading anything: DNS lookup, TCP handshake, and TLS negotiation. These round-trips can add hundreds of milliseconds of delay.

Preconnect tells the browser to start this connection process early, before it even discovers the resource. This way, when your page actually requests that third-party file, the connection is already warm and ready.

How Preconnect Works

Think of it like calling ahead to a restaurant. Instead of showing up, waiting for a table, then ordering, you reserve the table in advance so you can sit down and order immediately.

Add preconnect in your HTML <head>:

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

The crossorigin attribute is required when preconnecting to origins that will send CORS requests (like font files).

When to Use Preconnect

Use preconnect for critical third-party resources you know you'll need soon:

  • Web fonts
  • Critical API endpoints
  • CDN origins for hero images
  • Analytics scripts loaded early

Don't overuse it. Each preconnect consumes browser resources and network bandwidth. Limit yourself to 2-3 preconnects per page for your most important origins. Remember: you already learned dns-prefetch for less critical domains—that's more lightweight when you only need the DNS step.

Key Takeaway: Use <link rel="preconnect"> to establish early connections to critical third-party origins, completing DNS, TCP, and TLS steps before resources are requested, saving valuable round-trip time.