-
-
Save jazibsawar/26e6a4eb34aca0d7e24389ba89b562e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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