To add next and previous post links in Astro, sort your posts by date inside getStaticPaths(), and for each post pass its neighbours as props: prev: posts[i + 1] (older) and next: posts[i - 1] (newer). The post page then renders two links. Because it all happens at build time, there is no extra query and no client JavaScript.
Key Takeaways
- Sort once in
getStaticPaths()and use the array index to find neighbours.- Pass only what the link needs (
idandtitle), not whole entries.- Decide which direction “next” means, and label the links so readers can tell.
- Wrap the links in a
<nav>with anaria-label.- Use the same filtered list as your index so drafts never appear as neighbours.
Pass neighbours from getStaticPaths()
---
// src/pages/blog/[...slug].astro
import { getCollection, render } from 'astro:content';
import BaseLayout from '../../layouts/BaseLayout.astro';
import PostNav from '../../components/PostNav.astro';
export async function getStaticPaths() {
const posts = (await getCollection('blog', ({ data }) => !data.draft)).sort(
(a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf(),
);
const link = (post?: (typeof posts)[number]) =>
post ? { href: `/blog/${post.id}/`, title: post.data.title } : undefined;
return posts.map((post, i) => ({
params: { slug: post.id },
props: {
post,
newer: link(posts[i - 1]),
older: link(posts[i + 1]),
},
}));
}
const { post, newer, older } = Astro.props;
const { Content } = await render(post);
---
<BaseLayout title={post.data.title}>
<article>
<h1>{post.data.title}</h1>
<Content />
</article>
<PostNav newer={newer} older={older} />
</BaseLayout>
The list is sorted newest first, so posts[i - 1] is the newer post and posts[i + 1] the older one. At either end of the list the neighbour is undefined, and the component skips that link. Naming the props newer and older avoids the classic confusion over whether “next” means forward in time or further down the list.
Passing a small { href, title } object instead of the whole entry keeps the data serialized for each page small. That matters little for 20 posts and more for 2,000.
Render the links
---
// src/components/PostNav.astro
interface Link { href: string; title: string }
interface Props { newer?: Link; older?: Link }
const { newer, older } = Astro.props;
---
{(newer || older) && (
<nav class="post-nav" aria-label="More posts">
{older && (
<a href={older.href} rel="prev">
<span class="label">Older post</span>
<span class="title">{older.title}</span>
</a>
)}
{newer && (
<a href={newer.href} rel="next" class="newer">
<span class="label">Newer post</span>
<span class="title">{newer.title}</span>
</a>
)}
</nav>
)}
<style>
.post-nav {
display: flex;
justify-content: space-between;
gap: 1.5rem;
margin-top: 4rem;
border-top: 1px solid currentColor;
padding-top: 1.5rem;
}
.post-nav a { display: grid; max-width: 45%; text-decoration: none; }
.post-nav .newer { margin-left: auto; text-align: right; }
.label { font-size: 0.875rem; opacity: 0.7; }
.title { font-weight: 600; }
</style>
The visible “Older post” and “Newer post” labels tell everyone which way each link goes; rel="prev" and rel="next" describe the same relationship in markup. Wrapping the pair in <nav aria-label="More posts"> gives screen reader users a named landmark separate from the main menu.
Keep the order consistent
The neighbours are only as good as the sort. Use the same filter and sort as your blog index, ideally from one helper in src/lib/posts.ts; sorting posts by date shows one with a tiebreaker so same-day posts never swap places. If you use drafts or scheduled posts, apply those filters here too, or a hidden post will show up as a neighbour and link to a page that was never built. See draft posts.
Neighbours within a category or series
To link within a tag or series instead of the whole blog, filter the sorted list for each post before finding its index:
// inside getStaticPaths()
return posts.map((post) => {
const siblings = posts.filter((p) => p.data.category === post.data.category);
const i = siblings.findIndex((p) => p.id === post.id);
return {
params: { slug: post.id },
props: { post, newer: link(siblings[i - 1]), older: link(siblings[i + 1]) },
};
});
For ordered tutorials, multi-part post series uses part numbers instead of dates. Paginated index pages use a different mechanism: the Astro pagination guide covers paginate() and its page.url.prev and page.url.next. Our content collections docs and the official routing reference cover getStaticPaths() props, and Blog & Editorial themes include post navigation like this out of the box.
FAQ
How do I get the next post in Astro?
Sort the collection in getStaticPaths(), find each post’s index, and pass the entry at the neighbouring index as a prop. The post page reads it from Astro.props.
Should “next post” mean newer or older?
Either works if the label says which. Blogs usually show “Older” on the left and “Newer” on the right, matching a timeline that reads left to right.
Why does my Astro next post link go to a 404?
The neighbour came from an unfiltered list that includes a draft or scheduled post, so its page was never built. Use the same filtered list everywhere.
Do previous and next links help SEO?
They add internal links between related posts, which helps crawlers find older content. Google no longer uses rel="prev" and rel="next" as an indexing signal, so their value is for readers and markup clarity.