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-prismic-pagination

v0.2.1

Published

A little help to get your gatsby-source-prismic-graphql based project paginating

Downloads

20

Readme

gatsby-prismic-pagination

A little help to get your prismic.io based Gatsby project paginating.

Pre-requisites

  • This plugin assumes you are using gatsby-source-prismic-graphql as your Gatsby source

Example usage

Create the paginated index pages:

In gatsby-node.js:

const { prismicPagination } = require('gatsby-prismic-pagination')

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

  await prismicPagination({
    graphql,
    createPage,
    component: path.resolve(`./src/components/PrismicIndex.tsx`), // Just like for createPage
    pathPrefix: '/blog', // Generated as e.g. /blog then /blog/2 etc.
    postsPerPage: 15, // Also the amount per call to server
    prismicConnectionName: 'allBlog_posts',
    prismicConnectionArgs: {
      sortBy: 'date_DESC',
    },
    nodeFields: ['title'], // You might want to use from destructured return later...
    nodeMeta: ['tags']
  })
}

Get all posts at once

The plugin will keep querying prismic until it has all your posts. This saves you writing your own loop for prismic's 20 post limit per query:

const { allPosts } = await prismicPagination(...yourArgsLikeBefore) // Still creates index pages

// We now have all posts in a single array rather than in prismic paged responses
// so it's easier to construct individual gatsby pages
allPosts.forEach(x => {
  let titleText = x.node.title[0].text
  const slug = yourFnToCreatePath(titleText) // Or maybe you want to use a UID?
  createPage({
    path: slug,
    component: path.resolve(`./src/components/SinglePrismicBlogPost.tsx`),
    context: {
      // Data passed to context is available
      // in page queries as GraphQL variables.
      slug: slug,
      id: x.node._meta.id,
    },
  })
})

Template usage with next/previous links

const PrismicIndex = ({ data, pageContext }) => {
  return (
    <main>
      {data.prismic.allBlog_posts.edges.map((edge, index) => (
            <div key={index}>
              <Link
                to={yourFnToCreatePath(edge.node.title[0].text)}
              >
                {edge.node.title[0].text}
              </Link>
            </div>
        )
      )}

        <footer>
          {pageContext.nextPagePath && (
            <Link to={pageContext.nextPagePath}>Next</Link>
          )}

          {pageContext.previousPagePath && (
            <Link to={pageContext.previousPagePath}>Previous</Link>
          )}
        </footer>
      </main>
    </Layout>
  );
};
export default PrismicIndex;

export const query = graphql`
  query($first: Int = 2, $after: String) {
    prismic {
      allBlog_posts(
        sortBy: date_DESC,
        first: $first,
        after: $after
        ) {
        edges {
          node {
            title
            excerpt
            featured_image
            author
            date
          }
        }
        pageInfo {
          hasNextPage
          hasPreviousPage
          startCursor
          endCursor
        }
      }
    }
  }
`;

Types of prismicConnectionArgs

You can provide enum, string and array values here:

const prismicConnectionArgs: {
  someEnumValue: 'date_DESC', // Enum value
  someMatchingStringSelector: `"myString"`,
  someArray: ["tag1", "tag2"]
}

This will be provided back to your template in the pageContext which also makes them available in graphQl variables.

Other pagination options

If your emphasis is less on speed and SEO and more on publishing an article with instant feedback, then the source plugin has an example of run-time pagination: https://github.com/birkir/gatsby-source-prismic-graphql/tree/master/examples/pagination

Contributing

The plugin is written in typescipt and has rudimentary snapshot based coverage. Please see the package.json for available scripts to build and test locally.

Todos

  • Update tests to parse the graphql query in a structured manner rather than a string snapshot