Skip to content

Communicating between createPage and the template, how? #11774

@kuworking

Description

@kuworking

When creating pages with templates, the way the docs guide us (AFAIU) here is that by creating a page with context, we'll be able to retrieve this context when on the template file.

But the key is here:

... the gatsby-source-filesystem plugin ships with a function for creating them ...
Add your new slugs directly onto the MarkdownRemark nodes. Any data you add to nodes is available to **query** later with GraphQL.

so that anything that you add here (from docs):

createPage({
  path: node.fields.slug,
  component: path.resolve(`./src/templates/blog-post.js`),
  context: {
    slug: node.fields.slug,
  },
})

you will be able to select it when inside the template by querying it:

export const query = graphql`
  query($slug: String!) {
    markdownRemark(fields: { slug: { eq: $slug } }) {
      html
      frontmatter {
        title
      }
    }
  }
`

But, if you don't use the plugin, then whatever you add to the context will be there, but not in the schema (?), so you will not be able to query for your specific node by using $slug

Then, the question is, how is this done? How can I send to the template the content I want it to format? Because when I'm in the template I only have access to graphql that shows me all the existing nodes, but no obvious way to use the specific node I am processing at that time

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions