Gatsby在Contentful CMS中提供新变量时生成页面

问题描述 投票:0回答:1

所以我用产品构建了一个Gatsby博客,它可以以编程方式为每个产品生成页面。我想添加到这个项目,以便它还为产品类别生成页面。每个类别页面将显示该类别中的所有产品。内容由Contentful CMS提供。

这可能吗?添加第二个布局模板并创建此类页面?

exports.createPages = ({ graphql, boundActionCreators }) => {
    const { createPage } = boundActionCreators

    return new Promise((resolve, reject) => {
        const blogPost = path.resolve('./src/templates/blog-post.js')
        const categoryPage = path.resolve('./src/templates/category-post.js')

        resolve(
            graphql(
                `
            {
              allContentfulBlog(limit: 200) {
                edges {
                  node {
                    id
                    categories
                    mainTitle
                    mainImg {
                      id
                    }
                    mainText {
                      childMarkdownRemark {
                        excerpt
                      }
                    }
                    slug
                  }
                }
              }
            }
          `
            )
                .then(result => {
                    if (result.errors) {
                        console.log(result.errors)
                        reject(result.errors)
                    }

                    result.data.allContentfulBlog.edges.forEach(edge => {
                        createPage({
                            path: edge.node.slug,
                            component: blogPost,
                            context: {
                                slug: edge.node.slug,
                            },
                        })
                    })
                    return
                })
                .then(result => {
                    result.forEach(category => {
                        createPage({
                            path: '/' + edge.node.category,
                            component: categoryPage,
                            context: {
                                slug: edge.node.category,
                            },
                        })
                    })
                })
        )
    })
}

promise中的第一个.then()方法工作正常,我的想法是简单地添加第二个.then(),以及类别页面的模板。

终点显示:terminal

reactjs gatsby contentful
1个回答
1
投票

您可以在产品页面循环后插入此项,但您没有类别数据。可以从产品中收集它,但这将是一个古怪的解决方法。

你需要单独的查询,单独的承诺,并在顶层组成承诺(.all())。

© www.soinside.com 2019 - 2024. All rights reserved.