This post was originally published on my blog on January 18, 2018.
Earlier this week I began rebuilding my blog using GatsbyJS + WordPress. As I familiarized with Gatsby, I found myself flipping through a million tabs, and I thought it might be useful to summarize concepts and to aggregate links I found helpful.
I recently decided to tackle a redo of my blog. I wanted to do something different and I’ve been hearing a lot about GatsbyJS. A static site generator for React that I can easily pull my existing WordPress data for? Sold. I’ll try it.
gatsby new gatsby-example-site
That gets us a new site directory with a couple (mostly) empty “gatsby”-prefixed files and a src directory with some basic scaffolding. The configuration and lifecycle hooks for Gatsby get put in those “gatsby”-prefixed files,
Essentially the Gatsby home base. The two things defined here initially (in the starter) are
See the docs page on gatsby-config.js for more.
For the curious:
gatsby-plugin-react-helmetis a plugin the starter includes. It’s a document head manager for React.
We can make use of any of Gatsby’s node APIs by exporting a function with the name of that API from this file.
For my purposes, the only one I have interacted with so far to get up and running is the
createPages API. This gets called after our data has been fetched and is available to use to dynamically build out our static pages. More on this later.
Same as above, we can make use of any of Gatsby’s browser APIs by exporting them from this file.
Having familiarized with the basic structure, my next step was getting my data successfully pulling from WordPress. There’s a plugin for that.
gatsby-source-wordpress is Gatsby’s plugin for sourcing data from WordPress sites using the WordPress JSON REST API.
(Fun fact: the WordPress REST API is already included starting with WordPress 4.7 — no longer requires installing a WordPress plugin. I didn’t actually know that, not having used the WordPress REST API for anything before).
I started by reviewing the code for the plugin’s demo site.
gatsby-config.js, add your configuration options, including your WordPress site’s baseUrl, protocol, whether it’s hosted on wordpress.com or self-hosted, and whether it makes use of the Advanced Custom Fields (ACF) plugin.
Once your source plugin is pulling data, you can construct your site pages by implementing the
createPages API in
gatsby-node.js. When this is called, your data has already been fetched and is available to query with GraphQL. Gatsby uses GraphQL at build time; Your source plugin (in this case,
gatsby-source-wordpress) fetches your data, and Gatsby uses that data to ”automatically infer a GraphQL schema” that you can query against.
createPages API exposes the
The GraphQL function allows us to run arbitrary queries against the local WordPress GraphQL schema… like the site has a built-in database constructed from the fetched data that you can run queries against. (Source)
I used the
gatsby-node.js file from the plugin demo to get started. For my purposes the code to construct ‘posts’ does what I need it to do out of the box (at least for the moment). It queries our local WordPress GraphQL schema for post data, then iterates through each post node to construct a static page for each, based on whatever template we define and feed it.
For example, below is the part of the demo
gatsby-node.js file that iterates over all the WordPress post data.
The docs define a Gatsby page as “a site page with a pathname, a template component, and optional graphql query and layout component.” See the docs on the createPage bound action creator and guide on creating and modifying pages for more detail.
In the step above we dynamically create pages based on our data by passing the absolute path to a defined template to “component”. So what’s a template?
A template is a page component we can loop over to dynamically create pages based on the content we’ve pulled in (described above). We pass the post id to “context” to make it available as a GraphQL variable in the template file. The GraphQL query defined for the template then uses that id to query for data specific to that post.
So a template is a page component that we can use to programmatically create pages. Then what’s a page component?
Page Component — React.js component that renders a page and can optionally specify a layout component and a graphql query. (Source).
React components living in
src/pages automatically become pages. The file name of a page maps to its site path. My site in its current state only has one good example of this —
src/pages/index.js maps to amberley.blog. If I had an ‘about’ page, it would live at
src/pages/about.js, and map to amberley.blog/about. (Since that doesn’t exist, it will actually end up hitting the only other page currently defined in my site, which is
src/pages/404.js — (read about 404 pages).
If you include the “optional GraphQL query” noted above, the result of that query is automatically passed to the component on a
data prop (
this.props.data). (Read more on GraphQL queries).
While this isn’t a tutorial — more a guided walkthrough of me familiarizing and stepping through an initial Gatsby setup — if you’re following along with the demo code you’re probably close to (or already!) seeing your WordPress data populate your Gatsby dev site if you run
npm run develop!
- You don’t need to know GraphQL to get started with Gatsby. I didn’t. It’s been a good introduction.
- Gatsby makes heavy use of plugins — both official and community — for a lot of things, from one that implements Google Analytics, to one that adds GitHub’s accessibility error scanner to all pages.
- Read through some of the source code. I particularly enjoyed reading through the bootstrap process. (It’s beautifully commented).
- Gatsby.js is a static Progressive Web App (PWA) generator, but to be PWA friendly (at least according to the Lighthouse PWA audit), look into two plugins:
- I did end up deploying with Netlify, and I’m super happy with it. (A previous post discussed Netlify a bit more, if you’re interested).