Challenge - 5 Problems
Astro Content Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediateWhat is the main purpose of content collections in Astro?
Content collections in Astro help organize content in a specific way. What is their main purpose?
Attempts:
2 left
💡 Hint
Think about how content collections help manage content beyond just files.
✗ Incorrect
Content collections group related content files that have structured data, making it easier to query and display them in Astro projects.
❓ component_behavior
intermediateHow does Astro use content collections to render pages?
Given a content collection of blog posts, how does Astro typically use this collection to render a list of posts?
Astro
const posts = await getCollection('blog'); return posts.map(post => <article>{post.data.title}</article>);
Attempts:
2 left
💡 Hint
Look at how the code uses the collection data to create components.
✗ Incorrect
Astro fetches all items in the collection and maps over them to render components, such as articles, for each post.
📝 Syntax
advancedIdentify the correct syntax to define a content collection in Astro.
Which of the following code snippets correctly defines a content collection named 'products' with a schema in Astro?
Attempts:
2 left
💡 Hint
Astro uses an object with collection names as keys and schema definitions using zod.
✗ Incorrect
Astro defines content collections as an exported object with collection names as keys and schema using zod for validation.
🔧 Debug
advancedWhy does this Astro content collection query fail?
Given this code snippet, why does the query fail to return any items?
const posts = await getCollection('articles');
console.log(posts.length);
Attempts:
2 left
💡 Hint
Check if the collection name matches the defined collections.
✗ Incorrect
If the collection name does not match any defined collection, getCollection returns an empty array or fails.
❓ state_output
expertWhat is the output of this Astro content collection filtering code?
Consider this code filtering a content collection by a tag:
const posts = await getCollection('blog');
const filtered = posts.filter(post => post.data.tags.includes('astro'));
console.log(filtered.length);
Attempts:
2 left
💡 Hint
Think about how filter and includes work on arrays.
✗ Incorrect
The code filters posts to only those whose tags array includes 'astro', so the output is the count of those posts.
