Data fetching at build time helps create fast websites by getting all needed data before users visit. This means pages load quickly without waiting for data to arrive.
Why data fetching happens at build time in Astro
export async function getStaticPaths() { // fetch data here return { paths: [{ params: { /* path params */ } }], fallback: false }; }
getStaticPaths defines which dynamic pages to build ahead of time and can provide fetched data via props.
For non-dynamic pages, fetch data directly in the frontmatter (between --- blocks).
--- delimiters) and use it in the template.--- const res = await fetch('https://api.example.com/data'); const data = await res.json(); ---
props in paths.export async function getStaticPaths() { const res = await fetch('https://api.example.com/items'); const items = await res.json(); const paths = items.map(item => ({ params: { id: item.id.toString() } })); return { paths, fallback: false }; }
This Astro page fetches a post at build time in the frontmatter and renders its title and body. The data is embedded, so users see content immediately with no loading delay.
--- const res = await fetch('https://jsonplaceholder.typicode.com/posts/1'); const post = await res.json(); --- <article> <h1>{post.title}</h1> <p>{post.body}</p> </article>
Fetching data at build time means the site needs to be rebuilt to update content.
Good for blogs, documentation, or product pages that don't change often.
Not ideal for real-time or user-specific data that changes frequently.
Data fetching at build time makes pages load faster by preloading content.
It improves SEO because content is ready for search engines.
Best for mostly static content that updates occasionally.
Practice
Solution
Step 1: Understand build time data fetching
Astro fetches data once during build, so pages have content ready before users visit.Step 2: Connect build time fetching to page speed
Since content is preloaded, pages load faster for users without waiting for data requests.Final Answer:
To make pages load faster by preloading content -> Option AQuick Check:
Build time fetching = faster page load [OK]
- Thinking data fetching happens on every user request
- Confusing build time with runtime fetching
- Believing it reduces file size directly
- Assuming it blocks search engine indexing
Solution
Step 1: Identify Astro's build time data fetching method
Astro usesgetStaticPaths()to fetch data at build time for generating pages.Step 2: Differentiate from other frameworks
getStaticProps()is from Next.js, not Astro;fetch()inside render runs at runtime, not build time.Final Answer:
Useexport async function getStaticPaths()to fetch data -> Option DQuick Check:
Astro build time fetch = getStaticPaths() [OK]
- Using getStaticProps() which is Next.js specific
- Fetching data inside render causing runtime fetch
- Confusing fetchContent() as a standard method
- Not exporting the function properly
export async function getStaticPaths() {
return [{ params: { id: '1' } }, { params: { id: '2' } }];
}
const data = { '1': 'Apple', '2': 'Banana' };
export default function Page({ params }) {
return {data[params.id]}
;
}Solution
Step 1: Understand getStaticPaths output
It returns two paths with ids '1' and '2', so two pages will be built.Step 2: Check data usage in component
Component uses params.id to get data from the object, so 'Apple' and 'Banana' will render correctly.Final Answer:
<p>Apple</p> and <p>Banana</p> pages generated at build time -> Option AQuick Check:
getStaticPaths creates pages for each id [OK]
- Assuming only one page is generated
- Thinking data must be fetched inside component
- Confusing build time with runtime data fetching
- Believing data object is undefined
export async function getStaticPaths() {
const response = await fetch('https://api.example.com/items');
const items = await response.json();
return items.map(item => ({ params: { id: item.id } }));
}
export default function Page({ params }) {
return {params.id}
;
}Solution
Step 1: Check getStaticPaths return format
Astro expects getStaticPaths to return an object with a 'paths' array, not just an array.Step 2: Identify correct return structure
The correct return is{ paths: [...] }, so returning only the array causes an error.Final Answer:
getStaticPaths must return an object with a 'paths' key -> Option BQuick Check:
getStaticPaths return = { paths: [...] } [OK]
- Returning array directly instead of object
- Thinking fetch is disallowed in getStaticPaths
- Assuming params are inaccessible in component
- Forgetting async on component (not required)
Solution
Step 1: Understand the nature of mostly static content
Posts that change rarely benefit from preloading data at build time for speed and SEO.Step 2: Match build time fetching benefits to blog needs
Preloading content means pages load quickly and search engines see full content, ideal for mostly static blogs.Final Answer:
Because it preloads content for fast page loads and good SEO, suitable for mostly static posts -> Option CQuick Check:
Mostly static content = build time fetch for speed & SEO [OK]
- Thinking build time fetch updates instantly on every visit
- Believing it disables caching completely
- Assuming no code is needed to fetch data
- Confusing build time with runtime fetching
Ready to prove it?
Pass the test and this pattern moves unknown
