2

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?

dnlmzw
  • 751
  • 1
  • 8
  • 20

1 Answers1

1

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.

Z. Zlatev
  • 4,757
  • 1
  • 33
  • 37
  • I removed the index.js from the pages folder and created my page.js in my templates folder instead, which fixed the issue. Thanks! – dnlmzw Dec 02 '19 at 20:42