The short answer

#

Add a link with rel="preload" to the head, set as to the type of resource, and for fonts always add crossorigin, even when the font is on your own domain. If the attributes do not match how the file is used later, the browser downloads it twice.


_10
<link
_10
rel="preload"
_10
href="/fonts/inter-var.woff2"
_10
as="font"
_10
type="font/woff2"
_10
crossorigin
_10
/>

Why it happens

#

The browser finds most resources while parsing HTML, but some are discovered late. A web font is only requested once the browser has downloaded the CSS, parsed it and found an element that uses the font. A background image in CSS, or a hero image inserted by JavaScript, has the same problem. That delay shows up as a flash of fallback text or a slow Largest Contentful Paint.

rel="preload" tells the browser to fetch a specific file straight away, with the right priority, without running it or applying it. When the real request comes later, the browser reuses the preloaded copy. For that reuse to work, the preload has to be the same kind of request:

  • as sets the request type and priority. Without it, the browser fetches with low priority and cannot match the later request.
  • crossorigin sets the CORS mode. Fonts are always fetched in CORS mode, so a font preload without crossorigin does not match and the font is downloaded again.
  • type lets browsers skip formats they do not support.

How to fix it

#

Preload a critical font

Preload only the one or two font files used above the fold, usually the main body weight. Use woff2.


_10
<link rel="preload" href="/fonts/body.woff2" as="font" type="font/woff2" crossorigin />

The URL must match the src in your @font-face rule exactly, including any query string.


_10
@font-face {
_10
font-family: 'Body';
_10
src: url('/fonts/body.woff2') format('woff2');
_10
font-display: swap;
_10
}

Preload a late-discovered image

For an image set in CSS or added by JavaScript, preload it with as="image". For responsive images, use imagesrcset and imagesizes so the browser picks the same file it would pick for the real image.


_10
<link
_10
rel="preload"
_10
as="image"
_10
href="/img/hero-800.jpg"
_10
imagesrcset="/img/hero-800.jpg 800w, /img/hero-1600.jpg 1600w"
_10
imagesizes="100vw"
_10
/>

Use fetchpriority for images already in the HTML

If the hero image is a normal img tag in the HTML, the browser already finds it early. Preloading it adds little. Raise its priority instead, and make sure it is not lazy-loaded.


_10
<img src="/img/hero.jpg" alt="" width="1600" height="900" fetchpriority="high" />

Send it as a header

You can also send a preload in a Link HTTP header, which the browser sees before it parses any HTML. The same rules for as and crossorigin apply.


_10
Link: </fonts/body.woff2>; rel=preload; as=font; type="font/woff2"; crossorigin

When not to preload

#
  • Files the browser already discovers early, such as stylesheets in the head or images in the HTML.
  • Every font weight and style. Each preload competes for bandwidth with the CSS and the main image, so preloading too much slows the page down.
  • Files that are not used on the current page. Chrome logs a console warning when a preloaded file is not used within a few seconds of the load event.
  • Resources for the next page. Use rel="prefetch" for those, which fetches at low priority.
  • JavaScript modules. Use rel="modulepreload", which also prepares the module for execution.

Common pitfalls

#
  • Leaving out crossorigin on font preloads. The font downloads twice; check the Network panel for duplicate requests.
  • Leaving out as, or using the wrong value. The request is not reused.
  • A different URL in the preload and the CSS, such as a missing version hash. The browser treats them as two files.
  • Preloading a lazy-loaded image. The two hints work against each other.
#
file under:
HTML

quick question

Was this resource helpful?