This Astro blog tutorial builds a complete blog in Astro 7: a project from the minimal template, posts in a content collection with a typed schema, a post layout, a sorted index page, an RSS feed, a sitemap, and a free deploy. Every step has the exact file and code, and the finished site is static HTML with no client JavaScript.
Plan on about an hour if you type everything out. You need Node.js 22.23.4 or newer and a terminal.
Key Takeaways
- Start with
npm create astro@latestand the minimal template so you understand every file.- Store posts as Markdown in
src/content/blog/and define the collection insrc/content.config.ts.- One dynamic route,
src/pages/blog/[id].astro, generates a page for every post withgetStaticPaths().@astrojs/rssand@astrojs/sitemapadd a feed and a sitemap in a few lines, oncesiteis set.npm run buildoutputs plain files indist/that any static host can serve.
Step 1: create the project
Run the Astro setup wizard:
npm create astro@latest my-blog -- --template minimal
cd my-blog
npm run dev
Choose to install dependencies and initialize git when prompted. Open http://localhost:4321 and you’ll see a single page. The project has a small set of files:
my-blog/
astro.config.mjs
package.json
public/
src/
pages/
index.astro
tsconfig.json
src/pages/ is file-based routing: every .astro or .md file there becomes a URL. The project structure explainer describes each folder in detail. If you’d rather start from the official blog starter, --template blog gives you a finished version of what this tutorial builds.
Step 2: set the site URL
Several features need to know your production domain. Set it now:
// astro.config.mjs
import { defineConfig } from 'astro/config';
export default defineConfig({
site: 'https://blog.example.com',
});
Use your real domain. You can change it later, but RSS links, canonical URLs and the sitemap all depend on it.
Step 3: define the blog collection
Create the collection config:
// src/content.config.ts
import { defineCollection } from 'astro:content';
import { glob } from 'astro/loaders';
import { z } from 'astro/zod';
const blog = defineCollection({
loader: glob({ pattern: '**/*.md', base: './src/content/blog' }),
schema: z.object({
title: z.string(),
description: z.string().max(160),
pubDate: z.coerce.date(),
updatedDate: z.coerce.date().optional(),
tags: z.array(z.string()).default([]),
draft: z.boolean().default(false),
}),
});
export const collections = { blog };
The schema is a contract for every post. If a post is missing a title, or the pubDate isn’t a valid date, the build stops with a message pointing to the file. The Zod frontmatter guide explains each validator.
Step 4: write your first posts
Add two Markdown files:
<!-- src/content/blog/hello-world.md -->
---
title: Hello, world
description: The first post on a new Astro blog.
pubDate: 2026-11-01
tags: [news]
---
This blog is built with Astro. Every page is static HTML.
## Why Astro
It ships zero JavaScript unless a component asks for it.
<!-- src/content/blog/writing-in-markdown.md -->
---
title: Writing posts in Markdown
description: How posts on this blog are written and organized.
pubDate: 2026-11-02
tags: [guides, markdown]
---
Each post is a Markdown file with frontmatter at the top.
The file name becomes the post’s id, so hello-world.md will live at /blog/hello-world/. Restart the dev server after creating src/content.config.ts so Astro generates the collection types.
Step 5: create a base layout
A layout wraps every page with the same <head>, header and footer:
---
// src/layouts/BaseLayout.astro
interface Props {
title: string;
description: string;
}
const { title, description } = Astro.props;
const canonical = new URL(Astro.url.pathname, Astro.site);
---
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>{title}</title>
<meta name="description" content={description} />
<link rel="canonical" href={canonical} />
<link rel="alternate" type="application/rss+xml" title="My Blog" href="/rss.xml" />
</head>
<body>
<header>
<a href="/">My Blog</a>
<nav><a href="/blog/">Posts</a></nav>
</header>
<main><slot /></main>
<footer>Built with Astro</footer>
</body>
</html>
<style is:global>
body { font-family: system-ui, sans-serif; max-width: 42rem; margin: 0 auto; padding: 1rem; line-height: 1.6; }
header { display: flex; justify-content: space-between; margin-bottom: 2rem; }
</style>
<slot /> is where each page’s content goes. The is:global style applies site-wide; normal <style> blocks in Astro are scoped to their component.
Step 6: generate a page for every post
Create a dynamic route. The square brackets mean the id segment comes from data:
---
// src/pages/blog/[id].astro
import { getCollection, render } from 'astro:content';
import BaseLayout from '../../layouts/BaseLayout.astro';
export async function getStaticPaths() {
const posts = await getCollection('blog', ({ data }) => import.meta.env.DEV || !data.draft);
return posts.map((post) => ({
params: { id: post.id },
props: { post },
}));
}
const { post } = Astro.props;
const { Content } = await render(post);
const date = post.data.pubDate.toLocaleDateString('en-US', { dateStyle: 'long' });
---
<BaseLayout title={post.data.title} description={post.data.description}>
<article>
<h1>{post.data.title}</h1>
<p><time datetime={post.data.pubDate.toISOString()}>{date}</time></p>
<Content />
</article>
</BaseLayout>
getStaticPaths() runs at build time and returns one entry per post. Astro builds /blog/hello-world/ and /blog/writing-in-markdown/, passing each post in as a prop. Drafts show in npm run dev but are left out of the production build. For a deeper explanation of this function, read getStaticPaths in Astro, explained.
Step 7: build the blog index
List posts newest first:
---
// src/pages/blog/index.astro
import { getCollection } from 'astro:content';
import BaseLayout from '../../layouts/BaseLayout.astro';
const posts = (await getCollection('blog', ({ data }) => !data.draft)).sort(
(a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf(),
);
---
<BaseLayout title="All posts" description="Every post on this blog, newest first.">
<h1>All posts</h1>
<ul>
{posts.map((post) => (
<li>
<a href={`/blog/${post.id}/`}>{post.data.title}</a>
<time datetime={post.data.pubDate.toISOString()}>
{post.data.pubDate.toLocaleDateString('en-US', { dateStyle: 'medium' })}
</time>
</li>
))}
</ul>
</BaseLayout>
Update src/pages/index.astro to use the same layout and show the three latest posts with posts.slice(0, 3). Once you have more than about 20 posts, split the index into pages with the paginate() helper covered in the Astro pagination guide.
Step 8: add an RSS feed
Install the RSS helper and create an endpoint:
npm install @astrojs/rss
// src/pages/rss.xml.ts
import rss from '@astrojs/rss';
import { getCollection } from 'astro:content';
import type { APIContext } from 'astro';
export async function GET(context: APIContext) {
const posts = await getCollection('blog', ({ data }) => !data.draft);
return rss({
title: 'My Blog',
description: 'Notes on building fast websites.',
site: context.site!,
items: posts.map((post) => ({
title: post.data.title,
description: post.data.description,
pubDate: post.data.pubDate,
link: `/blog/${post.id}/`,
})),
});
}
The feed is written to /rss.xml at build time. The <link rel="alternate"> in the layout lets feed readers discover it. The RSS docs cover full-content feeds and styling.
Step 9: add a sitemap
npx astro add sitemap
The command installs @astrojs/sitemap and adds it to astro.config.mjs. After the next build you’ll find sitemap-index.xml in dist/, listing every post and page. Submit it in Google Search Console once the site is live. The sitemap tip covers filtering and lastmod.
Step 10: build and deploy
npm run build
npm run preview
npm run build writes the finished site to dist/. npm run preview serves that folder locally so you can check it before going live.
Push the project to GitHub, then connect the repository to Cloudflare Pages, Netlify or Vercel. Use npm run build as the build command and dist as the output directory. Every push rebuilds the site. The Astro deploy guide has host-by-host instructions, and the official Astro blog tutorial is a good companion if you want more background on each concept.
Where to go from here
You now have a working blog. Common next steps, roughly in order of value:
- Tag pages at
/tags/[tag]/, using the tags already in your schema. - An SEO component with Open Graph tags and JSON-LD.
- Reading time, a table of contents and related posts.
- MDX for posts that need components, via
npx astro add mdx. - Real styling, either with your own CSS or Tailwind CSS.
If you’d rather skip the design work, Astro blog themes give you a finished, styled version of this same structure that you can edit freely.
FAQ
Is Astro good for building a blog?
Yes. Astro was designed for content sites. Posts render to static HTML, Markdown and MDX are supported natively, and content collections validate frontmatter at build time. Pages ship no JavaScript by default, which keeps them fast.
Do I need a database for an Astro blog?
No. Posts are Markdown files in your repository, and the build turns them into HTML. If non-developers need to write posts, you can add a Git-based or headless CMS later without changing the page code.
How do I add a new post to an Astro blog?
Create a new .md file in src/content/blog/ with the required frontmatter fields, then rebuild. If you deploy from Git, pushing the file to your repository triggers the rebuild automatically.
How long does it take to build a blog with Astro?
Following this tutorial, about an hour for a working blog with posts, an index, RSS and a sitemap. Design and extra features like tags and search take longer, which is where a starter template or theme saves time.
Where can I host an Astro blog for free?
Cloudflare Pages, Netlify, Vercel and GitHub Pages all host static Astro sites on free plans. Build with npm run build and point the host at the dist folder.