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?
AInside the frontmatter script block
BInside the HTML template
CIn a separate CSS file
DIn a client-side JavaScript file only
✗ Incorrect
API fetch code runs in the frontmatter script block before the page renders.
What does await fetch(url) do in Astro frontmatter?
ASends data to the server
BFetches data from the URL asynchronously
CBlocks the page from loading
DCreates a new HTML element
✗ Incorrect
It fetches data from the URL asynchronously and waits for the response.
Why is fetching API data in frontmatter good for SEO?
ABecause it delays page rendering
BBecause it hides the data from search engines
CBecause data is ready when the page loads
DBecause it uses client-side JavaScript
✗ 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?
AUsing <code>response.text()</code>
BUsing <code>JSON.parse(response)</code>
CUsing <code>response.toString()</code>
DUsing <code>response.json()</code>
✗ Incorrect
The response.json() method parses the response body as JSON.
What is a good way to handle fetch errors in Astro frontmatter?
AUse try-catch blocks around fetch calls
BIgnore errors and continue
CUse CSS to hide errors
DFetch data twice to avoid errors
✗ 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.
Practice
(1/5)
1. What is the main purpose of fetching APIs in the frontmatter of an Astro component?
easy
A. To get data before the page renders so it shows fresh content immediately
B. To delay data loading until after the page is fully loaded
C. To style the page using CSS variables
D. To handle user input events on the page
Solution
Step 1: Understand frontmatter role in Astro
Frontmatter runs before the page renders, allowing data fetching early.
Step 2: Purpose of fetching API in frontmatter
Fetching data here ensures the page shows fresh content immediately when loaded.
Final Answer:
To get data before the page renders so it shows fresh content immediately -> Option A
Quick Check:
Fetching in frontmatter = fresh data before render [OK]
Hint: Frontmatter fetches data before page shows [OK]
Common Mistakes:
Thinking data loads after page renders
Confusing frontmatter with styling or events
Assuming frontmatter runs on client side
2. Which of the following is the correct syntax to fetch data in Astro frontmatter?
easy
A. ---
const response = await fetch('https://api.example.com/data');
const data = await response.json();
---
B.
C. const data = fetch('https://api.example.com/data');
D. ---
fetch('https://api.example.com/data')
---
Solution
Step 1: Identify frontmatter syntax
Astro frontmatter uses triple dashes --- to enclose code that runs before rendering.
Step 2: Correct async fetch usage
Using await fetch and await response.json() inside frontmatter is correct to get JSON data.
Final Answer:
---
const response = await fetch('https://api.example.com/data');
const data = await response.json();
--- -> Option A
Quick Check:
Frontmatter uses --- and await fetch [OK]
Hint: Use --- and await fetch inside frontmatter [OK]
Common Mistakes:
Using fetch without await in frontmatter
Placing fetch inside <script> tag instead of frontmatter
Not parsing response with .json()
3. Given this Astro frontmatter code, what will be the value of data.message in the component?
Assuming the API returns JSON: { "message": "Hello, Astro!" }
medium
A. undefined
B. "Hello, Astro!"
C. SyntaxError
D. null
Solution
Step 1: Understand fetch and JSON parsing
The fetch gets the API response, and await response.json() parses it into an object with a message property.
Step 2: Accessing data.message in component
The component uses {data.message} which will display the string from the API: "Hello, Astro!".
Final Answer:
"Hello, Astro!" -> Option B
Quick Check:
API message property = "Hello, Astro!" [OK]
Hint: Parsed JSON data shows in component as expected [OK]
Common Mistakes:
Forgetting to await response.json()
Trying to access data before fetch completes
Assuming data.message is undefined or null
4. What is wrong with this Astro frontmatter code?
---
const data = fetch('https://api.example.com/data');
const json = data.json();
---
medium
A. Using triple dashes --- incorrectly
B. json() is not a function
C. fetch URL is invalid syntax
D. Missing await before fetch and json() calls
Solution
Step 1: Check async fetch usage
fetch returns a Promise, so it must be awaited to get the response object.
Step 2: Check json() call
response.json() also returns a Promise and must be awaited to get parsed data.
Final Answer:
Missing await before fetch and json() calls -> Option D
Quick Check:
Always await fetch and response.json() [OK]
Hint: Always await fetch and json() in frontmatter [OK]
Common Mistakes:
Not using await causing unresolved Promises
Assuming fetch is synchronous
Calling json() on Promise instead of response
5. You want to fetch a list of users from an API in Astro frontmatter and display their names. Which approach correctly fetches and prepares the data for rendering?
---
// Option A
const res = await fetch('https://api.example.com/users');
const users = await res.json();
---
{users.map(user =>
{user.name}
)}
hard
A. Fetch data in frontmatter but forget to parse JSON
B. Fetch without await, map users directly on Promise
C. Fetch with await, parse JSON, then map users in component
D. Use fetch inside component JSX without frontmatter
Solution
Step 1: Fetch and parse JSON in frontmatter
Using await fetch and await res.json() gets the user list as an array before rendering.
Step 2: Map over users array in component
Mapping users to list items displays each user name correctly in the rendered HTML.
Final Answer:
Fetch with await, parse JSON, then map users in component -> Option C
Quick Check:
Await fetch + parse + map users = correct [OK]
Hint: Await fetch and parse JSON before mapping data [OK]
Common Mistakes:
Mapping over unresolved Promise instead of data array
Fetching inside JSX causing runtime errors
Not parsing JSON before using data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.