Programmatically create pages from data fails
billp72 opened this issue · 0 comments
billp72 commented
I'm trying to createpages. It works when I use gatsby develop but it fails when I use gatsby build
const Project = ({ data }) => {
34 | const post = data.markdownRemark;
> 35 | const featuredImgFluid = post.frontmatter.featuredImage.childImageSharp.fluid;
| ^
36 | return (
37 | <Layout>
38 | <Container>
WebpackError: TypeError: Cannot read property 'childImageSharp' of null
- project.js:35 Project
src/templates/project.js:35:59
- gatsby-ssr.js:14 Module.replaceRenderer
gatsby-ssr.js:14:40
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === 'MarkdownRemark') {
const slug = createFilePath({ node, getNode, basePath: 'content' });
createNodeField({
node,
name: `slug`,
value: slug,
});
}
};
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const result = await graphql(`
query {
allMarkdownRemark {
edges {
node {
fields {
slug
}
}
}
}
}
`);
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
createPage({
path: node.fields.slug,
component: path.resolve(`./src/templates/project.js`),
context: {
// Data passed to context is available
// in page queries as GraphQL variables.
slug: node.fields.slug,
},
});
});
};