To build an archive page in Astro, fetch your posts with getCollection(), sort them newest first, group them by pubDate.getUTCFullYear() into a Map, and render one heading and list per year in src/pages/blog/archive.astro. For a page per year, generate /blog/2026/-style routes from the same groups with getStaticPaths().
Key Takeaways
- Sort first, then group; a
Mapkeeps the years in the order you inserted them.- Use
getUTCFullYear()so a post dated January 1 does not land in the previous year.- One archive page with year headings suits most blogs.
- Year pages are a
[year].astroroute built from the same grouping.- Reuse the filtered post list so drafts never appear in the archive.
Group posts by year
// src/lib/archive.ts
import { getCollection, type CollectionEntry } from 'astro:content';
type Post = CollectionEntry<'blog'>;
export async function getPostsByYear() {
const posts = (await getCollection('blog', ({ data }) => !data.draft)).sort(
(a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf(),
);
const years = new Map<number, Post[]>();
for (const post of posts) {
const year = post.data.pubDate.getUTCFullYear();
if (!years.has(year)) years.set(year, []);
years.get(year)!.push(post);
}
return years;
}
Because the posts are sorted before grouping, the Map holds years newest first and each yearβs posts are already in order. pubDate must be a Date (use z.coerce.date() in the schema). Dates without a time are parsed as midnight UTC, so getUTCFullYear() gives the year you wrote; getFullYear() can shift January 1 posts into the previous year when your build machine runs in a time zone behind UTC.
Render the archive page
---
// src/pages/blog/archive.astro
import BaseLayout from '../../layouts/BaseLayout.astro';
import { getPostsByYear } from '../../lib/archive';
const years = await getPostsByYear();
const total = [...years.values()].reduce((sum, posts) => sum + posts.length, 0);
---
<BaseLayout title="Archive" description={`All ${total} posts, by year.`}>
<h1>Archive</h1>
<nav aria-label="Years">
{[...years.keys()].map((year) => <a href={`#y${year}`}>{year}</a>)}
</nav>
{[...years].map(([year, posts]) => (
<section id={`y${year}`} aria-labelledby={`h${year}`}>
<h2 id={`h${year}`}>{year} <small>({posts.length})</small></h2>
<ul>
{posts.map((post) => (
<li>
<time datetime={post.data.pubDate.toISOString()}>
{post.data.pubDate.toLocaleDateString('en-US', { month: 'short', day: 'numeric', timeZone: 'UTC' })}
</time>
<a href={`/blog/${post.id}/`}>{post.data.title}</a>
</li>
))}
</ul>
</section>
))}
</BaseLayout>
The jump links at the top help on long archives. IDs start with a letter (y2026) because an ID that begins with a digit is awkward to target in CSS. Passing timeZone: 'UTC' to the date formatter keeps the displayed day consistent with the year grouping.
One page per year
If you have many posts per year, or want URLs like /blog/2025/, generate a route per year:
---
// src/pages/blog/[year].astro
import BaseLayout from '../../layouts/BaseLayout.astro';
import { getPostsByYear } from '../../lib/archive';
export async function getStaticPaths() {
const years = await getPostsByYear();
return [...years].map(([year, posts]) => ({
params: { year: String(year) },
props: { posts },
}));
}
const { year } = Astro.params;
const { posts } = Astro.props;
---
<BaseLayout title={`Posts from ${year}`}>
<h1>Posts from {year}</h1>
<ul>
{posts.map((post) => <li><a href={`/blog/${post.id}/`}>{post.data.title}</a></li>)}
</ul>
</BaseLayout>
Watch for collisions: src/pages/blog/[year].astro and a post route at src/pages/blog/[...slug].astro can both match /blog/2025/. Astro prefers the more specific single-segment route, but a post whose ID is 2025 would then be unreachable. Putting year pages under /blog/year/[year]/ avoids the question entirely. The routing guide explains route priority.
Group by month too
For busy blogs, nest months inside each year with a key like 2026-11:
const key = post.data.pubDate.toISOString().slice(0, 7); // "2026-11"
Group with the same Map pattern, then format the heading with toLocaleDateString('en-US', { month: 'long', year: 'numeric', timeZone: 'UTC' }).
Link the archive from the blog index and the footer, so readers and crawlers can reach every old post in two clicks. It pairs well with tag and category pages and depends on the sort order in sorting posts by date. The content collections docs cover the collection setup, and themes in our Blog & Editorial category include archive layouts you can adapt.
FAQ
How do I create an archive page in Astro?
Create src/pages/blog/archive.astro, fetch and sort your posts, group them by year, and render a heading and list for each group. It is generated at build time like any other page.
How do I group Astro posts by year?
Loop over the sorted posts and push each one into a Map keyed by post.data.pubDate.getUTCFullYear(). The Map keeps insertion order, so the years stay sorted.
Why is a post in the wrong year in my Astro archive?
The date was read in local time. Use getUTCFullYear() for grouping and pass timeZone: 'UTC' when formatting, because dates without a time are parsed as UTC midnight.
Should an archive page be paginated?
Usually not. A plain list of titles and dates stays light even with hundreds of posts, and a single page is easier to scan and crawl than many short ones.