Incremental builds with data let your website update only the parts that changed. This saves time and makes your site faster to build and load.
Incremental builds with data in Astro
export async function getStaticPaths() { const data = await fetchData() return data.map(item => ({ params: { id: item.id } })) } export async function getStaticProps({ params }) { const itemData = await fetchDataById(params.id) return { props: { itemData } } }
getStaticPaths tells Astro which pages to build based on your data.
getStaticProps fetches data for each page during build time.
export async function getStaticPaths() { return [ { params: { id: '1' } }, { params: { id: '2' } } ] } export async function getStaticProps({ params }) { return { props: { message: `Page for id ${params.id}` } } }
export async function getStaticPaths() { const posts = await fetch('https://api.example.com/posts').then(res => res.json()) return posts.map(post => ({ params: { slug: post.slug } })) } export async function getStaticProps({ params }) { const post = await fetch(`https://api.example.com/posts/${params.slug}`).then(res => res.json()) return { props: { post } } }
This Astro component builds two pages: one for id 'a' and one for 'b'. Each page shows its own title and content. Only these pages are built, saving time.
--- export async function getStaticPaths() { return [ { params: { id: 'a' } }, { params: { id: 'b' } } ] } export async function getStaticProps({ params }) { const data = { a: { title: 'Page A', content: 'Content for A' }, b: { title: 'Page B', content: 'Content for B' } } return { props: { page: data[params.id] } } } --- <html lang="en"> <head><title>{page.title}</title></head> <body> <h1>{page.title}</h1> <p>{page.content}</p> </body> </html>
Incremental builds only rebuild pages with changed data, making builds faster.
Use getStaticPaths to control which pages get built.
Make sure your data fetching in getStaticProps is fast and reliable.
Incremental builds update only needed pages, saving time.
Use getStaticPaths and getStaticProps to fetch data for pages.
This approach improves site speed and developer experience.
Practice
Solution
Step 1: Understand incremental build purpose
Incremental builds focus on rebuilding only what changed, not the whole site.Step 2: Identify the benefit in context
This saves build time and speeds up development and deployment.Final Answer:
Only pages with changed data are rebuilt, saving time -> Option AQuick Check:
Incremental builds = rebuild changed pages only [OK]
- Thinking all pages rebuild every time
- Confusing incremental builds with disabling data fetching
- Assuming it automatically deploys the site
Solution
Step 1: Recall Astro data fetching functions
Astro usesgetStaticPathsto define dynamic routes for static generation.Step 2: Match function to incremental build role
getStaticPathstells Astro which pages to build incrementally with data.Final Answer:
getStaticPaths -> Option DQuick Check:
Page list for incremental build = getStaticPaths [OK]
- Confusing getStaticPaths with getServerSideProps
- Using React hooks like useEffect in Astro build functions
- Thinking renderToString controls incremental builds
export async function getStaticPaths() {
return [
{ params: { id: '1' } },
{ params: { id: '2' } }
];
}Solution
Step 1: Analyze getStaticPaths return value
The function returns an array with params objects containing id '1' and '2'.Step 2: Understand path generation
Astro uses these params to build pages at /1 and /2 paths incrementally.Final Answer:
Pages for /1 and /2 will be built incrementally -> Option CQuick Check:
Params id '1' and '2' create /1 and /2 pages [OK]
- Assuming only first param builds a page
- Thinking return type must be an object, not array
- Confusing path format with nested folders
export async function getStaticPaths() {
return {
params: [
{ id: 'a' },
{ id: 'b' }
]
};
}Solution
Step 1: Check getStaticPaths return type
Astro expects getStaticPaths to return an array of params objects, not an object wrapping an array.Step 2: Identify correct return structure
The code incorrectly returns an object with params array instead of just the array.Final Answer:
getStaticPaths must return an array, not an object -> Option AQuick Check:
Return type must be array for getStaticPaths [OK]
- Wrapping params array inside an object
- Making function synchronous when async is allowed
- Returning a function instead of data
Solution
Step 1: Understand incremental build with data fetching
Incremental builds require listing pages (post IDs) with getStaticPaths and fetching data per page with getStaticProps.Step 2: Match approach to requirement
Use getStaticPaths to list all post IDs, then getStaticProps to fetch data for each ID uses getStaticPaths to specify posts and getStaticProps to fetch each post's data, enabling incremental rebuilds.Final Answer:
Use getStaticPaths to list all post IDs, then getStaticProps to fetch data for each ID -> Option BQuick Check:
Incremental builds = getStaticPaths + getStaticProps [OK]
- Fetching all posts at once disables incremental builds
- Relying only on client-side fetching loses static benefits
- Rebuilding entire site wastes time and defeats incremental purpose
Ready to prove it?
Pass the test and this pattern moves unknown
