Advanced

The network path: connections and negotiation

Before bytes move, the browser resolves a name, opens a connection, and often negotiates a protocol. Those steps cost time even for a zero-byte response, and they repeat per origin. Cutting origins and reusing connections is often a bigger win than shrinking a file.

What you will see

Only for an origin the page requests from. An unused preconnect costs a connection and nothing else.

About 1 min of reading

Try It

Count origins and compress one asset

1

List the origins

grep -oE 'https?://[a-z0-9.-]+' index.html views/partials/*.ejs | sed 's|https\?://||' | sort -u

Before you run it, say out loud what it will print. Then open the answer.

What it prints
The distinct origins. More than two on a first-party page is usually avoidable.
2

Check compression

curl -s -H 'Accept-Encoding: br,gzip' -o /dev/null -w '%{size_download} %{content_type}\n' http://localhost:3000/public/js/main.js?v=20261009

Before you run it, say out loud what it will print. Then open the answer.

What it prints
A size smaller than the file on disk. If equal, the server is not compressing.
3

Enable compression at the server

grep -nE 'gzip|br|compress' server.js middleware/*.js | head -5

Before you run it, say out loud what it will print. Then open the answer.

What it prints
The place that sets the encoding. If nothing matches, add it at the static file handler.
4

Preconnect only a used origin

printf ' ' | tee -a views/partials/head.ejs

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Only for an origin the page requests from. An unused preconnect costs a connection and nothing else.
Do not preconnect to an origin you only use below the fold. It competes for the connection the first-paint assets need.

Data Flow

URL to bytes on the wire
1 Resolve the name for the origin
↓
2 Open or reuse the connection
↓
3 Negotiate the protocol
↓
4 Send the request and receive compressed bytes
Why this works
The order of network fixes

Remove origins first, reuse connections second, compress third. Each is a server or markup change, and each is cheaper to get wrong than a code change.

Where keep-alive matters

On HTTP/1, requests to the same origin share a connection. On HTTP/2 they share it and multiplex. Either way, same-origin is the thing that makes the handshake amortize.

Examples

One origin, one handshake

Every distinct origin costs a DNS lookup and usually a connection. A page that pulls fonts, scripts, and images from four origins pays four setups for content that could share one.

Preconnect the origin you need

preconnect starts the handshake before the resource is discovered. It pays only for an origin the page will actually use; preconnecting to an unused origin is a wasted connection.

Compression is per response

A compressible text asset costs bytes until it is compressed. Brotli or gzip at the server turns the bytes axis off for repeat visits, and it is a server setting, not a page edit.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Reading the numbers you collected
Previous Lesson
Reading the numbers you collected
Next Lesson
The critical path and what to inline
The critical path and what to inline →