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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I removed the index.js from the pages folder and created my page.js in my templates folder instead, which fixed the issue. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.