📖 Scenario: You are building a simple Astro website that shows a list of books. You want to use incremental builds to only update the pages for books that have changed. This helps your site build faster and stay up to date.
🎯 Goal: Create an Astro component that loads a list of books, sets a build configuration for incremental builds, filters books by a minimum rating, and exports the final filtered list for rendering.
📋 What You'll Learn
Create a constant books with exactly three book objects with title and rating properties
Create a constant minRating set to 4
Use a filter method on books to create filteredBooks with only books having rating >= minRating
Export filteredBooks as the default export for the component
💡 Why This Matters
🌍 Real World
Incremental builds help websites update only changed parts, saving time and resources when deploying content like book lists or product catalogs.
💼 Career
Understanding incremental builds and data filtering is important for frontend developers working with modern frameworks like Astro to optimize site performance.
Progress0 / 4 steps
1
Create the initial books data
Create a constant called books that is an array with these exact objects: { title: 'Astro Basics', rating: 5 }, { title: 'Learn JavaScript', rating: 3 }, and { title: 'Web Design', rating: 4 }.
Astro
Hint
Use const books = [ ... ] with the exact objects inside the array.
2
Add a minimum rating configuration
Create a constant called minRating and set it to the number 4.
Astro
Hint
Use const minRating = 4; to set the minimum rating.
3
Filter books by minimum rating
Create a constant called filteredBooks that filters books to include only those with rating greater than or equal to minRating. Use the filter method with a callback that takes book as parameter.
Astro
Hint
Use books.filter(book => book.rating >= minRating) to filter the array.
4
Export the filtered books for incremental build
Add a default export that exports the filteredBooks constant.
Astro
Hint
Use export default filteredBooks; to export the filtered list.
Practice
(1/5)
1. What is the main benefit of using incremental builds in Astro projects?
easy
A. Only pages with changed data are rebuilt, saving time
B. All pages are rebuilt every time for consistency
C. It disables data fetching during build
D. It automatically deploys the site to a server
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 A
Quick Check:
Incremental builds = rebuild changed pages only [OK]
Hint: Incremental means update only changed parts [OK]
Common Mistakes:
Thinking all pages rebuild every time
Confusing incremental builds with disabling data fetching
Assuming it automatically deploys the site
2. Which Astro function is used to specify which pages should be built incrementally with data?
easy
A. useEffect
B. getServerSideProps
C. renderToString
D. getStaticPaths
Solution
Step 1: Recall Astro data fetching functions
Astro uses getStaticPaths to define dynamic routes for static generation.
Step 2: Match function to incremental build role
getStaticPaths tells Astro which pages to build incrementally with data.
Final Answer:
getStaticPaths -> Option D
Quick Check:
Page list for incremental build = getStaticPaths [OK]
Hint: Use getStaticPaths to list pages for static build [OK]
Common Mistakes:
Confusing getStaticPaths with getServerSideProps
Using React hooks like useEffect in Astro build functions
A. getStaticPaths must return an array, not an object
B. Params should be a string, not an array
C. Function must be synchronous, not async
D. Return value should be a function, not an object
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 A
Quick Check:
Return type must be array for getStaticPaths [OK]
Hint: Return array directly from getStaticPaths [OK]
Common Mistakes:
Wrapping params array inside an object
Making function synchronous when async is allowed
Returning a function instead of data
5. You want to build a blog with incremental builds in Astro. You fetch posts from an API and want only new or updated posts to rebuild. Which approach correctly implements this?
hard
A. Use getStaticProps only once to fetch all posts and build all pages at once
B. Use getStaticPaths to list all post IDs, then getStaticProps to fetch data for each ID
C. Use client-side fetching only, no static generation
D. Manually rebuild the entire site on every post update
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 B