Build a Gatsby Blog using the Cosmic JS source plugin

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


Download the GitHub repo.
Check out the demo.


You will be required to install Node JS, npm, and Gastby CLI before starting. Make sure you already have them installed.

What is Gatsby?

Gatsby is a blazing-fast website framework for React. It allows developers to build React based websites within minutes. Whether you want to develop a blog or a business website, Gatsby will be a good option.

Because it is based on React, the website pages are never reloaded and also it will generate static pages which make the generated website super fast.  

Blog Development

We have to set up the environment in order to start working on the blog.

Install Gatsby

First, install Gatsby CLI:

npm install --global gatsby-cli

Scaffold a Gatsby Template

 gatsby new gatsby-blog-cosmicjs

Enter in your project's folder:

cd gatsby-blog-cosmicjs

Start the server:

gatsby develop

At this point, you should already be able to get access to your Gatsby JS blog website at this address: http://localhost:8000.

Install the Cosmic JS Source Plugin

In Static Blog, your data can be consumed from different sources: Markdown files, HTML files, External API (WordPress, Cosmic JS, etc).

Therefore, Gatsby implemented independent layer: the data layer. Which is powered by GraphQL. Very exciting stuff!

To connect this Data Layer with different Data Providers, you need to integrate Source Plugin. Fortunately, there are many Source Plugins available to fulfil most of the needs.

In our case, we are using Cosmic JS. Obviously, we need to integrate Source Plugin for Cosmic JS. Good news: Cosmic JS has implemented their Source Plugin!

Let's install:

npm install --save gatsby-source-cosmicjs

We need to install some other plugins also. Let's do that also

npm install --save gatsby-plugin-offline gatsby-source-filesystem

These plugins need some configurations. So, replace the content of gatsby-config.jswith:

Path: gatsby-config.js

Then, restart the server to let Gatsby consider these updates.

Posts List & Settings

First, we want to display the list of posts on the homepage. To do so, add the following content to the existing homepage file:

Path: src/pages/index.js


At the end of index.jsfile, 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 } destructed object as parameter of IndexPage and loop on its allCosmicjsPosts & cosmicjsSettings object to display the data.

Single Post Layout

Till now we have integrated Cosmic JS source plugin with Gatsby and it's look like a Blog. Now we will work on blog post's details page.

Let's create the template:

Path: 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 thanks to the createPage function.

Path: gatsby-node.js

Restart the Gatsby server.

From now on, you should be able to visit the detail page by clicking on URLs displayed on the homepage.

Extra Stuff!

In addition to this, 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 is available on GitHub. To see it live, clone the repository, and run (cd gatsby-blog-cosmicjs && npm i && npm run develop).

Finally, restart the server and visit the website. It will look awesome!

The static website generated by Gatsby can easily be published on storage providers: Netlify, S3/Cloudfront, GitHub pages, GitLab pages, Heroku, etc.

Note: Our demo is deployed on Netlify.


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. 

Was this article helpful?

Thank you! Your input helps us improve our articles and resources. If you have any other questions or comments, please contact support.

You may also like

In this tutorial I'm going to demonstrate how to build a photography portfolio website using React, Styled-Components and Cosmic JS. Let's get started.
In addition to Import / Export Features and Revision History, Cosmic JS has added Automatic Bucket Backups to deliver even more peace of mind to you and your content editing team. To add daily Backups to your Buckets go to Your Bucket Dashboard > Settings > Backups.
In this tutorial, Cosmic JS co-founder Tony Spiro takes you on a tour of Cosmic JS Webhooks. Learn how webhooks work, see how to easily install integrations with Slack, auto-deploy websites to Netlify, and get an intro to Cosmic JS Functions.
This application was mainly inspired by Offerup, Letgo, and Craigslist. Like Craigslist, LetGo allows you to buy and sell items locally, which means you don’t have to deal with the hassles of shipping like you would on eBay or Amazon. With 30 million users, it is more popular than OfferUp...
One of the largest problems I’ve faced while working on projects is having to deal with integrating content and events into third-party services.It’s time consuming, and as the demands in the application in question grows, so does the mental strain in having to keep up to pace with all the changes
In this Guide, I’m going to show you how Imgix Extension will provide optimized images for your application. Will also talk about the benefits, and show you how to install it to your Bucket in a few clicks.