0
0
Astroframework~20 mins

Static vs server-side data fetching in Astro - Practice Questions

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!
component_behavior
intermediate
2:00remaining
What will this Astro component render?

Consider this Astro page that declares data at build time. What will the rendered output show?

Astro
---
const message = 'Hello from static!';
---

<h1>{message}</h1>
A<h1>Hello from server!</h1>
B<h1>undefined</h1>
C<h1>Hello from static!</h1>
D<h1>Error: props not found</h1>
Attempts:
2 left
💡 Hint

Top-level declarations run at build time and are available in the template.

state_output
intermediate
2:00remaining
What data does this server-side Astro component show on each request?

This Astro page fetches the current time on each request using server-side rendering. What will it display?

Astro
---
const prerender = false;
const time = new Date().toISOString();
---

<p>{time}</p>
AThe timestamp from build time, never changing
BThe current ISO timestamp updated on every page load
CAn error because fetch is not allowed in Astro components
DEmpty content because the API route is missing
Attempts:
2 left
💡 Hint

Server-side data fetching runs on every request, so the time updates.

📝 Syntax
advanced
2:00remaining
Which option correctly fetches static data in Astro?

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

A
---
const res = await fetch('https://api.example.com/data');
const data = await res.json();
---
B
---
const prerender = false;
const res = await fetch('https://api.example.com/data');
const data = await res.json();
---
C
export async function GET() {
  const res = await fetch('https://api.example.com/data');
  const data = await res.json();
  return new Response(JSON.stringify(data));
}
D
&lt;script type="module"&gt;
  const res = await fetch('https://api.example.com/data');
  const data = await res.json();
&lt;/script&gt;
Attempts:
2 left
💡 Hint

Static data fetching uses top-level await fetch() in the frontmatter.

🔧 Debug
advanced
2:00remaining
Why does this server-side fetch cause a runtime error?

Given this Astro component code, why does it throw an error when fetching data?

Astro
---
const prerender = false;
const res = await fetch('https://api.example.com/data');
if (!res.ok) throw new Error('Failed to fetch');
const data = await res.json();
---

<h1>{data.title}</h1>
Aawait fetch cannot be used in server-side pages
Bthrow new Error is not allowed in frontmatter
Cdata is undefined in the template
DThe fetch URL is unreachable causing a network error
Attempts:
2 left
💡 Hint

Server-side rendering runs fetches on every request, so network issues cause runtime errors.

🧠 Conceptual
expert
2:00remaining
Which statement best describes static vs server-side data fetching in Astro?

Choose the most accurate description of the difference between static and server-side data fetching in Astro.

AStatic fetching runs once at build time and embeds data in the HTML; server-side fetching runs on every request generating fresh data.
BStatic fetching runs on every user request; server-side fetching runs only once during deployment.
CStatic fetching requires client-side JavaScript; server-side fetching does not use JavaScript at all.
DStatic fetching and server-side fetching are identical in Astro and differ only in naming.
Attempts:
2 left
💡 Hint

Think about when data is fetched and how often it updates.