IoT-Arduino
10/16/2019 - 11:59 PM

gatsby-node.js--catSection

create category archive pagination code in gatsby-node.js

  const { allContentfulCategory, allContentfulBlogPost } = res.data

  // create archive pages for each category
  allContentfulCategory.edges.forEach(catEdge => {
    // filter out the posts that belongs tot the current category
    const filteredPosts = allContentfulBlogPost.edges.filter(
      el => el.node.catetory.catSlug === catEdge.node.catSlug
    )

    if (filteredPosts.length > 0) {
      paginate({
        createPage,
        items: filteredPosts,
        itemsPerPage: 3,
        component: postListCatTemplate,
        pathPrefix: `/blog/${catEdge.node.catSlug}`,
        context: {
          catId: catEdge.node.id,
          catName: catEdge.node.Title,
          catSlug: catEdge.node.catSlug,
          catCount: catEdge.node.count,
          categories: allContentfulCategory.edges,
        },
      })
    }
  })