Gatsby 5迁移后无法获取MDX节点的fileAbsolutePath求助
解决Gatsby 5中MDX节点无法获取文件路径的问题
Gatsby 5 对 MDX 节点的结构做了调整,原有的 fileAbsolutePath 字段被移除,且不再直接提供 file 关联字段。要获取文件绝对路径,需要通过 parent 字段关联到对应的 File 节点,再读取其路径属性,正确的查询如下:
allMdx { nodes { id frontmatter { name type } parent { ... on File { absolutePath } } } }
另外你之前的查询里把 absolutePath 拼写成了 absoluteParth,这也是需要修正的点。
如果需要把路径直接挂载在MDX节点下,也可以通过 gatsby-node.js 中的 onCreateNode API 自定义字段:
exports.onCreateNode = ({ node, actions, getNode }) => { const { createNodeField } = actions; if (node.internal.type === 'Mdx') { const fileNode = getNode(node.parent); createNodeField({ node, name: 'fileAbsolutePath', value: fileNode.absolutePath, }); } };
之后就能在查询里通过 fields.fileAbsolutePath 获取路径:
allMdx { nodes { id frontmatter { name type } fields { fileAbsolutePath } } }
内容的提问来源于stack exchange,提问作者Jani Bolkvadze
相关产品推荐
相关产品推荐

