A collection of pagination tools for Gatsby.
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.
(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 |
createPage
must be provided.
edges
must be provided.
component
must be provided.
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
});
This callback is provided the current edge and must generate the path
and optionally context
object for each page.
Type: Function
(string)
The original path to format.
string
:
The formatted path.
// ...
createPaginationPages({
createPage: createPage,
edges: result.data.allMarkdownRemark.edges,
component: path.resolve(`./src/templates/my-sweet-new-page.js`),
// example pathFormatter
pathFormatter: path => `/blog/${path}`;
});
Type: Object
(number)
: the maximum number of nodes per page
(number)
: the current page number, base-1
(number)
: the total number of pages
(number)
: the total number of nodes
// 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;
//...
});
An optional path formatter to utilize with createPaginationPages, enabling users to generate page paths prepend by a prefix.
(string)
prefix to prepend path
function (pageNumber): string
:
path formatter intended for createPaginationPages
// 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")
});
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.
(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. |
createPage
must be provided.
edges
must be provided.
component
must be provided.
edgeParser
must be provided.
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
}
});
});
Parses a given 'edge' and extracts the path
and optionally context
data to be passed to Gatsby's createPage API.
Type: Function
(Object)
data node to parse
EdgeParserStruct
:
payload intended for createPage API.
//...
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
}
});
});
(string)
: the path data to pass to pageCreate
Type: Object
(number)
: the total number of nodes/pages
// usage of properties provided by createLinkedPages inside the component.
import React from "react";
const IndexPage = ({ data, pathContext }) => {
const { prev, next, total } = pathContext;
//...
});