Skip to content

Instantly share code, notes, and snippets.

@jazibsawar jazibsawar/gatsby-node.js Secret
Created Jun 5, 2018

Embed
What would you like to do?
const _ = require('lodash')
const Promise = require('bluebird')
const path = require('path')
exports.createPages = ({ graphql, boundActionCreators }) => {
const { createPage } = boundActionCreators
const indexPage = path.resolve('./src/pages/index.js')
createPage({
path: `posts`,
component: indexPage,
})
return new Promise((resolve, reject) => {
const blogPost = path.resolve('./src/templates/blog-post.js')
resolve(
graphql(
`
{
allCosmicjsPosts(sort: { fields: [created], order: DESC }, limit: 1000) {
edges {
node {
slug,
title
}
}
}
}
`
).then(result => {
if (result.errors) {
console.log(result.errors)
reject(result.errors)
}
// Create blog posts pages.
const posts = result.data.allCosmicjsPosts.edges;
_.each(posts, (post, index) => {
const next = index === posts.length - 1 ? null : posts[index + 1].node;
const previous = index === 0 ? null : posts[index - 1].node;
createPage({
path: `posts/${post.node.slug}`,
component: blogPost,
context: {
slug: post.node.slug,
previous,
next,
},
})
})
})
)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.