dnlmzw
dnlmzw

Reputation: 761

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?

Upvotes: 2

Views: 1445

Answers (1)

Z. Zlatev
Z. Zlatev

Reputation: 4820

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.

Upvotes: 1

Related Questions