日本語

Speeding Up LCP with Early Hints in Core Web Vitals

​​Core Web Vitals​Chrome,HTTP/2,Node.js,Playwright,web-vitals

On pages where the server takes time to return HTML, for example because of a database query, the browser cannot start loading the page's images or CSS during that wait. With the HTTP 103 Early Hints status, you can send a Link header that says "this page uses these images and this CSS" before the final HTML (200). The browser then starts loading them in parallel with the server's work. In a simulated page that took 600ms to return HTML, the median LCP measured with web-vitals dropped from 1,460ms to 1,256ms.

This technique applies to pages with SSR or similar setups where TTFB is long and the LCP image waits for the HTML to arrive. The web-vitals TTFB dropped from 1,209ms to 648ms, but that is not because the HTML arrived sooner. I also cover this point, along with cases where it did not work with HTTP/1.1 and with certificate errors.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, web-vitals 6.2.3, and Playwright 1.63.0. The browser was Chrome for Testing 153, which comes with Playwright. The server was written with Node.js's http2 and https modules. The server processing before it returned HTML was only a simulated 600ms wait. In front of the server, I put a relay that delayed data by 100ms one way (200ms round trip), and I also made it wait one round trip at the start of each connection. The certificate was a self-signed one I created myself. I did not test HTTP/3, rel=preconnect in Early Hints, resources on other domains, Early Hints sent by a CDN (such as Cloudflare), browsers other than Chrome, Lighthouse, or real user data (field data).

What to send in 103 Early Hints

According to MDN's explanation of 103 Early Hints, 103 is an interim response that the server can send while it prepares the final response. Its contents are mainly a Link header, which tells the browser in advance which resources the final response (the HTML) will use.

text
HTTP/2 103
link: </style.css>; rel=preload; as=style, </hero.jpg>; rel=preload; as=image

HTTP/2 200
content-type: text/html; charset=utf-8
(HTML の本文)

According to Chrome's explanation of Early Hints, Chrome uses only preload and preconnect in a 103 response, not prefetch. Early Hints can also only be used for page navigation requests. The same page also says that if a resource is not cacheable, it will be fetched twice. In this test, I set Cache-Control: max-age=3600 on both style.css and hero.jpg.

Sending 103 from a Node.js server

The test server is an HTTP/2 server (http2.createSecureServer). When it receives an HTML request, it immediately sends 103 with writeEarlyHints(), waits 600ms, and then returns the HTML.

js
import http2 from 'node:http2';

const LINK = [
  '</style.css>; rel=preload; as=style',
  '</hero.jpg>; rel=preload; as=image',
];

http2.createSecureServer(tls, (req, res) => {
  // Return style.css and hero.jpg with Cache-Control: max-age=3600 (omitted)
  if (req.url === '/hints.html') {
    res.writeEarlyHints({ link: LINK }); // Send 103 first
    setTimeout(() => {                   // Instead of server processing (600ms)
      res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
      res.end(html);
    }, 600);
  }
}).listen(9443);

For measurements, I accessed the server through a relay on port 8443.

The HTML loads style.css in <head> and only has an <img> for the hero image (hero.jpg, 1200×675, about 130KB) in the body. The page without Early Hints (none.html) returns the same HTML after the same 600ms wait.

When passing two or more resources in link, pass them as an array. When I passed one string joined with commas, both http2 and http raised an error (ERR_INVALID_ARG_VALUE).

console
$ node linkcheck.mjs
http2 joined string: ERR_INVALID_ARG_VALUE
http2 array: ok
http   joined string: ERR_INVALID_ARG_VALUE
http   array: ok

The Node.js documentation example for writeEarlyHints() also passes an array when there are two or more.

You can confirm that the hints are sent with curl -v. (The -w format string is long, so I abbreviated it as "…".) The 103 and the link header arrived before the 200. The first byte (ttfb) came in about 0.62 seconds, and the total was about 1.22 seconds.

console
$ curl -sk --http2 -v -o /dev/null -w "..." https://localhost:8443/hints.html
< HTTP/2 103 
< link: </style.css>; rel=preload; as=style, </hero.jpg>; rel=preload; as=image
< HTTP/2 200 
http=2 tls=0.409766 ttfb=0.617130 total=1.223257

