LEARNING OBJECTIVES ⌵
- Diagnose and fix the catastrophic "Double Download" bug caused by mismatched
asattributes or missingcrossorigin. - Understand font preloading CORS requirements (
<link rel="preload" as="font" crossorigin>). - Avoid over-preloading and network bandwidth starvation on mobile 3G/4G connections.
- Configure Chrome DevTools Network panel to identify wasted preloads.
📖 The Mental Model & Story
Imagine reserving a table at a restaurant for an important business dinner, but you put the reservation under your nickname instead of your legal name. When you arrive, the host doesn't recognize you, marks the first reservation as unused, and gives you a brand-new table—charging you twice!
This is exactly what happens with <link rel="preload"> when attributes don't match the actual consumer. If you preload a font without crossorigin="anonymous", or preload a script with as="fetch" instead of as="script", the browser downloads the entire file twice, throwing a warning in the console: "The resource was preloaded using link preload but not used within a few seconds."
Preload Tag: <link rel="preload" href="/font.woff2" as="font"> (NO crossorigin)
===> Browser fetches font with CORS mode: "no-cors"
Consumer CSS: @font-face { src: url('/font.woff2'); }
===> Browser spec REQUIRES fonts to use CORS mode: "cors"
Result: Browser cannot reuse the preloaded buffer ===> FETCHES FONT A SECOND TIME! ❌
Technical Deep Dive & Specifications
The Correct Font Preload Rule
Under the W3C Web Fonts specification, all font fetches must be anonymous CORS requests, even if loaded from the same origin:
<!-- Correct: MUST include crossorigin -->
<link rel="preload" href="/fonts/inter-bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<!-- Wrong: Will trigger a double download -->
<link rel="preload" href="/fonts/inter-bold.woff2" as="font" type="font/woff2">
📌 Key Takeaways
- Always include
crossoriginon<link rel="preload" as="font">, even for same-origin fonts. - Ensure the
hrefandasattributes in<link rel="preload">match the consuming tag exactly. - Limit preloading to top 2–3 critical above-the-fold assets (Hero LCP image, primary font, critical CSS).
- --
❓ Knowledge Check
1. Which of the following is correct?
2. Which of the following is correct?
🏋️ Practice Exercise
Challenge: Modify the code example above to experiment with the concepts covered in this lesson. Try changing values, adding new elements, or combining techniques.