gatsby-pagination

A collection of pagination tools for Gatsby.

gatsby-pagination

createPaginationPages

Factory method used to create Gatsby's pages, each provided with a subset of nodes and pagination properties, including the paths of the previous (prev) and next posts if they exist. The API divides the provided data nodes into groups which it provides to the Gatsby's createPage API, along with additional pagination properties. This pagination data is accessible within each 'components' via the pathContext parameter.

createPaginationPages(props: Object)
Parameters
props (Object) properties
Name Description
props.createPage function reference to Gatsby's createPage method
props.edges Array<Object> collection of data nodes
props.component string The absolute path to the component used for each page
props.layout string? The name of the layout for this page. By default 'index' layout is used
props.limit number (default 10) the posts per page limit
props.pathFormatter pathFormatter (default DEFAULT_PATH_FORMATTER) the formatter to use when generating page paths
props.context Object? the context data to pass to pageCreate, See Gatsby documentation
Throws
  • Error: Argument createPage must be provided.
  • Error: Argument edges must be provided.
  • Error: Argument component must be provided.
Example

each page created will have pagination properties, see pathContext

// usage inside gatsby-node.js
import {createPaginationPages} from "gatsby-pagination";

// ...
createPaginationPages({
  createPage: createPage,
  edges: result.data.allMarkdownRemark.edges,
  component: path.resolve(`./src/templates/my-sweet-new-page.js`),
  // define number of posts per page with limit, which defaults to 10.
  limit: 5
});

pathFormatter

This callback is provided the current edge and must generate the path and optionally context object for each page.

pathFormatter(path: string): string

Type: Function

Parameters
path (string) The original path to format.
Returns
string: The formatted path.
Example
// ...
createPaginationPages({
  createPage: createPage,
  edges: result.data.allMarkdownRemark.edges,
  component: path.resolve(`./src/templates/my-sweet-new-page.js`),
  // example pathFormatter
  pathFormatter: path => `/blog/${path}`;
});

PaginationPage~pathContext

PaginationPage~pathContext

Type: Object

Properties
nodes (Array<Object>) : the subset of Edge nodes
limit (number) : the maximum number of nodes per page
page (number) : the current page number, base-1
pages (number) : the total number of pages
total (number) : the total number of nodes
prev ((string | undefined)) : the path of previous page if it exists
next ((string | undefined)) : the path of next page if it exists
Example
// usage of properties provided by createPaginationPages inside the component.
import React from "react";

const IndexPage = ({ data, pathContext }) => {
  const { nodes, page, prev, next, pages, total, limit } = pathContext;

  //...
});

prefixPathFormatter

An optional path formatter to utilize with createPaginationPages, enabling users to generate page paths prepend by a prefix.

prefixPathFormatter
Parameters
prefix (string) prefix to prepend path
Returns
function (pageNumber): string: path formatter intended for createPaginationPages
Example
// usage inside gatsby-node.js
import {createPaginationPages, prefixPathFormatter} from "gatsby-pagination";

//...
createPaginationPages({
  createPage: createPage,
  edges: result.data.allMarkdownRemark.edges,
  component: path.resolve(`./src/templates/my-sweet-new-page.js`),
  // path results: /blog, /blog/2, /blog/3, ...etc
  pathFormatter: prefixPathFormatter("/blog")
});

createLinkedPages

Factory method used to create Gatsby's pages, each provided with a single of data nodes and pagination properties, including the paths to the previous (prev) and next post if they exist. The API provides each node to Gatsby's createPage API, along with additional pagination properties, perfect for posts that would like to link to the previous or next post. This pagination data is accessible within each 'components' via it's pathContext parameter.

createLinkedPages(props: Object)
Parameters
props (Object) properties
Name Description
props.createPage function reference to Gatsby's createPage method
props.edges Array<Object> collection of data nodes
props.component string The absolute path to the component used for each page
props.edgeParser edgeParser the edgeParser that handles providing properties handed to createPage per Edge
props.circular boolean? if the pagination should link the first and last post, looping the content.
Throws
  • Error: Argument createPage must be provided.
  • Error: Argument edges must be provided.
  • Error: Argument component must be provided.
  • Error: Argument edgeParser must be provided.
Example

each page created will have pagination properties, see pathContext

// usage inside gatsby-node.js
import {createLinkedPages} from "gatsby-pagination";

//...
createLinkedPages({
  createPage: createPage,
  edges: result.data.allMarkdownRemark.edges,
  component: path.resolve(`./src/templates/my-sweet-pages.js`),
  edgeParser: edge => ({
    path: `/${edge.node.fields.slug}`,
    context: {
      slug: edge.node.fields.slug
    }
  });
});

edgeParser

Parses a given 'edge' and extracts the path and optionally context data to be passed to Gatsby's createPage API.

edgeParser(edge: Object): EdgeParserStruct

Type: Function

Parameters
edge (Object) data node to parse
Returns
EdgeParserStruct: payload intended for createPage API.
Example
//...
createLinkedPages({
  createPage: createPage,
  edges: result.data.allMarkdownRemark.edges,
  component: path.resolve(`./src/templates/my-sweet-pages.js`),
  // example edgeParser
  edgeParser: edge => ({
    path: `/${edge.node.fields.slug}`,
    context: {
      slug: edge.node.fields.slug
    }
  });
});

EdgeParserStruct

new EdgeParserStruct()
Properties
path (string) : the path data to pass to pageCreate
context (Object?) : the context data to pass to pageCreate, See Gatsby documentation
layout (string?) : The name of the layout for this page. By default 'index' layout is used

LinkedPage~pathContext

LinkedPage~pathContext

Type: Object

Properties
total (number) : the total number of nodes/pages
prev ((string | undefined)) : the path of previous page if it exists
next ((string | undefined)) : the path of next page if it exists
Example
// usage of properties provided by createLinkedPages inside the component.
import React from "react";

const IndexPage = ({ data, pathContext }) => {
  const { prev, next, total } = pathContext;

  //...
});