The image request now happened before the HTML arrived

I launched Chrome myself, connected to it from Playwright, and measured LCP and TTFB with Google's web-vitals library (attribution build). I also recorded the browser's Navigation Timing and Resource Timing values, and the times when the server received each request. These are all from the first run. (I omitted the nav lines.)

console
none.html :8443 cdp cert=spki run1: LCP=1472ms TTFB(web-vitals)=1212ms lcpUrl=/hero.jpg loadDelay=20 loadDuration=206 renderDelay=34
  /style.css  initiatorType=link        start=1220 requestStart=1232 responseEnd=1438 transferSize=415
  /hero.jpg   initiatorType=img         start=1221 requestStart=1232 responseEnd=1438 transferSize=129847
  [サーバーのログ]
    15:44:19.324 [HTTP/2.0] GET /none.html  +0ms
    15:44:20.148 [HTTP/2.0] GET /style.css  +824ms
    15:44:20.149 [HTTP/2.0] GET /hero.jpg  +825ms
hints.html :8443 cdp cert=spki run1: LCP=1260ms TTFB(web-vitals)=653ms lcpUrl=/hero.jpg loadDelay=569 loadDuration=2 renderDelay=36
  /style.css  initiatorType=early-hints start=1219 requestStart=1221 responseEnd=1224 transferSize=0
  /hero.jpg   initiatorType=early-hints start=1221 requestStart=1222 responseEnd=1224 transferSize=0
  [サーバーのログ]
    15:44:22.489 [HTTP/2.0] GET /hints.html  +0ms
    15:44:22.742 [HTTP/2.0] GET /style.css  +253ms
    15:44:22.743 [HTTP/2.0] GET /hero.jpg  +254ms

The +…ms values in the server log are the elapsed time since the HTML request arrived. Without Early Hints, the hero.jpg request arrived 825ms later, which was after the server returned the HTML (600ms). With Early Hints, it arrived 254ms later, so the image transfer started while the server was still preparing the HTML.

In the browser's Resource Timing, the initiatorType of both resources loaded through Early Hints was early-hints, and their transferSize was 0. This means the images were already available when the browser found the <img> in the HTML. (The loadDuration in the LCP breakdown was also 2ms.) According to Chrome's explanation, Chrome DevTools also shows early-hints in the Initiator column of the Network panel for resources loaded through Early Hints. Note that the start of the hero.jpg row (1,221ms) is later than the HTML arrival and does not match the server log time when the image was requested. Do not read it as the time the load actually started.

I restarted the browser every time and measured five runs per page. The medians are as follows:

Page LCP TTFB (web-vitals) Time the 200 response started arriving
Without Early Hints 1,460ms 1,209ms 1,209ms
With Early Hints 1,256ms 648ms 1,208ms
Three pairs of horizontal bar charts. TTFB (web-vitals value) is 1,209ms without Early Hints and 648ms with them. The time the 200 response started arriving is 1,209ms without and 1,208ms with, the same. LCP is 1,460ms without and 1,256ms with, with the 204ms reduction shown in pink text
Medians of 5 runs. The time the HTML arrives did not change, but TTFB and LCP went down (labels in the image are in Japanese)

The median LCP dropped by 204ms. Without Early Hints, after the HTML arrived, it took about 200ms to fetch the image (the round trip added by the relay). That time was almost entirely removed. With Early Hints, LCP was about 50ms after the HTML arrived, so the time the HTML arrives determines LCP.

Lower TTFB does not mean the HTML arrived sooner

As the table shows, the web-vitals TTFB dropped from 1,209ms to 648ms. However, the time the 200 response started arriving (finalResponseHeadersStart in Navigation Timing) was about 1,208ms in both cases and did not change.

According to web.dev's explanation of TTFB, 103 Early Hints counts as the "first byte," and TTFB is measured up to responseStart. Chrome once changed responseStart to the start of the final response, but because of compatibility problems with many tools, it reverted that change in Chrome 133. Now the start of the final response is measured by a separate value, finalResponseHeadersStart. In Chrome 153, which I tested, responseStart on a page with Early Hints equaled the time the 103 arrived (firstInterimResponseStart).

