Consider this Astro page that declares data at build time. What will the rendered output show?
---
const message = 'Hello from static!';
---
<h1>{message}</h1>Top-level declarations run at build time and are available in the template.
The top-level const message is evaluated at build time and available in the template. So {message} contains 'Hello from static!'.
This Astro page fetches the current time on each request using server-side rendering. What will it display?
---
const prerender = false;
const time = new Date().toISOString();
---
<p>{time}</p>Server-side data fetching runs on every request, so the time updates.
With prerender = false, the top-level code runs on each request and computes the current time.
Which code snippet correctly fetches data at build time in an Astro page?
Static data fetching uses top-level await fetch() in the frontmatter.
Option A uses top-level await fetch() which runs at build time for static pages. Option A is server-side (runs per request), C is an API route handler, D runs client-side in the browser.
Given this Astro component code, why does it throw an error when fetching data?
--- 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>
Server-side rendering runs fetches on every request, so network issues cause runtime errors.
If the API is unreachable during a request, fetch fails and throws an error at runtime. Astro server-side pages execute top-level code on each request.
Choose the most accurate description of the difference between static and server-side data fetching in Astro.
Think about when data is fetched and how often it updates.
Static fetching happens once at build time, embedding data in the generated HTML. Server-side fetching runs on each request, providing fresh data dynamically.