at.ncvp.me

First Astro project with 'pages' and 'posts' collections

Turn pages into a content collection

Should I do it? Here are the pros and cons

Posted on 30th May 2026 at 10:40 by Admin


You can turn your /src/pages/ branch into a content collection.
In Astro, you can define any folder in /src/content/ as a collection—whether it is for blog posts, documentation, or deeply nested hierarchical pages.
Here is a breakdown of how it works and the trade-offs to consider.

How to do it

Instead of having physical files deeply nested in /src/pages/xxxx/yyyy/, you would move your Markdown or MDX files into a new collection, for example, /src/content/docs

Define the collection schema in src/content/config.ts.

Create a catch-all dynamic route file at src/pages/[…slug].astro.

In this file, you query the collection using getCollection(), map the slugs, and render the pages.

Pros: Why you should do it

Cons: Why you might want to keep /src/pages/

Implementation

I did it on 1 June following Gemini’s suggestions, and it was surproisingly easy

  1. Convert any .astro files in /src/pages tree to MD or MDX, leaving 404.astro, sitemap.astro and select-pages-posts.astro. Move these to /src/pages.

  2. Create /content/pages directory. Move all MD(X) files and their folders from src/pages to src/content/pages, leaving behind 404.astro, sitemap.astro and select-pages-posts.astro.

  3. Also leave behind /src/pages/posts which contains all the fancy routing stuff fo posts

  4. Revise src/content.config.ts

  5. Add src/pages/[…slug].astro

Conclusion

Good move.

Category: admin Tag: content

Rendered by src/pages/posts/[slug].astro