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

Pagination made easy

2m:23s

Astro has a paginate function to create pages. To make this work in a flawless manner, we have to define our file structure like this.

Terminal window
src/pages/blog/
├── [slug].astro # Creates individual posts
└── [...page].astro # Creates pages, like /blog/, /blog/2/, etc.

This structure is important because:

  • [slug] will be matched first, so we don’t lose any posts.
  • [...page] will catch other URLs for page generation.

In case you’re wondering, 404 pages will still work because slugs and pages are generated during build time.

Using Paginate

Astro’s paginate function is deceptively simple. You just need to grab it as an argument from getStaticPaths, and use it directly.

src/pages/blog/[...page].astro
---
import { processFiles } from '@splendidlabz/astro/content'
import { getCollection } from 'astro:content'
export const prerender = true // optional if output is static
export async function getStaticPaths({ paginate }) {
const files = await getCollection('blog')
const posts = await processFiles(files)
return paginate(posts, { pageSize: 10 })
}
const { page } = Astro.props
---

pageSize here refers to the number of items to be included in a single page.

And the page object contains many variables. The ones that are most relevant to us are:

  1. data — the items for this page
  2. currentPage — the current page number
  3. lastPage — the last page number (or total number of pages)
  4. page.url.* — links to the first, prev, next, and last pages

Populating items on the page

You can use data to populate items for the page:

src/blog/[...page].astro
---
// ...
const { page } = Astro.props
---
{
page.data.map(post => (
<article>
<h1>
<a href={`/blog/${post.data.slug}`}>{post.data.title}</a>
</h1>
<Markdown> {post.data.excerpt} </Markdown>
</article>
))
}

The Pagination UI

Creating a pagination UI is the hard part, because there’s really a lot of code.

But I’ve chosen to simplify it for us by building a Pagination component.

All you have to do: set the base directory, pass in the page variable, and choose whether you want a simple or full pagination.

---
import { Pagination } from '@splendidlabz/astro'
// ...
const { page } = Astro.props
---
{/* Choose between simple or full */}
<Pagination base="blog" {page} type="full" />

Simple Pagination

Contains only the previous and next page links

Full Pagination

Contains everything you expect from a standard page-based pagination component

These are relatively unstyled at the moment so feel free to add your styles! You can find more information about the Pagination component in the docs.