从内容数据以编程方式创建 Gatsby 页面

IT技术 javascript reactjs graphql contentful gatsby
2021-05-01 05:27:11

我正在寻求有关 GatsbyJS 和 Contentful 的帮助。文档并没有给我足够的信息。

我希望以编程方式创建基于内容数据的页面。在这种情况下,数据类型是一个零售“商店”,在 /retail_store_name 有一个 gatsby 页面

每个商店的 index.js 基本上是几个带有props的react组件,例如商店名称和谷歌地点 ID。

  1. 向内容添加数据。这是我的示例数据模型:

    {
        "name": "Store"
        "displayField": "shopName",
        "fields": [
            {
                "id": "shopName",
                "name": "Shop Name",
                "type": "Symbol",
                "localized": false,
                "required": true,
                "validations": [
                    {
                    "unique": true
                    }
                ],
                "disabled": false,
                "omitted": false
                },
                {
                "id": "placeId",
                "name": "Place ID",
                "type": "Symbol",
                "localized": false,
                "required": true,
                "validations": [
                    {
                    "unique": true
                    }
                ],
                "disabled": false,
                "omitted": false
            }
    }
    
  2. 我已将内容丰富的站点数据添加到 gatsby-config.js

    // In gatsby-config.js
    plugins: [
        {
            resolve: `gatsby-source-contentful`,
            options: {
            spaceId: `your_space_id`,
            accessToken: `your_access_token`
            },
        },
    ];
    
  3. 查询内容丰富 - 我不确定这应该发生在哪里。我有一个模板文件,它是从内容数据创建的每个商店网页的模型。

如前所述,这只是一些传入 props 的组件。示例:

import React, { Component } from "react";

export default class IndexPage extends Component {
constructor(props) {
    super(props);
    this.state = {
        placeId: "",
        shopName: "",
    };
}
render (){
    return (
        <ComponentExampleOne shopName={this.state.shopName} />
        <ComponentExampleTwo placeId={this.state.placeId} />
    );
}

我真的不知道该怎么做。最终目标是为非技术用户自动发布,他们在 Contentful 中发布新商店以在生产站点上进行更新。

1个回答

您可以在构建时动态创建页面,为此您需要向gatsby-node.js文件添加一些逻辑这是一个简单的片段。

const path = require('path')

exports.createPages = ({graphql, boundActionCreators}) => {
  const {createPage} = boundActionCreators
  return new Promise((resolve, reject) => {
    const storeTemplate = path.resolve('src/templates/store.js')
    resolve(
      graphql(`
        {
          allContentfulStore (limit:100) {
            edges {
              node {
                id
                name
                slug
              }
            }
          }
        }
      `).then((result) => {
        if (result.errors) {
          reject(result.errors)
        }
        result.data.allContentfulStore.edges.forEach((edge) => {
          createPage ({
            path: edge.node.slug,
            component: storeTemplate,
            context: {
              slug: edge.node.slug
            }
          })
        })
        return
      })
    )
  })
}

createPages已导出的是你能找到的文档的完整列表中的盖茨比节点API函数在这里

对于查询,allContentfulStore它是这样调用的,因为您的 contentType 名称是storegatsby 查询将是 allContentful{ContentTypeName}。

最后,我创建了一个 YouTube 视频系列,解释了如何使用 Contentful 构建 Gatsby 网站。你可以在这里找到

我希望这能回答你的问题。干杯,哈立德