Gatsby-paginate
This library provides a simple API for paginating an array of posts/pages for your blog/site homepage in Gatsby js.
LOOKING FOR MAINTAINERS - please email rob@pixelstew.co.uk with subject GATSBY_PAGINATE
Installation
yarn add gatsby-paginate
Usage
- Require the package in your
gatsby-node.js
file. - Add a call to createPaginatedPages in
gatsby-node.js
.
Then add the following to the top of your gatsby-node.js
file.
const createPaginatedPages =
Use case 1 - paginate list of posts on home page
To create a paginated index of your blog posts, you need to do four things:
- Remove the
index.js
file from the pages directory. - Create an
index.js
file in the templates directory and refer to it in the createPaginatedPages call
Call createPaginatedPages
You probably already have something like this in your gatsby-node.js
file to generate the pages for your blog:
exports { return { }}
Just insert a call to createPaginatedPages
before (or after) the createPage function:
exports { return { }}
Notice that createPaginatedPages
is being passed an options object.
edges
is the array of nodes that comes from the GraphQL query.createPage
is simply thecreatePage
function you get fromactions
.pageTemplate
is a template to use for the index page. AndpageLength
is an optional parameter that defines how many posts to show per index page. It defaults to 10.pathPrefix
is an optional parameter for passing the name of a path to add to the path generated in thecreatePage
func. This is used in use case 2 below.context
is an optional parameter which is used as thecontext
property whencreatePage
is called.
createPaginatedPages
will then call createPage
to create an index page for each of the groups of pages. The content that describes the blogs (title, slug, etc) that will go in each page will be passed to the template through props.pageContext
so you need to make sure that everything that you want on the index page regarding the blogs should be requested in the GraphQL query in gatsby-node.js
.
Use case 2 - paginate a post or use pagination on a page other than index
Call createPaginatedPages in the same way as above but...
This time pass in a pathPrefix
Then...
- Create a template in tha same way as above but this time
- Add a
pathPrefix
- (optional) add
buildPath
if you want to have more control over the pagination URL structure
In this instance a new set of pages will be created at the following path your_site/your_page_name
Then a second paginated page of your_site/your_page_name/2
PLEASE NOTE: THE PATH PREFIX FUNCTIONALITY IS UNDER DEVELOPMENT AND MORE FLEXIBILITY WILL BE ADDED SOON
Create the template
This is a simple template which might be used in use case 1 above to replace the index of a blog with a paginated list of posts.
The pageContext
object which contains the following 5 keys is passed to the template;
group
- (arr) an array containing the number of edges/nodes specified in thepageLength
option.index
- (int) this is the index of the edge/node.first
- (bool) Soon to be deprecated - please calculate first using index and pageCount - is this the first page?last
- (bool) Soon to be deprecated - please calculate last using index and pageCount - is this the last page?pageCount
- (int) the total number of pages being paginated throughadditionalContext
- (obj) optional additional context
import React Component from 'react'import Link from 'gatsby-link' const NavLink = { if !propstest return <Link =>propstext</Link> else return <span>propstext</span> } const IndexPage = pageContext const group index first last pageCount = pageContext const previousUrl = index - 1 == 1 ? '/' : index - 1 const nextUrl = index + 1 return <div> <h4>pageCount Pages</h4> group <div ="previousLink"> <NavLink = = ="Go to Previous Page" /> </div> <div ="nextLink"> <NavLink = = ="Go to Next Page" /> </div> </div>