Performance: Why content collections organize structured content
Organizing content collections affects page load speed by enabling efficient data fetching and rendering of structured content.
const blogPosts = getContentCollection('blog');
render(blogPosts);const allContent = fetchAllFilesFromDisk(); const filteredContent = allContent.filter(item => item.type === 'blog'); render(filteredContent);
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Fetching all files then filtering | High (many nodes) | Multiple reflows | High paint cost | [X] Bad |
| Using content collections to fetch filtered data | Low (only needed nodes) | Single reflow | Low paint cost | [OK] Good |
defineCollection with a zod schema object to define content collections.defineCollection and z.object correctly, matching Astro's recommended pattern.export const blog = defineCollection({ schema: z.object({ title: z.string(), published: z.boolean() }) })published field?published is missing, the validation fails and Astro throws an error.export const docs = defineCollection({ schema: z.object({ title: z.string(), date: z.string() }) })date as JavaScript Date objects. What is the problem and how to fix it?date as a string, but content provides Date objects, causing validation failure.z.date() to accept Date objects properly.z.string(), z.date(), z.array(z.string()), and z.string().optional() correctly.Pass the test and this pattern moves unknown