Astro Pagination for SSG pages with type support

2024-08-04 14:04

So after fiddling around and checking Astro’s discord I got an implementation working to use the paginate helper function within getStaticPaths with type support.

To use Astro’s pagination helpers the template file needs to be either:

  • [page].astro or [...page].astro
  • The template has to set export const prerender = true
  • For type support the GetStaticPaths type has to be imported and implemented via the satisifies directive
---
import type { GetStaticPaths } from "astro";
export const prerender = true;

export const getStaticPaths = (async ({ paginate }) => {
  const posts = await getCollection('posts');
  return paginate(posts, { pageSize: 10 });

}) satisfies GetStaticPaths;
---

Relevant Astro Documentation

Astro Pagination in SSR mode

When rendering pages entirely on the server the paginate function will not be available and you will have to implement your own pagination functions.