简体   繁体   中英

Gatsby pageContext empty in GraphQL

I'm building a dynamic Gatsby site with multiple languages through Contentful. 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.

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 . However, for every other page created in gatsby-node.js the pageContext object appears as it should.

I'm creating my pages in gatsby-node.js like this:

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:

/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 .

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.

Anyone who have experience with this?

Yes, Gatsby by default creates a page for every js file in src/pages . Try moving your template to some other folder like src/templates and adjust the value of component in createPage() accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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