Data fetching is how your website gets information to show. Static fetching gets data once when building the site. Server-side fetching gets data every time someone visits.
Static vs server-side data fetching in Astro
export async function getStaticPaths() { // returns paths for static/dynamic pages at build time } // Static fetching: fetch data directly in .astro frontmatter // const res = await fetch('https://api.example.com/data'); // const data = await res.json(); // Server-side fetching: // export const prerender = false; // const res = await fetch('https://api.example.com/data'); // const data = await res.json();
Static fetching happens once during build, so pages load very fast.
Server-side fetching happens on every request, so data is always fresh but slower.
--- const res = await fetch('https://api.example.com/posts') const posts = await res.json() ---
--- export const prerender = false; const res = await fetch('https://api.example.com/posts') const posts = await res.json() ---
This Astro page fetches a todo item once during build time using static rendering (default). The page shows the todo details. The data won't change until you rebuild the site.
--- const res = await fetch('https://jsonplaceholder.typicode.com/todos/1') const todo = await res.json() --- <html lang="en"> <head> <title>Static Data Fetching Example</title> </head> <body> <h1>Todo Item</h1> <p>ID: {todo.id}</p> <p>Title: {todo.title}</p> <p>Completed: {todo.completed ? 'Yes' : 'No'}</p> </body> </html>
Static fetching is great for speed but not for frequently changing data.
Server-side fetching can slow down your site if used too much.
Choose the method based on how fresh your data needs to be.
Static fetching gets data once at build time for fast pages.
Server-side fetching gets data on every request for fresh content.
Pick static for stable data, server-side for dynamic data.
Practice
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 DQuick Check:
Static = build time, Server-side = request time [OK]
- Confusing when data is fetched
- Thinking static fetches on every request
- Assuming server-side fetches only once
Solution
Step 1: Identify Astro static data fetching syntax
Astro usesgetStaticPathsto fetch data at build time and generate static paths.Step 2: Check options for correct function and return structure
export async function getStaticPaths() { return { paths: [] } } correctly definesgetStaticPathsreturning paths. Others use wrong function names or incomplete returns.Final Answer:
export async function getStaticPaths() { return { paths: [] } } -> Option CQuick Check:
Static fetch uses getStaticPaths [OK]
- Using wrong function names like getStatic
- Returning data without paths wrapper
- Using getStaticProps which is from Next.js
export async function GET({ params }) {
const res = await fetch('https://api.example.com/greet');
const data = await res.json();
return {
body: JSON.stringify({ greeting: data.message })
};
}Solution
Step 1: Analyze the fetch and JSON parsing
The code fetches from the API, parses JSON, and extracts the 'message' field which is "Hello".Step 2: Check the returned body structure
The returned body is a JSON string with key 'greeting' set to data.message, so output is {"greeting":"Hello"}.Final Answer:
{"greeting":"Hello"} -> Option AQuick Check:
Returned JSON key is greeting with value Hello [OK]
- Confusing returned keys with API keys
- Expecting raw API response instead of transformed
- Assuming fetch error without context
export async function GET() {
const response = await fetch('https://api.example.com/data');
const data = await response.json;
return {
body: JSON.stringify(data)
};
}Solution
Step 1: Check the fetch and JSON parsing syntax
The code callsresponse.jsonwithout parentheses, which is a function and must be called asresponse.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 AQuick Check:
json() needs parentheses to execute [OK]
- Forgetting parentheses on json()
- Confusing server-side and static function names
- Using wrong return structure for server-side
export async function getStaticPaths() { ... } or export async function GET()?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
getStaticPathsfetches data once at build time, ideal for stable content.GETfetches on every request, unnecessary here.Final Answer:
Use getStaticPaths to fetch posts once at build time for faster load and stable content. -> Option BQuick Check:
Rarely changing data = static fetch [OK]
- Choosing server-side fetch for stable data
- Mixing fetch methods incorrectly
- Trying to fetch every request inside static method
Ready to prove it?
Pass the test and this pattern moves unknown
