📖 Scenario: You are building a simple Astro website that shows a list of books. Some data will be fetched at build time (static), and some data will be fetched on each request (server-side).
🎯 Goal: Create an Astro component that fetches a static list of books at build time and also fetches the current date and time on each request using server-side data fetching.
📋 What You'll Learn
Create a static data array of books in the component
Create a server-side function to fetch the current date and time
Display the static books list in the page
Display the server-side fetched date and time in the page
💡 Why This Matters
🌍 Real World
Websites often need to show some data that never changes (static) and some data that updates every time a user visits (server-side).
💼 Career
Understanding static vs server-side data fetching is key for building fast, dynamic websites with frameworks like Astro.
Progress0 / 4 steps
1
Create static book data
In your Astro component, create a constant called books that is an array with these exact objects: { title: 'The Hobbit', author: 'J.R.R. Tolkien' } and { title: '1984', author: 'George Orwell' }.
Astro
Hint
Use const books = [ ... ] with the exact objects inside the array.
2
Add server-side data fetching function
Add an export async function get({ params }) that returns an object with a props property containing the current date and time as currentTime using new Date().toISOString().
Astro
Hint
Define export async function get() that returns { props: { currentTime: new Date().toISOString() } }.
3
Display static books list in the component
Inside the component's HTML, use a {books.map((book) => ( ... ))} expression to create a <li> for each book showing the book's title and author.
Astro
Hint
Use {books.map((book) => ( ... ))} to create list items inside the <ul>.
4
Display server-side fetched current time
Add a <p> below the books list that shows the text Current time: followed by the currentTime prop value.
Astro
Hint
Use a paragraph tag with Current time: {currentTime} to show the server-side data.
Practice
(1/5)
1. What is the main difference between static data fetching and server-side data fetching in Astro?
easy
A. Both fetch data only once at build time.
B. Static fetching gets data on every request; server-side fetching gets data once at build time.
C. Both fetch data on every request.
D. Static fetching gets data once at build time; server-side fetching gets data on every request.
Solution
Step 1: Understand static data fetching
Static fetching means data is fetched once during the build process, so pages load very fast but data stays the same until next build.
Step 2: Understand server-side data fetching
Server-side fetching means data is fetched fresh on every user request, so content is always up-to-date but slower to load.
Final Answer:
Static fetching gets data once at build time; server-side fetching gets data on every request. -> Option D
Quick Check:
Static = build time, Server-side = request time [OK]
C. Return statement should use props instead of body
D. Function should be named getStaticProps
Solution
Step 1: Check the fetch and JSON parsing syntax
The code calls response.json without parentheses, which is a function and must be called as response.json().
Step 2: Validate other parts of the code
The fetch URL looks valid, returning body is correct for server-side, and function name GET is valid for server-side fetch in Astro.
Final Answer:
Missing parentheses after json method call -> Option A
Quick Check:
json() needs parentheses to execute [OK]
Hint: Call json() with parentheses to parse response [OK]
Common Mistakes:
Forgetting parentheses on json()
Confusing server-side and static function names
Using wrong return structure for server-side
5. You want to build a blog with Astro where posts rarely change. Which data fetching method should you use and why?
export async function getStaticPaths() { ... } or export async function GET()?
hard
A. Use GET to fetch posts on every request to ensure always fresh content.
B. Use getStaticPaths to fetch posts once at build time for faster load and stable content.
C. Use getStaticPaths but fetch data on every request inside it.
D. Use GET and cache the data manually to simulate static fetching.
Solution
Step 1: Understand blog content update frequency
Since posts rarely change, fetching once at build time is efficient and fast.
Step 2: Match fetching method to content needs
getStaticPaths fetches data once at build time, ideal for stable content. GET fetches on every request, unnecessary here.
Final Answer:
Use getStaticPaths to fetch posts once at build time for faster load and stable content. -> Option B
Quick Check:
Rarely changing data = static fetch [OK]
Hint: Rarely changing data? Use static fetch for speed [OK]
Common Mistakes:
Choosing server-side fetch for stable data
Mixing fetch methods incorrectly
Trying to fetch every request inside static method
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.