4. Identify the error in this Astro font optimization code:
import { Font } from '@astrojs/font';
const openSans = Font({ src: '/fonts/opensans.woff2', display: 'fallback', preload: 'yes' });
medium
A. The preload option should be a boolean, not a string.
B. The display option 'fallback' is invalid in Astro.
C. The src path must end with .ttf, not .woff2.
D. Font helper must be called with a string, not an object.
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 A
Quick Check:
Preload must be true/false, not 'yes' [OK]
Hint: Preload must be true or false, never a string [OK]
Common Mistakes:
Using string 'yes' instead of boolean true
Thinking .woff2 is invalid
Assuming display: 'fallback' is wrong
5. You want to optimize loading two fonts in Astro: one for headings and one for body text. You want headings to preload and use display swap, but body text font should load lazily without preload. Which code correctly implements this?
hard
A.
import Font from '@astrojs/font';
const headingFont = Font('/fonts/heading.woff2', { preload: true });
const bodyFont = Font('/fonts/body.woff2', { preload: false });