Discover how choosing when to fetch data can make your website lightning fast or always up-to-date!
Static vs server-side data fetching in Astro - When to Use Which
Imagine building a website where you have to update product prices every time a user visits the page by manually fetching data and rewriting HTML for each request.
This manual approach is slow because the server does all the work on every visit, making pages load late. It also risks showing outdated info if you don't fetch fresh data properly.
Static and server-side data fetching let you choose when and how to get data: either once ahead of time for fast pages or on each request for fresh info, making your site faster and more reliable.
fetch data on every page load and rebuild HTML manually--- const data = await fetch('https://api.example.com/data').then(r => r.json()); --- // Static at build time (default) or server-side on demand with `export const prerender = false;`
You can build websites that load instantly or always show the latest data without extra work.
An online store showing product details: static fetching pre-builds pages for fast browsing, server-side fetching updates stock info live.
Manual data fetching on every visit slows down pages and risks stale info.
Static fetching preloads data once for speed.
Server-side fetching gets fresh data on each request for accuracy.
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
