Font optimization helps your website load faster and look better by using only the fonts you need.
Font optimization in Astro
import { font } from 'astro:font'; const myFont = font({ src: './fonts/MyFont.woff2', preload: true, display: 'swap', subsets: ['latin'], weight: '400', style: 'normal' });
preload: Loads the font early for faster display.
display: Controls how text is shown while font loads (e.g., 'swap' shows fallback text first).
import { font } from 'astro:font'; const roboto = font({ src: './fonts/Roboto-Regular.woff2', preload: true, display: 'swap' });
import { font } from 'astro:font'; const openSans = font({ src: './fonts/OpenSans-Bold.woff2', weight: '700', style: 'italic', subsets: ['latin', 'cyrillic'] });
This Astro component imports and optimizes the Lato font. It preloads the font and uses the 'swap' display to show fallback text while loading. The font style is injected into the head, and the body uses the Lato font family.
--- import { font } from 'astro:font'; const lato = font({ src: './fonts/Lato-Regular.woff2', preload: true, display: 'swap', subsets: ['latin'], weight: '400', style: 'normal' }); --- <html lang="en"> <head> <title>Font Optimization Example</title> <link rel="preload" href={lato.url} as="font" type="font/woff2" crossorigin /> {lato.style} </head> <body style="font-family: 'Lato', sans-serif;"> <h1>Hello, this text uses the optimized Lato font!</h1> <p>Font optimization helps this page load faster and look nice.</p> </body> </html>
Always preload fonts you use above the fold to avoid invisible text.
Use font-display: swap to show fallback fonts while your custom font loads.
Limit font subsets and weights to only what you need to reduce file size.
Font optimization makes your website faster and smoother.
Astro's font helper lets you easily load and control fonts.
Preload fonts and use display: 'swap' for best user experience.
Practice
Solution
Step 1: Understand font optimization purpose
Font optimization aims to improve website speed and smoothness by managing font loading efficiently.Step 2: Identify the correct benefit
Among the options, only making the website load faster and improving user experience matches the purpose.Final Answer:
It makes the website load faster and improves user experience. -> Option AQuick Check:
Font optimization = faster load and better UX [OK]
- Thinking font optimization changes font colors
- Believing it removes fonts entirely
- Assuming it increases font file size
Font helper to preload a font with display swap?Solution
Step 1: Check correct import and usage
Astro's Font helper is imported from '@astrojs/font' and called with an object including src, display, and preload keys.Step 2: Verify options for preload and display
import { Font } from '@astrojs/font'; const myFont = Font({ src: '/fonts/myfont.woff2', display: 'swap', preload: true });correctly uses preload: true and display: 'swap' which matches best practices.Final Answer:
import { Font } from '@astrojs/font'; const myFont = Font({ src: '/fonts/myfont.woff2', display: 'swap', preload: true }); -> Option BQuick Check:
Correct import and preload with display swap =import { Font } from '@astrojs/font'; const myFont = Font({ src: '/fonts/myfont.woff2', display: 'swap', preload: true });[OK]
- Wrong import path or syntax
- Using display: 'block' instead of 'swap'
- Not setting preload to true
import { Font } from '@astrojs/font';
const roboto = Font({ src: '/fonts/roboto.woff2', display: 'swap', preload: true });
export default function Page() {
return <h1 style={{ fontFamily: roboto.style }}>Hello</h1>;
}Solution
Step 1: Analyze the Font helper usage
The font is imported with preload: true and display: 'swap', which preloads the font and allows fallback text until font loads.Step 2: Understand the effect on rendering
Using display: 'swap' avoids invisible text and preload speeds up font availability, improving page speed and user experience.Final Answer:
Roboto font is preloaded and swaps smoothly when loaded, improving page speed. -> Option DQuick Check:
Preload + display swap = smooth font load [OK]
- Assuming no preload causes no flash
- Thinking syntax error exists here
- Ignoring display: 'swap' effect
import { Font } from '@astrojs/font';
const openSans = Font({ src: '/fonts/opensans.woff2', display: 'fallback', preload: 'yes' });Solution
Step 1: Check preload option type
Preload expects a boolean (true or false), but 'yes' is a string, causing an error.Step 2: Validate other options
Display: 'fallback' is valid CSS font-display value, .woff2 is a valid font format, and Font helper accepts an object.Final Answer:
The preload option should be a boolean, not a string. -> Option AQuick Check:
Preload must be true/false, not 'yes' [OK]
- Using string 'yes' instead of boolean true
- Thinking .woff2 is invalid
- Assuming display: 'fallback' is wrong
Solution
Step 1: Check heading font requirements
Heading font must preload and use display: 'swap'.import { Font } from '@astrojs/font'; const headingFont = Font({ src: '/fonts/heading.woff2', display: 'swap', preload: true }); const bodyFont = Font({ src: '/fonts/body.woff2', display: 'swap', preload: false });sets preload: true and display: 'swap' correctly.Step 2: Check body font requirements
Body font should load lazily without preload, so preload: false and display: 'swap' is correct inimport { Font } from '@astrojs/font'; const headingFont = Font({ src: '/fonts/heading.woff2', display: 'swap', preload: true }); const bodyFont = Font({ src: '/fonts/body.woff2', display: 'swap', preload: false });.Step 3: Verify other options
Other options either preload body font incorrectly, use wrong display values, or wrong import syntax.Final Answer:
import { Font } from '@astrojs/font'; const headingFont = Font({ src: '/fonts/heading.woff2', display: 'swap', preload: true }); const bodyFont = Font({ src: '/fonts/body.woff2', display: 'swap', preload: false });-> Option CQuick Check:
Preload headings true, body false with display swap =import { Font } from '@astrojs/font'; const headingFont = Font({ src: '/fonts/heading.woff2', display: 'swap', preload: true }); const bodyFont = Font({ src: '/fonts/body.woff2', display: 'swap', preload: false });[OK]
- Preloading body font when it should be lazy
- Using display: 'block' instead of 'swap'
- Incorrect import or function call syntax
Ready to prove it?
Pass the test and this pattern moves unknown
