Skip to content
Merged
32 changes: 31 additions & 1 deletion src/content/docs/en/reference/experimental-flags/fonts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ import { Font } from 'astro:assets';

<p>

**Type:** `boolean`<br />
**Type:** `boolean | { weight?: string | number; style?: string; subset?: string }[]`<br />
**Default:** `false`
</p>

Expand All @@ -325,6 +325,36 @@ import { Font } from 'astro:assets';
<Font cssVariable="--font-roboto" preload />
```

With the `preload` directive, the browser will immediately begin downloading all possible font links during page load.

#### Granular preloads

<p>
<Since v="5.15.0" />
</p>

You may not always want to preload every font link, as this can block loading other important resources or may download fonts that are not needed for the current page.

To selectively control which font files are preloaded, you can provide an array of objects describing any combination of font `weight`, `style`, or `subset` to preload.

The following example will only preload font files with a `400` weight or a `normal` style in the `latin` subset:

```astro title="src/components/Head.astro" {7-10}
---
import { Font } from 'astro:assets';
---

<Font
cssVariable="--font-roboto"
preload={[
{ subset: 'latin', style: 'normal' },
{ weight: '400' },
]}
/>
```

Variable weight font files will be preloaded if any weight within its range is requested. For example, a font file for font weight `100 900` will be included when `400` is specified in a `preload` object.

## Accessing font data programmatically

The `getFontData()` function is intended for retrieving lower-level font family data programmatically, for example, in an [API Route](/en/guides/endpoints/#server-endpoints-api-routes) or to generate your own meta tags.
Expand Down