0

我有一个 Gatsby Prismic 博客,其中包含两种自定义内容类型 - 指南和评论。

我希望 URL 结构为 www.mysite.com/guide_url/review_url。

到目前为止我所做的:

我已将 Prismic 中的内容关系字段添加到父自定义类型指南并将子项(评论)链接到它。

在我的 gatsby-node.js 文件中,代码如下所示:

exports.createPages = ({ graphql, boundActionCreators }) => {
  const { createPage } = boundActionCreators;
  return new Promise((resolve, reject) => {
    graphql(`
      {
        allPrismicGuide {
          edges {
            node {
              id
              uid
            }
          }
        }
        allPrismicReview {
          edges {
            node {
              id
              uid
            }
          }
        }
    `).then(result => {
      const categoriesSet = new Set();
      result.data.allPrismicGuide.edges.forEach(({ node }) => {
        createPage({
          path: node.uid,
          component: path.resolve(`./src/templates/GuideTemplate.js`),
          context: {
            // Data passed to context is available in page queries as GraphQL variables.
            slug: node.uid
          }
        });
      });
      result.data.allPrismicReview.edges.forEach(({ node }) => {
        createPage({
          path: node.uid,
          component: path.resolve(`./src/templates/ReviewTemplate.js`),
          context: {
            // Data passed to context is available in page queries as GraphQL variables.
            slug: node.uid,
            tags: node.tags
          }
        });
      });

我对盖茨比和棱镜很陌生。

我很想得到一些反馈(有用的链接)和建议!

4

2 回答 2

1

[更新] Prismic 不再推荐gatsby-source-prismic-graphql插件。

这是一篇文章,可以帮助您迁移到另一篇文章:

如何将项目从“gatsby-source-prismic”迁移到“gatsby-source-prismic-graphql”

于 2020-02-18T15:31:11.507 回答
0

Prismic 文档现在推荐gatsby -source-prismic插件。

如果您的关系字段被命名,review您的查询应该如下所示:

{
  allPrismicGuide {
    edges {
      node {
        id
        uid
        data {
          review {
            document {
              ... on PrismicReview {
                uid
              }
            }
          }
        }
      }
    }
  }
}

然后您应该能够访问node.data.review.document.uid. 所以:

createPage({
  path: `${node.uid}/${node.data.review.document.uid}`,
  // ... other stuff ...
});

在你的linkResolver

if (doc.type === 'guide') {
  return `${doc.uid}/${doc.data.review.uid}`;
}

盖茨比源棱镜版本:3.2.1

于 2020-10-04T23:24:23.840 回答