Skip to main content

Overview

Astro Portfolio v3 uses Astro’s Content Collections API to manage all content with type safety and validation. Content is organized into collections, each with its own schema defined using Zod.
All content collections are defined in ~/workspace/source/src/content.config.ts with their schemas and validation rules.

Content Collections Types

The project uses two types of content collections:
  1. Content-based collections - Store Markdown/MDX files with frontmatter
  2. Data-based collections - Load JSON files using Astro’s file loader

Available Collections

Blog Collection

The blog collection stores all blog posts as Markdown files.Schema Definition:
Example Blog Post:
Location: src/content/blog/Usage:

Working with Content Collections

Fetching All Entries

Fetching a Single Entry

Filtering Collections

Sorting Collections

Adding New Content

Adding a Blog Post

  1. Create a new Markdown file in src/content/blog/:
  1. The post will automatically be available through the blog collection

Adding a Project

  1. Create a new Markdown file in src/content/projects/:

Adding Data to JSON Collections

Edit the corresponding JSON file in src/data/:

Type Safety

Content Collections provide full TypeScript support:

Generating Dynamic Routes

Use content collections to generate pages:

Schema Validation

Zod schemas ensure data integrity:

Advanced Techniques

Querying by Series

Using Content in Components

Rendering MDX Components

If using MDX, you can pass components to the renderer:

Best Practices

  1. Use strict schemas - Define all required fields explicitly
  2. Leverage Zod coercion - Use z.coerce.date() for date fields
  3. Keep collections focused - Don’t mix unrelated content types
  4. Use optional fields wisely - Mark fields as optional only when necessary
  5. Validate URLs - Use z.string().url() for URL fields
  6. Sort consistently - Always sort collections before rendering
  7. Cache collection queries - Queries are cached during build
  8. Use TypeScript types - Leverage CollectionEntry for type safety

Project Structure

See where content files are located

Components

Learn how components use content collections

i18n

Understand internationalized content