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

@decimoseptimo/gatsby-awesome-pagination

v0.3.8

Published

0.3.8 Fork - Adds support for trailing slashes

Downloads

53

Readme

Awesome Pagination for Gatsby

A sensible approach to pagination for Gatsby sites.

Please post questions on StackOverflow, only bug reports are accepted via GitHub.

Contents

Quick start

Open gatsby-node.js and import:

import { paginate } from 'gatsby-awesome-pagination';

Then, use paginate() like so:

exports.createPages = ({ actions, graphql }) => {
  const { createPage } = actions;

  // Fetch your items (blog posts, categories, etc).
  const blogPosts = doSomeMagic();

  // Create your paginated pages
  paginate({
    createPage, // The Gatsby `createPage` function
    items: blogPosts, // An array of objects
    itemsPerPage: 10, // How many items you want per page
    pathPrefix: '/blog', // Creates pages like `/blog`, `/blog/2`, etc
    component: path.resolve('...'), // Just like `createPage()`
  })
}

Now in your page query you can use the pagination context like so:

export const pageQuery = graphql`
  query ($skip: Int!, $limit: Int!) {
    allMarkdownRemark(
      sort: { fields: [frontmatter___date], order: DESC }
      skip: $skip // This was added by the plugin
      limit: $limit // This was added by the plugin
    ) {
      ...
    }
  }
`

Then inside your component, you can link to next / previous pages, and so on:

const BlogIndex = (props) => {
  return (
    <div>
      {data.allMarkdownRemark.edges.map(edge => <PostItem item={edge.node}/>)}
      <div>
        {/* previousPageLink and nextPageLink were added by the plugin */ }
        <Link to={props.pageContext.previousPagePath}>Previous</Link>
        <Link to={props.pageContext.nextPagePath}>Next</Link>
      </div>
    </div>
  )
}

For a more detailed example, see docs/examples.md

Introduction

Love Gatsby, wanna paginate. Sweet, that's exactly what this package is for.

We differ from other pagination options as follows:

  • Don't abuse context to pass data into components
  • Pass only pagination context via context
  • Provide helpers for next / previous links

There are 2 types of pagination. You have 80 blog posts and you want to show them 15 at a time on pages like /blog, /blog/2, /blog/3, etc. You do this with paginate(). Then on each blog post, you want to link to the previous and next blog posts. You do this with createPagePerItem().

Philosophy

Why did we create this plugin? We felt that the other Gatsby pagination plugins were using an approach that goes against the principles of GraphQL. One of the advantages of GraphQL is to be able to decide what data you need right where you use that data. That's how Gatsby works with page queries.

By putting all the data into context, the other pagination plugins break this. Now you need to decide what data you require for each page inside gatsby-node.js and not inside your page query.

We also felt that there were some helpers missing. Generating links to the next and previous pages.

This plugin aims to make it easy to paginate in Gatsby properly. No compromises.

API

Both paginate() and createPagePerItem() take a single argument, an object. They share the following keys (* = required):

  • createPage* - The createPage function from exports.createPages
  • component* - The value you would pass to createPage() as component Gatsby docs here
  • items* - An array of objects, the items you want to paginate over

paginate()

In addition to the arguments above, paginate() also supports:

  • itemsPerPage* - An integer, how many items should be displayed on each page
  • itemsPerFirstPage - An integer, how many items should be displayed on the first page
  • pathPrefix* - A (nonempty) string or string returning function, the path (eg /blog) to which /2, /3, etc will be added
  • context - A base context object which is extended with the pagination context values
  • trailingSlash - A boolean, indicating the setting of canonical URLs with trailing slashes

Example:

paginate({
  createPage: boundActionCreators.createPage,
  component: path.resolve('./src/templates/blog-index.js'),
  items: blogPosts,
  itemsPerPage: 15,
  itemsPerFirstPage: 3,
  pathPrefix: '/blog',
  trailingSlash: true
})

Each page's context automatically receives the following values:

  • pageNumber - The page number (starting from 0)
  • humanPageNumber - The page number (starting from 1) for human consumption
  • skip - The $skip you can use in a GraphQL query
  • limit - The $limit you can use in a GraphQL query
  • numberOfPages - The total number of pages
  • previousPagePath - The path to the previous page or undefined
  • nextPagePath - The path to the next page or undefined

pathPrefix()

For more advanced use cases, you can supply a function to pathPrefix. This function will receive a single object as its only argument, that object will contain pageNumber and numberOfPages, both integers.

A simple example implementation could be:

const pathPrefix = ({ pageNumber, numberOfPages }) =>
  pageNumber === 0 ? '/blog' : '/blog/page'

This example produces pages like /blog, /blog/page/2, /blog/page/3, etc.

createPagePerItem()

WARNING: This API is under active development and will probably change. USE WITH CAUTION.

In addition to the arguments above, createPagePerItem() also accepts:

  • itemToPath* - A function that takes one object from items and returns the path for this item
  • itemToId* - A function that takes one object from items and returns the item's ID

NOTE: Both itemToPath and itemToId also accept a string with the path to the value, for example node.frontmatter.permalink or node.id.

NOTE: If an individual item has a property called context, and that property is an object, then it's own properties will be added to the page's context for that item.

Example:

createPagePerItem({
  createPage: boundActionCreators.createPage,
  component: path.resolve('./src/templates/blog-post.js'),
  items: blogPosts,
  itemToPath: 'node.frontmatter.permalink',
  itemToId: 'node.id'
})

Each page's context automatically receives the following values:

  • previousPagePath - The path to the previous page or undefined
  • previousItem - A copy of the previous element from items
  • previousPageId - The ID of the previous page
  • nextPagePath - The path to the next page or undefined
  • nextItem - A copy of the next element from items
  • nextPageId - The ID of the next page

Notes

Flow

This plugin is written using flow. There are some limitations when using flow and lodash. Specifically this issue. In many cases we use $FlowExpectError and explicitly define the type of something to workaround. A more elegant solution does not currently seem to exist. Any input on improving the typing is greatly appreciated in the plugin's issues.

Contributors

Thanks to the following for their contributions:

  • https://github.com/Pyrax
  • https://github.com/JesseSingleton
  • https://github.com/silvenon