Coder Social home page Coder Social logo

kaumac / gatsby-prismic-pagination Goto Github PK

View Code? Open in Web Editor NEW

This project forked from laurienicholas/gatsby-prismic-pagination

0.0 2.0 0.0 101 KB

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

License: MIT License

TypeScript 29.92% JavaScript 70.08%

gatsby-prismic-pagination's Introduction

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

gatsby-prismic-pagination's People

Contributors

laurienicholas avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.