gatsby-transform-remark: fields from createNodeField are not available in createPages
See original GitHub issueDescription
During onCreateNode
Iβm adding two node fields to MarkdownRemark
nodes:
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
switch (node.internal.type) {
case 'MarkdownRemark': {
const { permalink, layout } = node.frontmatter
const { relativePath } = getNode(node.parent)
let slug = permalink
if (!slug) {
slug = `/${relativePath.replace('.md', '')}/`
}
// Used to generate URL to view this content.
createNodeField({
node,
name: 'slug',
value: slug || ''
})
// Used to determine a page layout.
createNodeField({
node,
name: 'layout',
value: layout || ''
})
break
}
default: {
break
}
}
}
Iβve been debugging this and this seems to work. Now I want to create some pages based on that fields:
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
const allMarkdown = await graphql(`
{
allMarkdownRemark(limit: 1000) {
edges {
node {
fields {
layout
slug
}
}
}
}
}
`)
if (allMarkdown.errors) {
reporter.panicOnBuild(allMarkdown.errors)
}
}
This throws now the error: GraphQLError: Cannot query field "layout" on type "MarkdownRemarkFields".
Steps to reproduce
- Please clone the following project: https://github.com/openscript/obin.ch
- Go to line 74 in
gatsby-config.js
and add thepagesPaths
option togatsby-plugin-i18n
{
resolve: 'gatsby-plugin-i18n',
options: {
langKeyDefault: 'en',
useLangKeyLayout: false,
prefixDefault: false,
pagesPaths: ['/src/pages/', '/src/content/'] // <--- this one
}
},
- Run
yarn start
.
Expected result
The layout
field should be available.
Actual result
The layout
field is not available and the graphql query fails.
Environment
System:
OS: Linux 5.4 undefined
CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
Shell: 5.0.11 - /bin/bash
Binaries:
Node: 12.14.0 - ~/.asdf/installs/nodejs/12.14.0/bin/node
Yarn: 1.21.1 - ~/.asdf/installs/nodejs/12.14.0/.npm/bin/yarn
npm: 6.13.4 - ~/.asdf/installs/nodejs/12.14.0/bin/npm
Languages:
Python: 3.8.1 - /usr/bin/python
Browsers:
Firefox: 71.0
npmPackages:
gatsby: ^2.18.17 => 2.18.17
gatsby-image: ^2.2.37 => 2.2.37
gatsby-plugin-canonical-urls: ^2.1.18 => 2.1.18
gatsby-plugin-emotion: ^4.1.18 => 4.1.18
gatsby-plugin-graphql-codegen: ^2.0.1 => 2.0.1
gatsby-plugin-i18n: ^1.0.1 => 1.0.1
gatsby-plugin-manifest: ^2.2.34 => 2.2.34
gatsby-plugin-react-helmet: ^3.1.18 => 3.1.18
gatsby-plugin-sharp: ^2.3.10 => 2.3.10
gatsby-plugin-svgr: ^2.0.2 => 2.0.2
gatsby-plugin-typescript: ^2.1.23 => 2.1.23
gatsby-remark-copy-linked-files: ^2.1.33 => 2.1.33
gatsby-remark-images: ^3.1.39 => 3.1.39
gatsby-remark-prismjs: ^3.3.28 => 3.3.28
gatsby-remark-responsive-iframe: ^2.2.30 => 2.2.30
gatsby-remark-smartypants: ^2.1.19 => 2.1.19
gatsby-source-filesystem: ^2.1.43 => 2.1.43
gatsby-transformer-json: ^2.2.22 => 2.2.22
gatsby-transformer-remark: ^2.6.45 => 2.6.45
gatsby-transformer-sharp: ^2.3.9 => 2.3.9
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Gatsby JS nodes not always created properly for use by ...
In my gatsby project, I have gatsby-node.js export two Bound Action Creators: onCreateNode and createPages. Normally these run ok - whenΒ ...
Read more >Actions | Gatsby
Documentation on actions and how they help you manipulate state within Gatsby.
Read more >MDX 2 Breaking changes and gatsby-plugin-mdx v4 (Slug)
In this post I'll only be covering the changes to the slug field. This change may only be relevant if you're using mdx.slug...
Read more >Gatsby Graphql schema customization for beginners
Learn how to replace data in the existing Graphql fields, ... As you can see, there are no more null values in isDraft...
Read more >Gatsby Tutorial: Sourcing Data and Content - Ibaslogic
Gatsby, unlike other site generators, allows us to source data not only ... These data are available inside the site dropdown field in...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@openscript iβve picked up on your issue and below iβm going to detail the steps i took to triage this.
gatsby-config.js
to match the issue descriptiongatsby develop
and the issue popped up.src\content\example.md
frontmatter to the following:gatsby-node.js
so that i could get a clear view of the node information Resulting in the following code:cache
and public folder throughgatsby clean
and issued another build and i get the following output:As you can see thereβs already a slug field added. It seems that
gatsby-plugin-i18n
is taking care of it. And with that it seems that you cannot create a field that already exists. Thatβs my take on this. I could be wrong and someone more knowledgeable can offer you better insights.onCreateNode
api hook to the following:layoutA.tsx
andlayoutB.tsx
with the following content:src\templates\layoutA.tsx
In
src\templates\layoutB.tsx
:gatsby develop
and i get the following output:Feel free to provide feedback so that we can close this issue or continue to work on it until we find a suitable solution
@openscript no problem, glad that i was able to provide you with some insights that helped you solve your issue.