toolhq.io

All posts
6 min readby Jameel Haider

HTTP 103 Early Hints: a head start while the server thinks

Slow pages often waste the one stretch of time nobody looks at. The browser has asked for the HTML, the origin is busy querying a database, and the connection sits idle. Everyone involved already knows the page will need a stylesheet and a font, but the browser cannot start fetching them until the HTML arrives and it reads the <head>.

103 Early Hints is a way to use that gap. The server sends a small set of headers straight away, the browser starts work on them, and the real response follows when it is ready.

What a 103 is

103 belongs to the 1xx family, the informational status codes. An informational response is not the answer to the request. It is a note sent ahead of the answer, on the same request, and the final status still comes afterwards. The HTTP Status Codes reference lists the whole family, and HTTP status codes explained covers how the five classes differ.

A 103 typically carries Link headers with rel=preload or rel=preconnect. On the wire, one request produces two responses:

HTTP/2 103
link: </assets/app.3f9c1a.css>; rel=preload; as=style
link: <https://cdn.example.com>; rel=preconnect

HTTP/2 200
content-type: text/html; charset=utf-8
cache-control: private, no-store
link: </assets/app.3f9c1a.css>; rel=preload; as=style

<!doctype html>
...

Two things follow from the fact that a 103 is informational.

  • It is not the final status. The response after it can be a 200, a 404, a redirect or anything else. Nothing in the 103 commits the server to an outcome.
  • A client that does not understand it ignores it. Older clients skip the 103 and read the final response as if nothing had been sent. That is what makes the feature safe to add.

Repeat the same Link headers on the final response as well. A client that ignored the 103 still gets the hint, only later.

When it helps

The gain comes from overlap. While the origin spends its think time building the page, the browser spends the same time opening connections and downloading the files the page depends on. When the HTML lands, the stylesheet is already there or well on its way.

That only works when two conditions hold.

  • The origin is slow for reasons you cannot remove. A personalised page that needs a session lookup and several queries is the typical case. If your server responds in a few milliseconds, there is nothing to overlap and no gain.
  • The resources are predictable regardless of the response body. The hints go out before the server knows what the page will contain, so they have to be true for every version of it. A site wide stylesheet qualifies. An image that depends on which product was requested does not.

rel=preconnect is the cheaper of the two hints. It only opens a connection to another origin, so a wrong guess costs little. rel=preload downloads a file, which pays off more when it is right and costs more when it is wrong.

Constraints in practice

Early Hints depends on the whole chain between your application and the browser. An intermediary or CDN that does not pass 1xx responses through will swallow the 103, and the browser never sees it. The request still succeeds, so nothing looks broken. You only notice that the page is no faster.

For that reason it is usually configured at the CDN, not in the application. The CDN sits closest to the visitor and can answer with the hints at once, while it waits for your origin to produce the page. Many application frameworks cannot send an informational response before the final one anyway.

It is also narrower than it first appears. In general it is only used on navigation requests, the ones that load a page, over HTTPS with HTTP/2 or HTTP/3. Hints on a subresource or an API call do nothing useful. HTTP/1.1 vs HTTP/2 vs HTTP/3 explains the protocol differences if you are not sure what your site serves.

The risk is hinting wrong

A hint is a promise that the page will use a file. Break the promise and you make the page slower.

  • Preloading a file the page does not use spends the visitor's bandwidth and competes with the files the page does need. The browser treats a preload as high priority, so the waste comes first.
  • A hint that points at a stale URL is worse than no hint. After a deployment, a cached or hand maintained hint list can keep naming last week's stylesheet. The browser fetches it, discards it, then fetches the real one.
  • A preload with the wrong attributes is fetched twice. A font preload needs as=font and crossorigin, even when the font is on your own origin, or the browser will not reuse it.

Two rules cover most of this. Keep the list short: the main stylesheet, perhaps one font, and a preconnect to an origin every page uses. And keep it to assets with stable, versioned URLs, generated from the same build manifest as the HTML, so the hints cannot drift from what the page references.

Cache first, hint second

Early Hints makes a wait more productive. It does not remove the wait. Cache-Control and a CDN cache remove it entirely, because a cached page comes back with no think time at all, and they are the better fix whenever they apply. Cache-Control headers explained covers the directives.

So the order of questions is:

  1. Can the HTML be cached, even briefly, even only for visitors who are not signed in? If so, do that.
  2. Can the slow part of the origin be made faster? That helps every request, hinted or not.
  3. Is the HTML uncacheable and the delay real? That is the case Early Hints exists for.

To check it is working, look at the request in your browser's developer tools, where a navigation that received a 103 shows the early headers alongside the final ones. If the 103 is missing there but your CDN says it is enabled, something in the chain is dropping it.