Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Practical Astro
Lesson Plan
  1. Content Systems
    1. Introduction
    2. An import alias
    3. Using Markdown
    4. Markdown extended
    5. Markdown component
    6. Markdown component in frameworks
    7. Content collections made easy
    8. Basic blogs
    9. Blogs made easy
    10. Prev/Next navigation
    11. Pagination made easy
    12. Better tag pages
    13. Paginated tag pages
    14. Markdown layouts
    15. Partials
    16. JS over JSON Data
    17. Dynamic 404 pages
    18. Putting everything together
    19. Other useful systems
  2. Image Systems
    1. Introduction
    2. Images are complicated because of this
    3. Astro's Image component
    4. Astro's Picture component
    5. Improvements over Astro's Image and Picture Components
    6. Images in Markdown and MDX
    7. Images in framework components
    8. Background images
    9. Art Direction with Astro
    10. Videos without layout shifts
    11. GIF Replacements and background video
    12. SVGs and icons
    13. SVG backgrounds
    14. Adding noise to images
    15. Asset caching on GitHub Actions
    16. Putting everything together
    17. Other useful systems

Prev/Next navigation

2m:9s

Blogs usually have a previous and a next navigation on an article page. This allows the reader to read more if they are interested in the content.

There is a very simple way to populate this previous and next navigation, and I’m going to show you how. (This method works for other collections too).

Gathering previous and next posts

We already have a list of the published articles in getStaticPaths. So we just need to let the current page know:

  1. What’s the current post
  2. What’s the previous post
  3. What’s the next post

We can do that easily with indexes!

src/pages/blog/[slug].astro
---
export async function getStaticPaths() {
const files = await getCollection('blog')
const posts = await processFiles(files)
return posts.map((post, index) => {
const prev = posts[index + 1]
const next = posts[index - 1]
// ...
})
}
---

In this case:

  • prev is the next item on the list because we have sorted the posts in reverse order. Hence [index + 1]
  • next is the previous item on the list, hence [index - 1].

Then we pass the relevant posts into the props:

src/pages/blog/[slug].astro
---
export async function getStaticPaths() {
// ...
return posts.map((post, index) => {
// ...
return {
params: { slug: post.data.slug },
props: {
posts: {
current: post,
prev,
next,
},
},
}
})
}
---

We can now get the previous and next article from Astro.props.

src/pages/blog/[slug].astro
---
export async function getStaticPaths() { /* ... */ }
const { posts } = Astro.props
const { current: post, prev, next } = posts
---

The next step is to detect whether the previous post or next post exists, and output the markup accordingly. For this, I prefer to put the logic in a PrevNextNav component.

We’re also going to pass the directory (blog in this case) into the component so the component can use this base to create the right link.

src/pages/blog/[slug].astro
---
// ...
const { current: post, prev, next } = posts
---
<PrevNextNav base="blog" {prev} {next}/>

The previous/next component

What we do in this component is really easy. We just need to check if prev or next exists and output markup accordingly.

src/components/PrevNextNav.astro
---
import { If } from '@splendidlabz/astro'
const { base = 'blog', prev, next } = Astro.props
---
<div class="prev-next-nav">
<If condition={prev}>
<a href={`/${base}/${prev?.data.slug}`}>
<span> Prev: </span>
<span>{prev?.data.title}</span>
</a>
</If>
<If condition={next}>
<a href={`/${base}/${next?.data.slug}`}>
<span> Next: </span>
<span>{next?.data.title}</span>
</a>
</If>
</div>

That’s it! The rest are just styles.