All notes >

Astro Content Collections: A Few Things Worth Knowing

Practical notes from building a blog and project directory with Astro content collections.


This site — lios.dev — is built with Astro 4, content collections, and a custom design system. Blog posts and project entries are both managed as typed collections, which keeps the data consistent and the components simple. A few things from that build that saved me time.

Schema defaults and optional fields

Use .default() on Zod fields you want to be optional with a fallback — it’s cleaner than .optional() when you need the value downstream without a null check.

icon: z.enum(['calendar', 'layers', 'code']).default('code'),
featured: z.boolean().default(true),

Sorting outside the collection

getCollection() returns entries in filesystem order, which is arbitrary. Always sort explicitly:

const posts = (await getCollection('blog'))
  .sort((a, b) => new Date(b.data.date).getTime() - new Date(a.data.date).getTime());

Type safety in components

Import CollectionEntry from astro:content and type your props — you get full autocomplete and catch frontmatter mismatches at build time rather than runtime.