0
0
Astroframework~20 mins

Fetching APIs in frontmatter in Astro - Practice Problems & Coding Challenges

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

Consider this Astro component that fetches a list of users from an API in the frontmatter and renders their names:

---
const response = await fetch('https://jsonplaceholder.typicode.com/users');
const users = await response.json();
---
    {users.map(user =>
  • {user.name}
  • )}

What will this component render?

Astro
---
const response = await fetch('https://jsonplaceholder.typicode.com/users');
const users = await response.json();
---
<ul>
  {users.map(user => <li>{user.name}</li>)}
</ul>
A<ul><li>undefined</li></ul>
BSyntaxError: Unexpected token in JSX
C<ul><li>Leanne Graham</li><li>Ervin Howell</li><li>Clementine Bauch</li><li>Patricia Lebsack</li><li>Chelsey Dietrich</li><li>Mrs. Dennis Schulist</li><li>Kurtis Weissnat</li><li>Nicholas Runolfsdottir V</li><li>Glenna Reichert</li><li>Clementina DuBuque</li></ul>
D<ul></ul>
Attempts:
2 left
💡 Hint

Think about what the API returns and how the map function transforms it.

📝 Syntax
intermediate
2:00remaining
Which option correctly fetches API data in Astro frontmatter with error handling?

Choose the correct Astro frontmatter code snippet that fetches data from an API and handles fetch errors gracefully.

A
---
try {
  const response = await fetch('https://api.example.com/data');
  if (!response.ok) throw new Error('Network error');
  const data = await response.json();
} catch (error) {
  const data = null;
}
---
B
---
const response = fetch('https://api.example.com/data');
const data = await response.json();
---
C
---
const response = await fetch('https://api.example.com/data');
const data = await response.json();
---
D
---
const response = await fetch('https://api.example.com/data');
const data = response.json();
---
Attempts:
2 left
💡 Hint

Remember to await fetch and check response.ok to catch HTTP errors.

🔧 Debug
advanced
2:00remaining
What error does this Astro frontmatter code produce?

Analyze this Astro frontmatter code snippet:

---
const response = fetch('https://api.example.com/data');
const data = await response.json();
---

What error will occur when this component runs?

Astro
---
const response = fetch('https://api.example.com/data');
const data = await response.json();
---
ATypeError: response.json is not a function
BReferenceError: response is not defined
CSyntaxError: await is only valid in async function
DRuntimeError: Cannot read property 'json' of undefined
Attempts:
2 left
💡 Hint

Consider what fetch returns if you don't await it.

state_output
advanced
2:00remaining
What is the value of 'data' after this Astro frontmatter fetch?

Given this Astro frontmatter code:

---
const response = await fetch('https://jsonplaceholder.typicode.com/posts/1');
const data = await response.json();
---

What is the value of data?

Astro
---
const response = await fetch('https://jsonplaceholder.typicode.com/posts/1');
const data = await response.json();
---
Aundefined
B{"userId":1,"id":1,"title":"sunt aut facere repellat provident occaecati excepturi optio reprehenderit","body":"quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"}
Cnull
D{"error":"Not Found"}
Attempts:
2 left
💡 Hint

Check the API endpoint and what it returns.

🧠 Conceptual
expert
2:00remaining
Why is fetching API data in Astro frontmatter beneficial compared to client-side fetching?

Choose the best explanation for why fetching API data in Astro frontmatter is often preferred over fetching it on the client side.

ABecause fetching in frontmatter requires no network requests, making the app offline-first.
BBecause fetching in frontmatter allows the client to control when data loads, improving user interactivity.
CBecause fetching in frontmatter delays rendering until the client fetches data, reducing server load.
DBecause fetching in frontmatter happens at build or server time, it improves performance and SEO by sending fully rendered HTML to the browser.
Attempts:
2 left
💡 Hint

Think about when frontmatter code runs and how it affects the HTML sent to the browser.