Prefetching and preloading help your website load faster by getting important files ready before the user needs them.
Prefetching and preloading in Astro
<link rel="prefetch" href="/path/to/resource" /> <link rel="preload" href="/path/to/resource" as="script" />
Prefetch tells the browser to fetch a resource when it is idle, for future use.
Preload tells the browser to fetch a resource immediately because it will be needed soon.
<link rel="prefetch" href="/about/page-data.json" />
<link rel="preload" href="/scripts/main.js" as="script" />
<link rel="preload" href="/images/banner.jpg" as="image" />
This Astro page prefetches blog post data for faster navigation and preloads the blog script to run quickly.
--- --- <html lang="en"> <head> <title>Prefetch and Preload Example</title> <link rel="prefetch" href="/blog/post-data.json" /> <link rel="preload" href="/scripts/blog.js" as="script" /> </head> <body> <h1>Welcome to the Blog</h1> <p>Content loads faster because resources are fetched early.</p> <script src="/scripts/blog.js" defer></script> </body> </html>
Use prefetch for resources you want to load quietly in the background.
Use preload for resources you need right away to avoid delays.
Be careful not to preload too many resources, or it can slow down the initial page load.
Prefetching loads resources in the background for future use.
Preloading loads resources immediately for quick use.
Both improve website speed and user experience when used wisely.
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
