npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gatsby-pagination

v1.2.0

Published

Gatsby utility that generates pages with pagination

Downloads

314

Readme

gatsby-pagination

NPM version Travis build status Coverage Status Conventional Commits

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 } = require("gatsby-pagination");

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.createPages = ({ graphql, boundActionCreators }) => {
  const { createPage } = boundActionCreators;

  return new Promise((resolve, reject) => {
    const indexPage = path.resolve("src/components/index.jsx");
    const postPage = path.resolve("src/components/post.jsx");
    resolve(
      graphql(
        `add GraphQL query`
      ).then(result => {
        if (result.errors) {
          reject(result.errors);
        }

        createPaginationPages({
          createPage: createPage,
          edges: result.data.allMarkdownRemark.edges,
          component: indexPage,
          limit: 5
        });

          createPage({
            path: edge.node.id,
            component: postPage,
            context: {
              slug: edge.node.fields.slug
            }
          });
        });
      })
    );
  });
};

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.

import React from "react";
import Link from "gatsby-link";

const IndexPage = ({ data, pathContext }) => {
  const { nodes, page, prev, next, pages, total, limit } = pathContext;
  const PaginationLink = props => {
    if (props.to && props.text) {
      return <Link to={props.to} text={props.text} />;
    }
    return null;
  };

  return (
    <div>
      {nodes.map(({ node }) => (
        <div className="postList" key={node.id}>
          <div className="postDate">{node.frontmatter.date}</div>
          <Link className="postUrl" to={node.fields.slug}>
            {node.frontmatter.title}
          </Link>
          <div> className="postExcerpt" {node.excerpt}</div>
        </div>
      ))}
      <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>
  );
};

import Index from "../components/index";

export default IndexPage;

export const query = graphql`add GraphQL query`;

License

MIT