📖 Scenario: You are building a simple Astro website with multiple pages. To improve user experience, you want to make sure that when users hover over navigation links, the linked pages start loading in the background (prefetching). Also, you want to preload a key image on the homepage to make it appear faster.
🎯 Goal: Build an Astro component with navigation links that use prefetch to load pages early on hover, and preload a hero image on the homepage for faster display.
📋 What You'll Learn
Create a navigation list with links to /about and /contact pages
Add prefetch attribute to these links to enable prefetching on hover
Add a <link rel="preload" as="image" href="/hero.jpg" /> tag in the <head> to preload the hero image
Use Astro syntax and components correctly
💡 Why This Matters
🌍 Real World
Prefetching and preloading help websites feel faster by loading resources before users need them, improving user experience.
💼 Career
Web developers use these techniques to optimize site performance and reduce wait times, which is important for user retention and SEO.
Progress0 / 4 steps
1
Create navigation links
Create an Astro component with a <nav> element containing two links: one to /about and one to /contact. Use <a> tags with href attributes exactly "/about" and "/contact".
Astro
Hint
Use <a href="/about">About</a> and <a href="/contact">Contact</a> inside a <nav>.
2
Add prefetch attribute to links
Add the prefetch attribute to both <a> tags linking to /about and /contact to enable prefetching when users hover over these links.
Astro
Hint
Add prefetch inside the opening <a> tags.
3
Preload hero image in head
Add a <link> tag inside the <head> of your Astro page to preload the hero image located at /hero.jpg. Use rel="preload" and as="image" attributes exactly as shown.
Astro
Hint
Place <link rel="preload" as="image" href="/hero.jpg" /> inside the <head> section.
4
Add hero image to body
Add an <img> tag inside the <body> below the navigation. Set its src attribute to "/hero.jpg" and alt attribute to "Hero image".
Astro
Hint
Use <img src="/hero.jpg" alt="Hero image" /> inside the <body> after the navigation.
Practice
(1/5)
1. What is the main purpose of prefetching in Astro?
easy
A. To remove unused resources from the page
B. To load resources immediately for current use
C. To load resources in the background before they are needed
D. To delay loading resources until user interaction
Solution
Step 1: Understand prefetching concept
Prefetching means loading resources quietly in the background before the user needs them.
Step 2: Compare with other options
Preloading loads immediately, delaying is opposite, and removing resources is unrelated.
Final Answer:
To load resources in the background before they are needed -> Option C
Quick Check:
Prefetching = background loading [OK]
Hint: Prefetching = load early but quietly in background [OK]
Common Mistakes:
Confusing prefetching with preloading
Thinking prefetching delays loading
Assuming prefetching removes resources
2. Which is the correct way to add a prefetch link in Astro's <head>?
easy
A.
B.
C.
D.
Solution
Step 1: Identify correct rel attribute for prefetch
Prefetch uses rel="prefetch" to hint browser to load resource in background.
Step 2: Check other options
Preload uses rel="preload", but question asks for prefetch. rel="fetch" is invalid.
Final Answer:
<link rel="prefetch" href="/script.js"> -> Option D
Quick Check:
Prefetch link uses rel="prefetch" [OK]
Hint: Prefetch link uses rel="prefetch" exactly [OK]
Common Mistakes:
Using preload instead of prefetch
Missing the rel attribute
Using invalid rel values like fetch
3. Given this Astro component snippet, what will happen?