Skip to content

Instantly share code, notes, and snippets.

@matthewbeta
Created November 19, 2019 09:28
Show Gist options
  • Save matthewbeta/f99db9e1ddd9edf5d22962d01914a53e to your computer and use it in GitHub Desktop.
Save matthewbeta/f99db9e1ddd9edf5d22962d01914a53e to your computer and use it in GitHub Desktop.
Paginated Blog in Gatsby
const createPaginatedPages = require('gatsby-paginate');
exports.createPages = ({ graphql, actions: { createPage } }) => {
return new Promise((resolve, reject) => {
graphql(`
{
blog: craft {
entriesConnection(section: [yoursectionhandle]) {
edges {
node {
... on Craft_YourBlogHandle {
// STUFF HERE
}
}
}
}
}
}
`).then((result) => {
createPaginatedPages({
edges: result.data.blog.entriesConnection.edges,
createPage: createPage,
pageTemplate: 'src/templates/blogPaginate.js',
pageLength: 24,
pathPrefix: 'blog',
buildPath: (index, pathPrefix) =>
index > 1 ? `${pathPrefix}/${index}` :
`/${pathPrefix}`,
context: {
allPosts: result.data.blog.entriesConnection.edges,
slug: result.data.blog.entriesConnection.edges.slug
}
});
result.data.blog.entriesConnection.edges.forEach(({ node }, index) => {
createPage({
path: `blog/${node.slug}`,
component: path.resolve(`./src/templates/blogEntry.js`),
context: {
prevPost:
index === 0 ? 'null' :
result.data.blog.entriesConnection.edges[index - 1].node.slug,
nextPost:
index === result.data.blog.entriesConnection.edges.length - 1 ? 'null' :
result.data.blog.entriesConnection.edges[index + 1].node.slug,
slug: node.slug
}
});
});
resolve();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment