question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using createPages with User Auth and static content (blog posts)? (Bonus routing question inside)

See original GitHub issue

So I’m writing a small app and I came across an issue that I haven’t been able to solve. First, I have this here: https://www.gatsbyjs.org/tutorial/authentication-tutorial/ which allows me to use this:


// Implement the Gatsby API “onCreatePage”. This is
// called after every page is created.
exports.onCreatePage = async ({ page, actions }) => {
  const { createPage } = actions
  // page.matchPath is a special key that's used for matching pages
  // only on the client.
  if (page.path.match(/^\/app/)) {
    page.matchPath = "/app/*"
    // Update the page.
    createPage(page)
  }
}

Then I’m using a graphql query to query static data (blog posts). The issue is that I’m unable to do both, for some reason.

When I run the following, I’m greeted with an error of ‘Cannot read property path of undefined.’

exports.createPages = async ({ page, actions, graphql, reporter }) => {
  const { createPage } = await actions
  if (page.path.match(/^\/anime/)) {
    page.matchPath = "/anime/*"
    createPage(page)
  }

  const result = await graphql(`
    {
      allMarkdownRemark {
        edges {
          node {
            html
            id
            frontmatter {
              path
              title
              date
              author
            }
          }
        }
      }
    }
  `)
  const blogs = result.data.allMarkdownRemark.edges

  blogs.forEach(({ node }) => {
    const title = node.frontmatter.path
    actions.createPage({
      path: `${title}`,
      component: require.resolve("./src/templates/newsletter.template.js"),
    })
  })
}

// exports.createPages = async ({ actions, graphql, reporter }) => {
// }

Now, when I run the following code, I do not get any errors. However, when I do it this way and go to the expected route of my blog(s) I’m prompted with post is null.

So my question is this: How can I use createPages/onCreatePage with both of these and have both work?

(Bonus question, but ties into the above)

When I change the route(s) of my blog post(s) in the frontmatter to /title-of-post/ ( being the starting route, in this case, it’s newsletter) I no longer have post is null as pointed out above. But, I want my blog posts to have the route name in front of them. So my question is this: How do I make all of this work?

https://github.com/Joey-Robinson/quick-anime

Is the repo name. Note: I’ve already tried making a folder in pages with the name of newsletter, but that didn’t do anything.

Edit: Formatting. Edit Two: Formatting.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
Js-Brechtcommented, Mar 18, 2020

@Joey-Robinson did that answer your questions?

3reactions
Js-Brechtcommented, Mar 17, 2020

So for the onCreatePage() and the createPages() I should just leave separated to achieve what I’m after?

Yup, that’s correct 🙂.

What’s happening under the hood is Gatsby is using gatsby-plugin-page-creator to process your pages directory. As it collects the files, it calls the createPage() action, which will trigger your onCreatePage() event.

Edit: This is what I have currently: https://github.com/Joey-Robinson/quick-anime/tree/002f6b73c253c88866fe2a4ca37d2d0633fd50dd

Looks pretty good to me. One thing that will happen here is you will wind up with double slashes at the beginning and end, since your slug reads /newsletter/the-greatness-of-cowboy-bebop/. It shouldn’t be a big deal, because your browser should just ignore double slashes.

My example was just to show one of the things you could do for organizing your posts/building your URLs. In my example, I would have made the .md file read:

slug: "the-greatness-of-cowboy-bebop"
category: "newsletter"

Then I can construct my URL from the simple values in the frontmatter. There’s plenty of ways to achieve that kind of affect, and the way you are doing it should be fine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Programmatically Creating Pages | GatsbyJS 中文网
Make a folder called content/posts and create two files in it called blog-1.mdx and blog-2.mdx . You can do this on the command...
Read more >
Part 6: Create Pages Programmatically - Gatsby
Use Gatsby's Filesystem Route API to dynamically create new pages for your blog posts. Render the contents of each blog post. Add a...
Read more >
Gatsby Private Route not working as intended #31861 - GitHub
Hi everyone,. I'm trying to implement a blog with gated content using Gatsby as the front-end and Strapi as the CMS for authentication....
Read more >
Next.js: The Good, Bad and Ugly - An Idiosyncratic Blog
Any file inside the folder pages/api/ is mapped to the route ... Next.js exposes APIs that can be use to fetch data and...
Read more >
Gatsby Changelog | 5.3.0
This means that changes to highly-shared components (such as headers, footers, and common layouts) no longer require a rebuild of all pages that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found