console
hints.html :8443 cdp cert=spki run1: ...
  nav: {"protocol":"h2","responseStart":653,"firstInterimResponseStart":653,"finalResponseHeadersStart":1210,"responseEnd":1211}

In other words, Early Hints shortens the value measured as TTFB, not the server's processing time. Do not conclude that the server got faster just because TTFB dropped. To find out whether the server processing itself got shorter, record finalResponseHeadersStart as well.

The web-vitals LCP breakdown (attribution) also uses the same responseStart as its boundary. On the page with Early Hints, the TTFB part of the breakdown became shorter, and the wait from TTFB until the image load began (loadDelay) was long at 569ms (20ms without Early Hints).

With HTTP/1.1, only TTFB dropped and LCP did not change

I ran the same server over HTTP/1.1 (https.createServer), accessed it through the relay on port 8444, and measured each page three times. With curl, 103 Early Hints and the Link header also arrived over HTTP/1.1.

console
none.html :8444 cdp LCP median 1648ms (min 1644, max 1648) / TTFB median 1207ms (min 1207, max 1207) / finalResponseHeadersStart median 1207ms (min 1207, max 1207)
hints.html :8444 cdp LCP median 1648ms (min 1644, max 1652) / TTFB median 648ms (min 648, max 651) / finalResponseHeadersStart median 1209ms (min 1207, max 1210)

LCP was 1,648ms in both cases, so Early Hints had no effect. In the first run, the initiatorType of hero.jpg stayed img, and the server received the image request only after it returned the HTML (1,027ms later). The Chrome in this test did not preload from a 103 received over HTTP/1.1. The MDN explanation also says that many browsers only handle 103 for HTTP/2 and later.

Warning

Even over HTTP/1.1, the web-vitals TTFB dropped from 1,207ms to 648ms, because the time the 103 arrived became responseStart. If you conclude that Early Hints worked just from the lower TTFB number, you will miss that the resources were not loaded early. Check whether initiatorType is early-hints.

Ignoring self-signed certificate errors caused a second fetch

When testing locally, be careful with the certificate. When I started Chrome with --ignore-certificate-errors to ignore a self-signed certificate, the Early Hints effect disappeared. (I omitted the nav lines.)

console
hints.html :8443 cdp cert=ignore run1: LCP=1488ms TTFB(web-vitals)=651ms lcpUrl=/hero.jpg loadDelay=574 loadDuration=246 renderDelay=17
  /style.css  initiatorType=link        start=1220 requestStart=1222 responseEnd=1428 transferSize=415
  /hero.jpg   initiatorType=img         start=1224 requestStart=1225 responseEnd=1471 transferSize=129847
  [サーバーのログ]
    15:45:17.849 [HTTP/2.0] GET /hints.html  +0ms
    15:45:18.099 [HTTP/2.0] GET /style.css  +250ms
    15:45:18.100 [HTTP/2.0] GET /hero.jpg  +251ms
    15:45:18.665 [HTTP/2.0] GET /style.css  +816ms
    15:45:18.667 [HTTP/2.0] GET /hero.jpg  +818ms

The server log shows that Chrome, after receiving the 103, requested hero.jpg 251ms after the HTML request. However, after the HTML arrived (818ms after the HTML request), it requested the same image again, and the page used that second fetch (initiatorType was img, and transferSize was 129,847 bytes). In the other measurements in this article, I passed the certificate's public key hash with --ignore-certificate-errors-spki-list to let Chrome accept it, and in those cases the second fetch did not happen. If Early Hints seems to have no effect on your local setup, first check the server log for the same resource being requested twice.

For the measurements, I launched Chrome myself with --remote-debugging-port and connected with Playwright's chromium.connectOverCDP(). I also tried once with a page opened by Playwright's chromium.launch() and browser.newContext(). There, initiatorType was early-hints, and LCP was 1,256ms, so Early Hints was used in the same way.

​Posted by 86