Migrate to Netlify Today

Netlify announces the next evolution of Gatsby Cloud. Learn more

ContactSign Up

Build a Gatsby Blog using the Cosmic source plugin

Tony Spiro
June 7th, 2018
Cosmic, and Gatsby logos next to a preview of the Cosmic starter

This article originally appears on the Cosmic website.

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


Download the GitHub repo.

Check out the demo.

Check out the Cosmic Source Plugin for Gatsby.


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 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 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, 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. We need a source plugin to connect data from Cosmic to our Gatsby site. Good news: there's already a source plugin for Cosmic!

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 Bucket by going to Your Cosmic 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 at:
    Your Cosmic 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:


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 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.

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


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 advantages.

Share on TwitterShare on LinkedInShare on FacebookShare via Email

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

Follow Tony Spiro on Twitter

Tagged with NoneView all Tags

Originally published at Cosmic

Talk to our team of Gatsby Experts to supercharge your website performance.

Contact Gatsby Now
© 2023 Gatsby, Inc.