← Back to HTML & CSS, page 8

html-css-0148 · Medium mid

What do preconnect, dns-prefetch, preload and prefetch do, and when can they hurt?

dns-prefetch resolves a host early, preconnect opens the full connection, preload fetches a file needed by the current page at high priority and needs as, and prefetch fetches a likely next resource at low priority. Too many hints compete with needed files, so use few and measure.

Full answer

Resource hints are link elements in the head that tell the browser about work it can do early.

  • dns-prefetch resolves the host name of another site early. It is cheap and widely supported, and it is a fallback for preconnect.
  • preconnect opens the whole connection early: the name lookup, the network connection and the security handshake. Use it for a few important other-origin hosts that you will definitely use soon, such as a font host or an image CDN. Add crossorigin when the resource is loaded with CORS, as fonts are.
  • preload tells the browser to fetch a specific file now, for the current page, with a high priority. Use it for something the browser would find late, such as a font used in CSS, a hero image used as a CSS background, or a script that is loaded by another script. It needs as (for example as="font", as="image") so the browser sets the right priority and headers.
  • prefetch fetches a resource at low priority for a likely next page, such as the script for the next step in a flow. It is used only when the browser is idle.
  • modulepreload is the same idea for JavaScript modules.

How they can hurt:

  • Too many hints compete for bandwidth with the files the page needs now. Preload is strong. A preloaded file that the page does not use within a short time causes a console warning and wastes data.
  • Preconnect to many hosts opens many connections, and unused connections are closed again.
  • A preload without the right as or crossorigin can fetch the file twice.
  • Prefetching on slow or limited connections wastes the user's data.

Rule: hint only for what you have measured to be late, and keep the list short. Test with the network panel.

The page content is unchanged, but the browser starts the connection to the font host and the font download early. The next step's stylesheet is fetched later at low priority.

Browser tools show the priority and the time for each request, which is the right place to confirm a gain.

html
<link rel="dns-prefetch" href="https://cdn.example.com">
<link rel="preconnect" href="https://fonts.example.com" crossorigin id="pc">
<link rel="preload" href="/fonts/brand.woff2" as="font" type="font/woff2" crossorigin id="pl">
<link rel="prefetch" href="/css/next-step.css" id="pf">
<p id="p">Page content</p>

The check read the link elements and their attributes. Network timing and priorities were not measured. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • dns-prefetch resolves names; preconnect opens the connection.
  • preload is for current-page files found late and needs as.
  • prefetch is low priority for a likely next page.
  • Fonts need crossorigin on preload.
  • Too many hints hurt; measure.

Common mistakes

  • Preloading many files, so the important ones are slowed down.
  • Preloading a font without crossorigin, which causes a second download.
  • Preconnecting to hosts that the page does not use early.

Follow-ups

  • What does the as attribute do on preload?
  • Why is dns-prefetch used with preconnect?

Tips

  • Give a one-line meaning for each of the four hints.
  • Say that hints must be measured and kept few.