Discover how tiny tricks like prefetching can make websites feel lightning fast!
Why Prefetching and preloading in Astro? - Purpose & Use Cases
Imagine visiting a website where every time you click a link, you wait several seconds for the new page to load completely before seeing anything.
This wait breaks your flow and makes browsing frustrating.
Manually loading pages only when clicked causes delays and a choppy experience.
Users feel the lag because the browser fetches resources too late, making the site feel slow and unresponsive.
Prefetching and preloading let the browser fetch important resources ahead of time, so pages appear instantly when you navigate.
This creates a smooth, fast experience without waiting.
<!-- No prefetch -->
<a href="/about">About</a><a href="/about" rel="prefetch">About</a>
It enables websites to feel instant and seamless, keeping users happy and engaged.
When shopping online, prefetching product pages means you can jump from item to item without waiting for each page to load.
Manual loading causes slow, frustrating waits.
Prefetching and preloading fetch resources early.
This makes navigation feel instant and smooth.
Practice
prefetching in Astro?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 CQuick Check:
Prefetching = background loading [OK]
- Confusing prefetching with preloading
- Thinking prefetching delays loading
- Assuming prefetching removes resources
<head>?Solution
Step 1: Identify correct rel attribute for prefetch
Prefetch usesrel="prefetch"to hint browser to load resource in background.Step 2: Check other options
Preload usesrel="preload", but question asks for prefetch.rel="fetch"is invalid.Final Answer:
<link rel="prefetch" href="/script.js"> -> Option DQuick Check:
Prefetch link uses rel="prefetch" [OK]
- Using preload instead of prefetch
- Missing the rel attribute
- Using invalid rel values like fetch
<head> <link rel="preload" as="style" href="/styles.css" /> </head> <body> <h1>Hello</h1> </body>
Solution
Step 1: Understand preload with as="style"
Preload withas="style"tells browser to load CSS immediately for fast styling.Step 2: Compare with prefetch behavior
Prefetch loads in background, but preload loads right away to speed up rendering.Final Answer:
The styles.css file loads immediately to style the page quickly -> Option AQuick Check:
Preload with as="style" = immediate CSS load [OK]
- Confusing preload with prefetch
- Thinking preload delays loading
- Ignoring the as attribute importance
<head> <link rel="prefetch" as="script" href="/app.js" /> </head>
Solution
Step 1: Check prefetch attribute usage
Prefetch links should not include theasattribute; it's only for preload.Step 2: Validate other attributes and placement
The href is present, rel="prefetch" is valid, and link tags belong in head, not body.Final Answer:
Prefetch should not use the 'as' attribute -> Option AQuick Check:
Prefetch without 'as' attribute [OK]
- Adding 'as' attribute to prefetch
- Placing link tags inside body
- Confusing prefetch with preload
Solution
Step 1: Understand prefetch for next page resources
Prefetch loads resources in background for future use, ideal for next page images.Step 2: Combine with preload on next page
Preloading on next page ensures immediate display when user arrives, improving UX.Step 3: Check invalid options
Preload on current page wastes bandwidth if image not shown now; 'as' attribute invalid with prefetch; delaying load hurts UX.Final Answer:
Use <link rel="prefetch" href="/large.jpg" /> on current page and preload on next page -> Option BQuick Check:
Prefetch now + preload later = best for next page image [OK]
- Preloading large images too early wasting bandwidth
- Adding 'as' attribute to prefetch links
- Not prefetching resources needed soon
Ready to prove it?
Pass the test and this pattern moves unknown
