Skip to content

Implementing Pagination or Lazy Loading for Multiple Blog Posts in ScrewFast #215

Answered by mearashadowfax
Najib90 asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @Najib90! Astro supports built-in pagination for large collections of data that need to be split into multiple pages, so you can implement custom pagination logic with something similar to the following:

const currentPage: number | null = +Astro.url.searchParams.get('page')! || 1;

const allRecipes: CollectionEntry<'recipes'>[] = (await getCollection('recipes'))
  .sort((a:CollectionEntry<'recipes'>, b:CollectionEntry<'recipes'>) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf());

const totalPages: number = Math.ceil(allRecipes.length / RECIPES_PER_PAGE);

const recipesPerPage: CollectionEntry<'recipes'>[] = allRecipes.slice(
  (currentPage - 1) * RECIPES_PER_PAGE, currentPage *…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by mearashadowfax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants