Recall & Review
beginner
What is frontmatter in Astro?
Frontmatter is a special section at the top of an Astro file where you write JavaScript or TypeScript code. It runs before the page renders and is used to fetch data or set variables.
Click to reveal answer
beginner
How do you fetch data from an API in Astro frontmatter?
You use the
fetch() function inside the frontmatter script block to get data from an API. Then you can use that data in your component.Click to reveal answer
intermediate
Why fetch API data in frontmatter instead of inside the component?
Fetching in frontmatter happens at build time or server-side, so the page loads faster and the data is ready when the page renders. It also helps SEO and avoids loading spinners.
Click to reveal answer
beginner
What does this Astro frontmatter code do?
<pre>---
const response = await fetch('https://api.example.com/data');
const data = await response.json();
---</pre>It fetches data from 'https://api.example.com/data' and converts the response to JSON format. The
data variable holds the API data for use in the page.Click to reveal answer
intermediate
How can you handle errors when fetching APIs in Astro frontmatter?
You can use try-catch blocks around the fetch call to catch errors. This way, you can show fallback content or handle the error gracefully.
Click to reveal answer
Where do you write API fetch code in Astro to run before rendering?
✗ Incorrect
API fetch code runs in the frontmatter script block before the page renders.
What does
await fetch(url) do in Astro frontmatter?✗ Incorrect
It fetches data from the URL asynchronously and waits for the response.
Why is fetching API data in frontmatter good for SEO?
✗ Incorrect
Data is ready at page load, so search engines see full content immediately.
How do you convert a fetch response to JSON in Astro frontmatter?
✗ Incorrect
The
response.json() method parses the response body as JSON.What is a good way to handle fetch errors in Astro frontmatter?
✗ Incorrect
Try-catch blocks let you catch and handle errors gracefully.
Explain how to fetch data from an API in Astro frontmatter and use it in your page.
Think about the steps from fetching to displaying data.
You got /4 concepts.
Describe why fetching API data in frontmatter improves page performance and SEO.
Consider what happens when data is ready early.
You got /4 concepts.