...

/

Creating Pages with the createPages API

Creating Pages with the createPages API

Learn how to programmatically create pages in Gatsby without adding page components to the pages directory.

Why we need the createPages API

Until this point, we only knew one way of creating pages in Gatsby projects—creating and exporting a page component in the pages directory. While this seems like the best way to go, it has some limits, too.

Let’s consider our blog posts’ detail pages. We recall that clicking any of our posts to open the detail page redirects to a 404. This is because we have not built a page for it yet. Now, we can think of going into the pages directory and creating pages for each of our Markdown blog posts. This is not ideal in any way because we may end up having up to ten or more blog posts, and we cannot create page components for each.

For the case above, we need a way to fetch the blog posts and create a page for each of them using code. This process must be run at build time, making the gatsby-node.js file the perfect (and the only) place to house such code.

This brings us to the ...