π Recommended Project Structure
In an Astro project, the src/
directory is the main source folder where most of your projectβs logic, components, and content reside. Within src/
, youβll often see the following directories:
src/
βββ π content/ # Content Collections
β βββ π blog/
β βββ π projects/
βββ π data/ # Static data
β βββ π site-config.ts
β βββ π navigation.ts
βββ π lib/ # Utilities and helpers
βββ π utils/
βββ π hooks/
βββ π types/
π src/data/
β Static & Structured Data
The data
directory is used for storing structured data that doesnβt change dynamically but is still separate from your content files. This is useful for things like:
- JSON, YAML, TOML, or CSV files containing site-wide information.
- Hardcoded lists of features, pricing plans, team members, etc.
- Any data fetched from external sources and stored locally for performance reasons.
πΉ Example
[
{ "name": "Alice", "role": "Developer" },
{ "name": "Bob", "role": "Designer" }
]
πΉ Usage in Astro
---
import team from "../data/team.json";
---
<ul>
{team.map(person => (
<li>{person.name} - {person.role}</li>
))}
</ul>
π src/content/
β Content Collections
This is the recommended place for Markdown-based content collections, such as:
- Blog posts
- Documentation pages
- FAQs
- Any structured content thatβs meant to be written in Markdown, MDX, JSON, or YAML.
Astroβs Content Collections API uses this directory to define and validate structured content.
πΉ Example
---
title: "My First Blog Post"
date: "2025-02-21"
author: "Christian"
---
This is my first blog post in Astro!
πΉ Usage in Astro
---
import { getCollection } from "astro:content";
const posts = await getCollection("blog");
---
<ul>
{posts.map(post => (
<li><a href={post.slug}>{post.data.title}</a></li>
))}
</ul>
πΉ Config
import { defineCollection, z } from "astro:content";
const blog = defineCollection({
schema: z.object({
title: z.string(),
date: z.string(),
author: z.string(),
}),
});
export const collections = { blog };
π Why use src/content/
instead of src/data/
?
src/content/
is optimized for Markdown-based content.- The Content Collections API allows validation, type safety, and querying.
src/data/
is better for purely structured non-content data like JSON lists.
π src/lib/
β Utility & Helper Functions
This is where you store reusable functions, scripts, and utilities that donβt belong to any specific page or component. Itβs useful for:
- Remark/Rehype plugins for Markdown processing.
- Utility functions (e.g., formatting dates, fetching APIs).
- Custom hooks or helpers used across multiple components.
Formatters
Common utilities include date and string formatters:
export const formatDate = (date: Date): string => {
return new Date(date).toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
});
};
export const slugify = (text: string): string => {
return text
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/(^-|-$)+/g, '');
};
Metadata Utilities
Helper functions for managing page metadata:
interface PageMeta {
title: string;
description: string;
image?: string;
}
export const generateMeta = ({ title, description, image }: PageMeta) => ({
title: `${title} | My Site`,
description,
ogImage: image || '/default-og.png',
// Add more metadata as needed
});
πΉ Example
export function formatDate(date: string): string {
return new Date(date).toLocaleDateString("en-US", {
year: "numeric",
month: "long",
day: "numeric",
});
}
πΉ Usage in an Astro Component
---
import { formatDate } from "../lib/formatDate";
import { getCollection } from "astro:content";
const posts = await getCollection("blog");
---
<ul>
{posts.map(post => (
<li>{formatDate(post.data.date)} - {post.data.title}</li>
))}
</ul>
π Final Takeaways
Directory | Purpose |
---|---|
src/data/ | Stores structured data (JSON, YAML, CSV, etc.) for things like team members, pricing, etc. |
src/content/ | Stores Markdown-based content collections (blogs, docs, FAQs) with type validation. |
src/lib/ | Stores reusable utility functions and custom plugins/helpers. |
Each directory serves a specific purpose in an Astro project, helping maintain a clean and organized structure.
Let me know if you need further clarification! π