Skip to main content

Tony Spiro

Cosmic JS Co-founder. Building (blazing fast) apps faster @ https://cosmicjs.com

2 min read · June 7th 2018 (originally published at Cosmic JS)

Build a Gatsby Blog using the Cosmic JS source plugin

Cosmic JS Gatsby

This article originally appears on the Cosmic JS website.

In this tutorial, I’m going to show you how to create a simple but blazing fast blog using React, Gatsby, and Cosmic JS.

TL;DR

Download the GitHub repo.

Check out the demo.

Check out the Cosmic JS Source Plugin for Gatsby.

Prerequisites

This tutorial requires Node.js and npm. Make sure you already have them installed.

Blog Development

In order to start working on the blog, we first have to set up the environment.

Install Gatsby and Set Up Your Site

First, install the Gatsby CLI:

Create a new site based on the Gatsby Cosmic JS starter:

Enter in your project’s folder:

Start the server:

At this point, you can access your Gatsby website by visiting http://localhost:8000.

Install the Cosmic JS Source Plugin

In a static website, data can be consumed from multiple different sources, for example Markdown files, HTML files, and/or an eternal API (WordPress, Cosmic JS, etc).

To make consuming data simpler, Gatsby implements a data layer powered by GraphQL. Very exciting stuff!

To connect this data layer with different data providers, you need to integrate a source plugin. Fortunately, there are many source plugins available for common data sources, and an API available to create your own if necessary.

In our case, we are using Cosmic JS. We need a source plugin to connect data from Cosmic JS to our Gatsby site. Good news: there’s already a source plugin for Cosmic JS!

Install the source plugin with the following command:

We’re going to use a couple other plugins later, so let’s install them here:

These plugins need some configuration, so let’s replace the content of gatsby-config.js with:

A couple of notes on this config:

  1. Notice bucketSlug is set to gatsby-blog-cosmic-js. This is a demo Bucket that includes pre-populated content, so when you download the GitHub repo and start the app, it will display demo content right out of the box. You can also import this demo content into your own Cosmic JS Bucket by going to Your Cosmic JS Bucket Dashboard > Settings > Apps, then find and install the Gatsby Blog.
  2. The API read key is optional and can be set after you login to Cosmic JS at: Your Cosmic JS Bucket Dashboard > Settings > Basic Settings

Now restart the server to apply the updates.

Posts List & Settings

First, we want to display the list of posts on the homepage. To do so, add the following content to src/pages/index.js:

Explanation:

At the end of index.js file, we exported pageQuery. These are GraphQL queries which are used to fetch important information about settings and list of posts.

Then, we pass the { data } destructured object as parameter of IndexPage and loop on its allCosmicjsPosts & cosmicjsSettings object to display the data.

Gatsby Cosmic Screenshot

Single Post Layout

Till now we have integrated Cosmic JS source plugin with Gatsby and it’s looking like a blog. Now we will work on adding a details page for individual blog posts.

Create the template at src/templates/blog-post.js:

That looks fine, but at this point, Gatsby does not know when this template should be displayed. Each post needs a specific URL. So, we are going to inform Gatsby about the new URLs we need using the createPages API.

Path: gatsby-node.js

Restart the Gatsby server, then visit the detail page by clicking on URLs displayed on the homepage.

Gatsby Cosmic Screenshot

Extra Stuff!

In addition to the code covered in this tutorial, we also implemented src/components/bio.js to display author information & src/layouts/index.js to create a generic layout for the blog.

The source code for this tutorial is available on GitHub. To see it live, clone the repository, and run (cd gatsby-blog-cosmicjs && npm i && npm run develop) or check out the demo on Netlify.

The static website generated by Gatsby can easily be published on services like Netlify, S3/CloudFront, GitHub Pages, GitLab Pages, Heroku, etc.

Gatsby Cosmic Screenshot

Conclusion

Congrats! You’ve successfully built a super fast and easy-to-maintain blog! Feel free to continue this project to discover both Gatsby and Cosmic JS advantages.

Enjoyed this post? Receive the next one in your inbox!

Docs
Tutorials
Plugins
Blog
Showcase