我正在使用 Gatsby 5 和 contentful/rich-text-react-renderer。
我有一个 ContentfulPost 类型,其中有一些指向富文本中相关类型的链接。
我正在关注文档(来自 Gatsby 和 Contentful),它们说我需要在 GraphQL 中查询它们:
query MyQuery {
allContentfulPost {
nodes {
body {
references {
... on ContentfulAlbum {
node_locale
contentful_id
__typename
id
slug
}
}
}
}
}
}
效果很好:
"nodes": [
{
"body": {
"references": [
{},
{
"contentful_id": "2P4r0pD6zphEIiOaKEaprO",
"__typename": "ContentfulAlbum",
"id": "06305ddb-a3fa-5265-9a2a-3e43524d351e",
"slug": "disorders"
}
]
}
},
]
文档然后指出我应该创建一个自定义渲染器并使用
node.data.target
中的一些属性(参见“参考”部分)。这些是我使用的选项:
const options2 = {
renderNode: {
[INLINES.ENTRY_HYPERLINK]: (node) => {
console.log(node);
return <></>;
},
},
};
但是,我在日志中获得的唯一字段是:
data: Object { target: {…} }
target: Object { sys: {…} }
sys: Object { id: "2P4r0pD6zphEIiOaKEaprO", type: "Link", linkType: "Entry" }
所以,我没有可以使用的弹头。
我到处都看到(https://stackoverflow.com/a/55769299或https://stackoverflow.com/a/55998008)解决方案是清除缓存(例如使用gatsbyclear) )。然而,这对我来说不起作用。
我的目标是使用动态 url (${locale}/music/${slug}) 渲染链接
我发现了问题:插件本身。
我应该使用
contentful/rich-text-react-renderer
,而不是使用 gatsby-source-contentful/rich-text
。这个确实使用 GraphQL 的查询填充了节点对象