Course contentsShow
← Web Development
Lesson 2130 of 2,98239. Network PerformanceFree lesson

Detecting and Leveraging HTTP/2 and HTTP/3

Discover how browsers negotiate protocol versions and how to optimize assets for each version.

Detecting and Leveraging HTTP/2 and HTTP/3

You'll learn how to identify which protocol version a browser is using and optimize your assets accordingly.

Now that you understand the benefits of HTTP/2 and HTTP/3, you need to know how browsers actually negotiate these protocols and how to adjust your optimization strategies based on what's available.

Protocol Negotiation

Browsers use ALPN (Application-Layer Protocol Negotiation) during the TLS handshake to determine which HTTP version to use. The server advertises supported protocols, and the browser selects the best one it understands—typically HTTP/3 over QUIC first, falling back to HTTP/2, then HTTP/1.1.

Think of it like two people meeting and quickly agreeing on a common language: they'll speak the most sophisticated language they both know.

Detecting the Protocol in Your Code

You can check which protocol was used for a request:

fetch('https://example.com/api/data')
  .then(response => {
    // Available in some browsers via PerformanceResourceTiming
    const entries = performance.getEntriesByName(response.url);
    if (entries.length > 0) {
      console.log('Protocol:', entries[0].nextHopProtocol);
      // Might log: "h2" (HTTP/2), "h3" (HTTP/3), or "http/1.1"
    }
  });

Optimization Strategy Shifts

Your asset strategy changes based on protocol:

  • HTTP/1.1: Bundle aggressively, use domain sharding, create sprite sheets
  • HTTP/2/3: Serve more granular files, skip domain sharding, use individual icons instead of sprites

You can conditionally load different bundles or adjust resource hints based on detected protocol support, though most modern sites simply optimize for HTTP/2+ and accept slightly suboptimal HTTP/1.1 performance.

Key Takeaway: Browsers automatically negotiate the best available protocol version through ALPN; you can detect which version was used via performance APIs and adjust your bundling and resource strategies accordingly, though most production sites now optimize primarily for HTTP/2 and HTTP/3.