Data & content · Lesson 12 of 22 · 15 min
Build the blog list page
Query the collection, sort and filter posts, and show them with a PostCard component.
Time to show the posts. You’ll build a PostCard component, a /blog page that lists every post, and a “latest posts” section on the homepage.
1. The PostCard component
src/components/PostCard.astro
---
import type { CollectionEntry } from 'astro:content';
interface Props {
post: CollectionEntry<'blog'>;
}
const { post } = Astro.props;
const { title, description, pubDate } = post.data;
---
<article>
<a href={`/blog/${post.id}`}>
<h3>{title}</h3>
</a>
<time datetime={pubDate.toISOString()}>
{pubDate.toLocaleDateString('en-US', { dateStyle: 'medium', timeZone: 'UTC' })}
</time>
<p>{description}</p>
</article>
<style>
article {
padding: 1.25rem 0;
border-bottom: 1px solid #eadfd2;
}
h3 {
margin: 0;
}
a {
text-decoration: none;
}
time {
color: var(--color-muted);
font-size: 0.9rem;
}
</style>
What a post looks like
Each entry from a collection has:
post.id: the file name without.md, likefall-menupost.data: the validated frontmatter (title,pubDate, and so on)
CollectionEntry<'blog'> is the type for one blog post. It gives you autocomplete on post.data.
2. The /blog page
Create a blog folder inside pages, then add index.astro. A file named index takes the folder’s URL, so this page lives at /blog.
src/pages/blog/index.astro
---
import BaseLayout from '../../layouts/BaseLayout.astro';
import PostCard from '../../components/PostCard.astro';
import { getCollection } from 'astro:content';
const posts = (await getCollection('blog', ({ data }) => !data.draft)).sort(
(a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf()
);
---
<BaseLayout title="Blog" description="Recipes, menus, and kitchen notes.">
<div class="container">
<h1>Blog</h1>
{posts.map((post) => <PostCard post={post} />)}
</div>
</BaseLayout>
<style>
h1 {
margin-top: 2rem;
}
</style>
Two things are happening in that one statement:
- Filter. The second argument to
getCollectionis a filter.({ data }) => !data.draftkeeps only posts that aren’t drafts. - Sort.
.sort()puts the newest first.valueOf()turns a date into a number so you can subtract.
Note the paths now start with ../../ because this file is one folder deeper.
3. Latest posts on the homepage
Add the two newest posts under Services:
src/pages/index.astro
---
import BaseLayout from '../layouts/BaseLayout.astro';
import Section from '../components/Section.astro';
import Card from '../components/Card.astro';
import PostCard from '../components/PostCard.astro';
import services from '../data/services.json';
import { getCollection } from 'astro:content';
const posts = (await getCollection('blog', ({ data }) => !data.draft))
.sort((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf())
.slice(0, 2);
---
<BaseLayout title="Home">
<!-- hero and Services stay the same -->
<Section id="latest" title="From the blog">
{posts.map((post) => <PostCard post={post} />)}
<p><a href="/blog">All posts →</a></p>
</Section>
</BaseLayout>
.slice(0, 2) keeps the first two.