@@ -566,6 +566,7 @@ export const createPages: GatsbyNode["createPages"] = async ({
566
566
tagsGroup : {
567
567
group : Array < {
568
568
fieldValue : string
569
+ totalCount : number
569
570
} >
570
571
}
571
572
} > ( `
@@ -590,6 +591,7 @@ export const createPages: GatsbyNode["createPages"] = async ({
590
591
tagsGroup: allMdx(limit: 2000) {
591
592
group(field: { frontmatter: { tags: SELECT } }) {
592
593
fieldValue
594
+ totalCount
593
595
}
594
596
}
595
597
}
@@ -626,12 +628,33 @@ export const createPages: GatsbyNode["createPages"] = async ({
626
628
627
629
// Make tag pages
628
630
tags . forEach ( tag => {
629
- createPage ( {
630
- path : `/news/tags/${ tag . fieldValue } /` ,
631
- component : tagTemplate ,
632
- context : {
633
- tag : tag . fieldValue ,
634
- } ,
631
+ // Calculate how many pages we need for this tag.
632
+ const numTagPages = Math . ceil ( tag . totalCount / postsPerPage )
633
+
634
+ Array . from ( { length : numTagPages } ) . forEach ( ( _ , index ) => {
635
+ const currentPageNumber = index + 1
636
+ const previousPageNumber =
637
+ currentPageNumber === 1 ? null : currentPageNumber - 1
638
+ const nextPageNumber =
639
+ currentPageNumber === numTagPages ? null : currentPageNumber + 1
640
+
641
+ createPage ( {
642
+ // Use a friendly URL for the first page; then add /page/2, etc.
643
+ path :
644
+ index === 0
645
+ ? `/news/tags/${ tag . fieldValue } /`
646
+ : `/news/tags/${ tag . fieldValue } /page/${ index + 1 } ` ,
647
+ component : tagTemplate ,
648
+ context : {
649
+ tag : tag . fieldValue ,
650
+ limit : postsPerPage ,
651
+ skip : index * postsPerPage ,
652
+ numTagPages,
653
+ currentPageNumber,
654
+ previousPageNumber,
655
+ nextPageNumber,
656
+ } ,
657
+ } )
635
658
} )
636
659
} )
637
660
0 commit comments