0
0
Astroframework~20 mins

Caching API responses in Astro - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Astro Caching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Astro component with caching?

Consider this Astro component that fetches data and caches it for 10 seconds. What will it render if the API returns { message: 'Hello' }?

Astro
---
import { cache } from 'astro/cache';
const fetchData = async () => {
  return await cache(async () => {
    const res = await fetch('https://api.example.com/greet');
    return res.json();
  }, 10000);
};
const data = await fetchData();
---
<p>{data.message}</p>
A<p>Hello</p>
B<p>undefined</p>
C<p>{message}</p>
D<p>Error: cache is not a function</p>
Attempts:
2 left
💡 Hint

Think about what the cache function returns after fetching.

📝 Syntax
intermediate
2:00remaining
Which option correctly caches API data for 5 minutes in Astro?

Choose the correct code snippet that caches the API response for 300000 milliseconds (5 minutes) using Astro's cache function.

Aconst data = await cache(async () => await fetch('https://api.example.com/data').then(r => r.json()), 300000);
Bconst data = await cache(async () => fetch('https://api.example.com/data').json(), 300000);
Cconst data = await cache(() => fetch('https://api.example.com/data').json(), 300000);
Dconst data = cache(async () => await fetch('https://api.example.com/data').json(), 300000);
Attempts:
2 left
💡 Hint

Remember to await the fetch and parse JSON properly inside an async function.

🔧 Debug
advanced
2:00remaining
Why does this Astro cache code throw an error?

This code throws TypeError: cache is not a function. What is the cause?

---
import cache from 'astro/cache';
const data = await cache(async () => {
  const res = await fetch('https://api.example.com/info');
  return res.json();
}, 60000);
---
AThe cache function requires a third argument for options
BThe import should use curly braces: <code>import { cache } from 'astro/cache';</code>
CThe fetch URL is invalid causing the error
DThe cache function must be called without await
Attempts:
2 left
💡 Hint

Check how cache is imported from the module.

state_output
advanced
2:00remaining
What is the value of data after running this Astro caching code?

Given this code snippet, what is the value of data if the API returns { count: 42 }?

---
import { cache } from 'astro/cache';
const data = await cache(async () => {
  const response = await fetch('https://api.example.com/count');
  const json = await response.json();
  return json.count * 2;
}, 15000);
---
A42
B{ count: 42 }
Cundefined
D84
Attempts:
2 left
💡 Hint

Look at what the async function returns inside the cache.

🧠 Conceptual
expert
3:00remaining
Which statement about Astro's cache function is TRUE?

Choose the correct statement about how Astro's cache function works when caching API responses.

AIt caches only the fetch request but not the parsed JSON data.
BIt caches the API response permanently until the server restarts.
CIt caches the result of the async function and reuses it until the timeout expires, then refetches on next call.
DIt caches data only on the client side, not during server rendering.
Attempts:
2 left
💡 Hint

Think about how caching with a timeout usually works in server frameworks.