简体   繁体   English

GraphQL 中的 Gatsby pageContext 为空

[英]Gatsby pageContext empty in GraphQL

I'm building a dynamic Gatsby site with multiple languages through Contentful.我正在通过 Contentful 构建一个具有多种语言的动态 Gatsby 网站。 I pass the page's ID in the pageContext object in my gatsby-node.js to fetch the necessary content on each page, but on the root URL / the data inside my pageContext object is missing.我在我的gatsby-node.js中的pageContext对象中传递了页面的 ID,以获取每个页面上的必要内容,但是在根 URL 上/我的pageContext对象中的数据丢失了。

In my index.js -file, the pageContext is empty on the root page / , with the exception of the parameter pageContext.isCreatedByStatefulCreatePages which is set to true .在我的index.js文件中,根页面/上的pageContext为空,但参数pageContext.isCreatedByStatefulCreatePages设置为true However, for every other page created in gatsby-node.js the pageContext object appears as it should.但是,对于在gatsby-node.js中创建的每个其他页面, pageContext对象都会按应有的方式出现。

I'm creating my pages in gatsby-node.js like this:我在gatsby-node.js中创建我的页面是这样的:

const baseUrl = locale === "da" ? "/" : `/${locale}/`
console.log(`${baseUrl}${slug ? slug : ""}`)
createPage({
  path: `${baseUrl}${slug ? slug : ""}`,
  component: path.resolve("./src/pages/index.js"),
  context: {
    id,
    slug,
    locale,
  },
})

My above console.log outputs the following:我上面的console.log输出如下:

/virksomhed
/konsulent
/kontakt
/ <-- Page context is empty here
/om-os
/en/company
/en/consultant
/en/contact
/en/
/en/about-us

I can see that every page is created.我可以看到每个页面都已创建。 On any other page than the index page ("/") the isCreatedByStatefulCreatePages is set to false .在除索引页 ("/") 之外的任何其他页面上, isCreatedByStatefulCreatePages 设置为false

I suspect that Gatsby somehow overrides the "createPage" for the root URL, but I wasn't able to find anything describing it in the documentation.我怀疑 Gatsby 以某种方式覆盖了根 URL 的“createPage”,但我无法在文档中找到任何描述它的内容。

Anyone who have experience with this?谁有这方面的经验?

Yes, Gatsby by default creates a page for every js file in src/pages .是的,Gatsby 默认为src/pages中的每个 js 文件创建一个页面。 Try moving your template to some other folder like src/templates and adjust the value of component in createPage() accordingly.尝试将模板移动到其他文件夹,如src/templates并相应地调整createPage()component的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM