Complete the code to fetch data at build time using Astro's static data fetching.
export async function getStaticPaths() {
const response = await fetch('[1]');
const data = await response.json();
return { paths: data.items };
}Static data fetching uses external URLs or local files available at build time. Here, a full URL is needed.
Complete the code to fetch data on each request using Astro's server-side data fetching.
export async function get({ params }) {
const response = await fetch('[1]');
const data = await response.json();
return { body: JSON.stringify(data) };
}Server-side data fetching happens on each request, so it can call dynamic APIs like external endpoints.
Fix the error in this Astro component to correctly fetch data server-side.
--- const response = await fetch('[1]'); const data = await response.json(); --- <h1>{data.title}</h1>
Server-side fetch in Astro components must use URLs accessible at request time, like external APIs.
Fill both blanks to create a static data fetching function that filters items with id greater than 10.
export async function getStaticPaths() {
const res = await fetch('[1]');
const data = await res.json();
return {
paths: data.items.filter(item => item.id [2] 10)
};
}The static fetch uses a full URL, and the filter checks for ids greater than 10 using >.
Fill all three blanks to create a server-side fetch that returns JSON with only active users.
export async function get() {
const res = await fetch('[1]');
const users = await res.json();
const activeUsers = users.filter(user => user.[2] === [3]);
return {
body: JSON.stringify(activeUsers)
};
}The server fetch calls the API URL, filters users by 'status' equal to 'active', and returns JSON.