0
0
Astroframework~20 mins

Why data fetching happens at build time in Astro - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Astro Data Fetching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why does Astro fetch data at build time?

Astro fetches data during the build process instead of at runtime. Why is this approach beneficial?

AIt reduces server load by pre-rendering pages with data, improving performance and SEO.
BIt allows pages to update data dynamically on every user request without rebuilding.
CIt delays data fetching until the user interacts with the page to save bandwidth.
DIt requires a server to run continuously to fetch data on demand.
Attempts:
2 left
💡 Hint

Think about when the page content is generated and how that affects speed and search engines.

component_behavior
intermediate
2:00remaining
What happens if data fetching is done at runtime in Astro?

Consider an Astro page that fetches data at runtime instead of build time. What is the likely effect on the page?

AThe page will not display any data because Astro does not support runtime fetching.
BThe page will load instantly with all data preloaded.
CThe page will load slower because data is fetched when the user visits the page.
DThe page will crash because runtime fetching is forbidden.
Attempts:
2 left
💡 Hint

Think about when the data is requested and how that affects user experience.

📝 Syntax
advanced
2:30remaining
Identify the correct Astro syntax for build-time data fetching

Which code snippet correctly fetches data at build time in an Astro component?

Aexport async function get() { const response = await fetch('https://api.example.com'); const data = await response.json(); return { body: JSON.stringify(data) }; }
Bconst response = await fetch('https://api.example.com'); const data = await response.json(); export default function Page() { return <div>{data.title}</div>; }
Cexport async function getStaticProps() { const data = await fetch('https://api.example.com'); return { props: { data } }; }
Dexport async function getStaticPaths() { const response = await fetch('https://api.example.com'); const data = await response.json(); return data; }
Attempts:
2 left
💡 Hint

Astro uses special exported functions to fetch data at build time and return it for page rendering.

🔧 Debug
advanced
2:30remaining
Why does this Astro page fail to fetch data at build time?

Review the code below. Why does the data not appear on the page after build?

Astro
export default function Page() {
  const data = fetch('https://api.example.com/data').then(res => res.json());
  return <div>{data.title}</div>;
}
ABecause the URL is incorrect and fetch fails silently.
BBecause fetch returns a promise and the component does not wait for it to resolve before rendering.
CBecause Astro does not allow fetch inside components at all.
DBecause the data is fetched at build time but not passed as props.
Attempts:
2 left
💡 Hint

Think about how JavaScript handles promises and rendering in components.

lifecycle
expert
3:00remaining
When exactly does Astro run build-time data fetching functions?

In Astro, when are build-time data fetching functions like getStaticPaths or get executed?

AEvery time a user visits the page, fetching fresh data on the server.
BWhen the browser runs client-side JavaScript after page load.
COnly when the page is first requested after deployment, then cached for later.
DDuring the build process before the site is deployed, generating static HTML with data included.
Attempts:
2 left
💡 Hint

Consider the difference between build time and runtime in static site generation.