gatsby-pagination
This simple utility is intended to help authors add pagination to their GatsbyJS projects. The library works by dividing the provided data set into smaller subsets, then calling Gatsby's createPage method for each subset. In addition to the subset data itself, each page also receives helpful pagination variables, modeled after Ghost's pagination data.
Installation
npm install --save gatsby-pagination
Usage
Overview
To create pages with pagination, gatsby-pagination should be required in
gatsby-node.js
. Instead of calling Gatsby's createPage directly, call
createPaginationPages. The method requires a reference to createPage, your
data set, as well as a component.
The name of the component is critical to the location of the intended page, see Gatsby's createPage documentation to learn more about components.
Starting from the root page, each additional page included it's page number in the route. e.g. /blog
for page 1, /blog/2
for page 2, etc.
Recommended reference: Creating pages in gatsby-node.js
Require the package
In gatsby-node.js
require or import the createPaginationPages factory method from gatsby-pagination.
const createPaginationPages = ;
Call createPaginatePages
In export.createPages get a reference to Gatsby's createPage method from access to boundActionCreators. Provide createPaginatePages with createPage, the data set of nodes, and the component to generate the pages with pagination. Additionally, createPaginationPages takes optional limit parameters for setting the maximum number of node per subset and pathFormatter which can be use to creating custom paths.
See the createPaginatePages documentation for more details.
exports { const createPage = boundActionCreators; return { const indexPage = path; const postPage = path; ); };};
Create the index.js component
Create an index.js
templates and make sure to remove any other pages
which could collide with the '/index' path. Gatsby-pagination adds extra
pagination properties to the page's pathContext, such as prev and next.
See the pathContext documentation for more details.
;; const IndexPage = { const nodes page prev next pages total limit = pathContext; const PaginationLink = { if propsto && propstext return <Link to=propsto text=propstext />; return null; }; return <div> nodes <div className="previousPost"> <PaginationLink to=prev text="Go to Previous Page" /> </div> <div className="nextPost"> <PaginationLink to=next text="Go to Next Page" /> </div> </div> ;}; ; ; const query = graphql`add GraphQL query`;
License
MIT