0
0
Astroframework~5 mins

Fetching APIs in frontmatter in Astro - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AInside the frontmatter script block
BInside the HTML template
CIn a separate CSS file
DIn a client-side JavaScript file only
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
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
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>
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
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.