Caching API responses
📖 Scenario: You are building a simple Astro component that fetches user data from an API. To improve performance and reduce repeated network requests, you want to cache the API response for a short time.
🎯 Goal: Create an Astro component that fetches user data from a public API and caches the response for 10 seconds before fetching fresh data again.
📋 What You'll Learn
Create a variable to hold the API URL
Create a cache object to store the response and timestamp
Write a function to fetch data and cache it if expired
Use the cached data in the component to render user names
💡 Why This Matters
🌍 Real World
Caching API responses helps reduce network calls and speeds up web apps by reusing recent data.
💼 Career
Understanding caching is important for frontend developers to optimize performance and user experience.
Progress0 / 4 steps