Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readPerformance

Send 103 Early Hints before the HTML

Start CSS and font fetches during server think time by sending a 103 Early Hints response with Link preload headers from Node.

Your server takes 500 ms to build a page, and for that whole time the browser has nothing to do. You already know the page needs /app.css and /fonts/inter.woff2. The browser finds out when the HTML arrives.

A 103 Early Hints response can let the browser start those requests while the server prepares the page. It is an informational response the server sends before the final one, and it carries Link headers naming resources to preload.

diagram

When the CSS and font fetches start, with and without a 103

The same 500 ms server delay with and without a 103: with it, the CSS and font fetches start at about 10 ms instead of after the 200 HTML.

Send the 103

Node’s response.writeEarlyHints() accepts a link array with one string per resource. Save this as server.mjs, put an Inter WOFF2 file named inter.woff2 beside it, and run node server.mjs.

import { readFileSync } from 'node:fs'
import { createServer } from 'node:http'

const font = readFileSync(new URL('./inter.woff2', import.meta.url))
const css = '@font-face { font-family: Inter; src: url(/fonts/inter.woff2); } body { font-family: Inter, sans-serif; }'

const link = [
  '</app.css>; rel=preload; as=style',
  '</fonts/inter.woff2>; rel=preload; as=font; crossorigin',
]

createServer(async (req, res) => {
  if (req.url === '/app.css') {
    res.writeHead(200, { 'content-type': 'text/css' })
    return res.end(css)
  }
  if (req.url === '/fonts/inter.woff2') {
    res.writeHead(200, { 'content-type': 'font/woff2' })
    return res.end(font)
  }
  if (req.url !== '/') {
    res.writeHead(404)
    return res.end()
  }

  res.writeEarlyHints({ link })

  await new Promise((resolve) => setTimeout(resolve, 500))

  res.writeHead(200, { 'content-type': 'text/html', link: link.join(', ') })
  res.end('<!doctype html><link rel="stylesheet" href="/app.css"><h1>Hello</h1>')
}).listen(4120, '127.0.0.1')

For the HTML request, the server writes the 103, waits 500 ms and sends the 200 with the same Link header. Requests for the stylesheet and font get their own responses.

See it on the wire

Start the server and ask for the page with curl -i --http1.1 http://127.0.0.1:4120/. The -i flag includes the response headers. The excerpt omits automatic headers.

HTTP/1.1 103 Early Hints
Link: </app.css>; rel=preload; as=style, </fonts/inter.woff2>; rel=preload; as=font; crossorigin

HTTP/1.1 200 OK
content-type: text/html
link: </app.css>; rel=preload; as=style, </fonts/inter.woff2>; rel=preload; as=font; crossorigin

<!doctype html><link rel="stylesheet" href="/app.css"><h1>Hello</h1>

The first block is the 103 and has no body. The second is the real response, which arrives half a second later.

To see the delay, a Node client can listen for the information event, which fires for each 1xx response.

import { request } from 'node:http'

const start = performance.now()
const ms = () => `${Math.round(performance.now() - start)} ms`

const req = request({ host: '127.0.0.1', port: 4120, path: '/' }, (res) => {
  console.log(ms(), res.statusCode, 'final response')
  res.resume()
})
req.on('information', (info) => console.log(ms(), info.statusCode, info.headers.link))
req.end()

It prints the 103 with the two links within a few milliseconds, then 200 final response at about 500 ms. For about 500 ms a browser that acts on the hint could be fetching the stylesheet and the font.

What the browser does with it

Chrome’s Early Hints guide recommends HTTP/2 or HTTP/3 for browsers. The HTTP/1.1 demo checks the response order. To test preloading in a browser, use an HTTP/2 or HTTP/3 endpoint that sends or forwards the 103, then check when the stylesheet request starts.

I list only what the page uses on every load, here the stylesheet and one font. A hint for a file the HTML never references is a download for nothing.