Caching means saving a copy of the API response so the app can use it again without asking the server. This makes the app faster and reduces server work.
Click to reveal answer
beginner
Why should you cache API responses in Astro projects?
Caching API responses in Astro helps pages load faster and lowers the number of requests to the server, improving user experience and saving resources.
Click to reveal answer
intermediate
Which Astro feature helps with caching API responses during build time?
Astro's server-side rendering and static site generation let you fetch and cache API data at build time, so pages serve pre-fetched data quickly.
Click to reveal answer
intermediate
What is stale-while-revalidate in caching?
Stale-while-revalidate means showing cached data immediately while fetching fresh data in the background to update the cache for next time.
Click to reveal answer
intermediate
How can you control cache duration for API responses in Astro?
You can set cache headers or use Astro's fetch options to specify how long the cached API response stays valid before fetching new data.
Click to reveal answer
What is the main benefit of caching API responses in Astro?
AMore API calls to the server
BFaster page loads and fewer server requests
CSlower page rendering
DIncreased server load
✗ Incorrect
Caching saves API data so pages load faster and the server handles fewer requests.
When does Astro typically fetch and cache API data for static pages?
AAt build time
BEvery time the user clicks a button
COnly on the client side
DNever
✗ Incorrect
Astro fetches API data during build time to generate static pages with cached data.
What does stale-while-revalidate caching do?
AAlways fetches fresh data before showing anything
BDeletes cache after every request
CNever updates cached data
DShows cached data immediately and updates it in the background
✗ Incorrect
It serves cached data fast and refreshes the cache quietly for next use.
How can you set how long an API response stays cached in Astro?
ABy editing CSS files
BBy changing the HTML title
CBy setting cache-control headers or fetch options
DBy disabling JavaScript
✗ Incorrect
Cache duration is controlled with cache headers or fetch settings.
Which of these is NOT a benefit of caching API responses?
AIncreased network traffic
BFaster user experience
CLower latency
DReduced server load
✗ Incorrect
Caching reduces network traffic, it does not increase it.
Explain how caching API responses improves performance in Astro projects.
Think about how saving data ahead helps avoid waiting for the server.
You got /4 concepts.
Describe the stale-while-revalidate caching strategy and why it is useful.
It's like showing an old photo quickly while taking a new one quietly.
You got /4 concepts.
Practice
(1/5)
1. What is the main benefit of caching API responses in Astro?
easy
A. It speeds up the site by reusing previously fetched data
B. It increases the size of the API response
C. It makes the API slower to respond
D. It disables the API from updating
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 A
Quick Check:
Caching = Speed up site [OK]
Hint: Caching saves time by reusing data, not slowing down [OK]
Common Mistakes:
Thinking caching makes API slower
Confusing cache with data size increase
Believing cache disables updates
2. Which syntax correctly sets cache revalidation time in Astro?
easy
A. const data = await fetch('https://api.example.com/data', { refresh: 60 });
B. const data = await fetch('https://api.example.com/data', { cacheTime: 60 });
C. const data = await fetch('https://api.example.com/data', { revalidate: 60 });
D. const data = await fetch('https://api.example.com/data', { updateAfter: 60 });
Solution
Step 1: Recall correct property name
The correct property to control cache duration is revalidate.
Step 2: Match syntax with Astro docs
The syntax uses { revalidate: 60 } inside fetch().
Final Answer:
const data = await fetch('https://api.example.com/data', { revalidate: 60 }); -> Option C
Quick Check:
Use revalidate property = const data = await fetch('https://api.example.com/data', { revalidate: 60 }); [OK]
Hint: Remember 'revalidate' sets cache time in seconds [OK]
Common Mistakes:
Using wrong property names like cacheTime or refresh
What happens when a user visits the page after 3 minutes?
medium
A. The cached data is used without fetching again
B. The cache is cleared but data is not fetched
C. The page shows an error due to cache expiry
D. The data is fetched again from the API
Solution
Step 1: Understand revalidate timing
The revalidate: 120 means 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 D
Quick Check:
Cache expired after 120s, fetch again [OK]
Hint: If visit time > revalidate, data refetches [OK]
Common Mistakes:
Assuming cache lasts forever
Thinking cache clears but no fetch happens
Confusing cache expiry with errors
4. Identify the error in this Astro caching code:
const data = await fetch('https://api.example.com/data').json();
medium
A. Missing await before .json() call
B. Incorrect property name 'revalidate'
C. fetch URL is invalid
D. Return statement missing props object
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 A
Quick Check:
Await fetch before .json() [OK]
Hint: Always await fetch before calling .json() [OK]
Common Mistakes:
Calling .json() directly on fetch without await
Misnaming revalidate property
Assuming fetch URL is wrong without checking
5. You want to cache API data in Astro but update it every hour. Which is the best way to set revalidate and why?
Options: A) revalidate: 3600 to update every hour B) revalidate: 60 to update every minute C) revalidate: 0 to never cache D) revalidate: -1 to disable caching
hard
A. revalidate: 60 to update every minute
B. revalidate: 3600 to update every hour
C. revalidate: 0 to never cache
D. revalidate: -1 to disable caching
Solution
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 B
Quick Check:
1 hour = 3600 seconds revalidate [OK]
Hint: Set revalidate in seconds matching update frequency [OK]
Common Mistakes:
Using too short revalidate causing frequent fetches
Setting zero disables caching, not hourly update
Negative values are invalid
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.