Caching API responses helps your website load faster by saving data from previous requests. This means users don't have to wait for the same information to be fetched again.
Caching API responses in Astro
export const revalidate = 3600; export default async function Page() { const response = await fetch('https://api.example.com/data'); const data = await response.json(); return ( <div>{/* use {data} here */}</div> ); }
revalidate tells Astro how many seconds to keep the cached data before fetching fresh data.
This example uses an async page component to fetch data at prerender time and cache it.
export const revalidate = 600; // cache for 10 minutes export default async function Page() { const response = await fetch('https://api.example.com/users'); const users = await response.json(); return ( <div>{/* use {users} here */}</div> ); }
export const revalidate = 86400; // cache for 24 hours export default async function Page() { const response = await fetch('https://api.example.com/posts'); const posts = await response.json(); return ( <div>{/* use {posts} here */}</div> ); }
export const revalidate = 300; // cache for 5 minutes export default async function Page() { const response = await fetch('https://api.example.com/weather'); const weather = await response.json(); return ( <div>{/* use {weather} here */}</div> ); }
This Astro component fetches product data from an API and caches it for 30 minutes. When users visit the page, they see the product list quickly without waiting for a fresh API call every time.
export const revalidate = 1800; // cache for 30 minutes export default async function ProductsPage() { const response = await fetch('https://api.example.com/products'); const products = await response.json(); return ( <main> <h1>Product List</h1> <ul> {products.map(product => ( <li key={product.id}>{product.name} - ${product.price}</li> ))} </ul> </main> ); }
Use caching to balance fresh data and fast loading times.
Set revalidate time based on how often your data changes.
Cached data is stored on the server, so users get faster responses.
Caching API responses speeds up your site by saving data from previous requests.
Use export const revalidate in your page component to control cache duration.
Choose cache time based on how often your data updates.
Practice
Solution
Step 1: Understand caching purpose
Caching stores data from previous API calls to avoid fetching again.Step 2: Identify benefit in Astro context
Reusing cached data reduces load time and speeds up the site.Final Answer:
It speeds up the site by reusing previously fetched data -> Option AQuick Check:
Caching = Speed up site [OK]
- Thinking caching makes API slower
- Confusing cache with data size increase
- Believing cache disables updates
Solution
Step 1: Recall correct property name
The correct property to control cache duration isrevalidate.Step 2: Match syntax with Astro docs
The syntax uses{ revalidate: 60 }insidefetch().Final Answer:
const data = await fetch('https://api.example.com/data', { revalidate: 60 }); -> Option CQuick Check:
Use revalidate property = const data = await fetch('https://api.example.com/data', { revalidate: 60 }); [OK]
- Using wrong property names like cacheTime or refresh
- Forgetting to return an object
- Placing revalidate outside return
const data = await fetch('https://api.example.com/data', { revalidate: 120 }).then(res => res.json());What happens when a user visits the page after 3 minutes?
Solution
Step 1: Understand revalidate timing
Therevalidate: 120means cache lasts 120 seconds (2 minutes).Step 2: Compare visit time with cache duration
After 3 minutes (180 seconds), cache expired, so Astro fetches fresh data.Final Answer:
The data is fetched again from the API -> Option DQuick Check:
Cache expired after 120s, fetch again [OK]
- Assuming cache lasts forever
- Thinking cache clears but no fetch happens
- Confusing cache expiry with errors
const data = await fetch('https://api.example.com/data').json();Solution
Step 1: Check fetch usage
fetch returns a Promise; calling .json() needs to await the fetch first.Step 2: Identify missing await
Code calls fetch(...).json() without awaiting fetch, causing error.Final Answer:
Missing await before .json() call -> Option AQuick Check:
Await fetch before .json() [OK]
- Calling .json() directly on fetch without await
- Misnaming revalidate property
- Assuming fetch URL is wrong without checking
revalidate and why?Options:
A)
revalidate: 3600 to update every hourB)
revalidate: 60 to update every minuteC)
revalidate: 0 to never cacheD)
revalidate: -1 to disable cachingSolution
Step 1: Understand revalidate meaning
revalidate sets cache duration in seconds before refreshing data.Step 2: Match desired update frequency
To update every hour, set revalidate to 3600 seconds (60*60).Step 3: Evaluate other options
60 seconds updates too often, 0 disables caching, -1 is invalid.Final Answer:
revalidate: 3600 to update every hour -> Option BQuick Check:
1 hour = 3600 seconds revalidate [OK]
- Using too short revalidate causing frequent fetches
- Setting zero disables caching, not hourly update
- Negative values are invalid
Ready to prove it?
Pass the test and this pattern moves unknown
