How to use Gatsby with Agility CMS as a Headless CMS
Tags: gatsby, cms, javascript, react
Published: 2019-10-31 13:42:55

##### By: Gabriella Pirrone
![Gabriella Pirrone](https://static.agilitycms.com/Attachments/NewItems/headshot-gabby_20190626205834_0.jpg?w=100)

Exploding in popularity right now is a framework called GatsbyJS, a progressive web-app framework built on-top of ReactJS that includes static site generation and uses GraphQL to store and access content from any data source. Gatsby helps [developers](https://agilitycms.com/free-signup-developers?source=gatsbyblog)build blazing-fast websites and apps.

Hundreds of thousands of developers have adopted Gatsby since it was open\-sourced four years ago. Even so, just last month, [Kyle Mathews](https://www.gatsbyjs.org/contributors/kyle-mathews/) the Founder of GatsbyJS, [announced](https://www.gatsbyjs.org/blog/gatsby-v1/) that they had raised a $15M Series A funding round, led by CRV, to drive the reinvention of website development.

<blockquote class="twitter-tweet"><p lang="en" dir="ltr">📣 BIG NEWS: Gatsby has raised $15M to drive the reinvention of website development.<br><br>We&#39;re thrilled by this validation of our vision and work and for all the new possibilities.<br><br>Read <a href="https://twitter.com/kylemathews?ref_src=twsrc%5Etfw">@kylemathews</a>&#39; writeup <a href="https://t.co/9NXPSM0nQ4">https://t.co/9NXPSM0nQ4</a> <a href="https://t.co/R3WhnMC4DT">pic.twitter.com/R3WhnMC4DT</a></p>&mdash; Gatsby (@gatsbyjs) <a href="https://twitter.com/gatsbyjs/status/1177253681416159233?ref_src=twsrc%5Etfw">September 26, 2019</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>


Since we just released a source-plugin for GatsbyJS + Agility CMS the other week, I was interested in learning more about it. I once again reached out to [James Vidler](https://www.linkedin.com/in/jameskvidler/), the VP of Product and Operations here at Agility CMS. James goes in depth on why a developer should use GatsbyJS, why it’s a good fit with [Agility CMS](https://agilitycms.com/product/for-developers?source=gatsbyblog), its disadvantages, and how you can quickly get started.

### What is GatsbyJS and why should developers use it?    

After nearly a year of research, prototyping, and testing, Gatsby [launched](https://www.gatsbyjs.org/blog/gatsby-v1/) their v1 in July 2017. Gatsby provides efficiency between developers, designers, and authors; it uses static site generators that offer many benefits over traditional dynamic website frameworks — including better performance, higher security, lower cost of scaling, and of course, a better developer experience. To dive into this a little bit more, I asked James to define what GatsbyJS is and why it has become so popular amongst web developers. 

_“GatsbyJS is a static site generator and website framework built on ReactJS, but it’s much more than that. Sites built using GatsbyJS simplify sourcing content from external systems, are progressive web apps, and are blazing fast! The secret sauce is the build process that GatsbyJS websites use. All content is updated during a build process. It’s during this time that your external APIs are called, the data is injected into your React components, and your static HTML is generated._

_In the end, it’s just HTML, JS, and CSS, which means you can deploy your site to any static host. Combine that with a global CDN, and you have a site that scales infinitely and will never go down! This architecture is also referred to as [JAMstack](https://agilitycms.com/resources/posts/what-is-jamstack-and-why-should-you-care-as-an-editor) (JavaScript-APIs-Markup).  As perhaps the most well-known of the JAMstack frameworks, GatsbyJS has a thriving open source community and a world-class developer experience with tutorials for everything and loads of plugins for your favorite data sources.” _

### Why are GatsbyJS and Agility CMS a good fit?    

Agility CMS is a headless CMS. A headless CMS creates an easy interface for your editors to manage content, which is then connected to your site generator by API. Agility CMS has an easy way to quickly get started using GatsbyJS, but we'll discuss that later, and I'll even show you how content creators can build and deploy the site at the click of a button, using [Netlify](https://agilitycms.com/resources/posts/what-is-netlify-and-why-should-you-care-as-an-editor?source=netlifyblog). To start, I asked James what this meant and why you should consider using them together? 

_“Agility CMS websites have always been built with speed in mind. We've strived to address traditional scaling and performance issues with CMS driven websites for years. With our Sync SDKs (for .NET Framework and .NET Core), we solved these issues by syncing content from our CMS to your website, ensuring that the content was always readily accessible on the web server. This meant you never had to fetch content over the internet in order to render your web pages. This has allowed Agility CMS driven websites to handle millions (and millions) of requests without a bottleneck on data access. The only limitation is the compute power of the web server itself. The more requests you have, the more horsepower you'll need to process each request and compile rendered HTML. GatsbyJS solves this same problem but in a different way. Instead of syncing content to the web server, your content is downloaded during the build process and included in your static HTML, all before it is ever deployed. The biggest improvement here is that you no longer have to run your website on a server that needs to compile your HTML on every request. The web server simply needs to return the stored HTML. This allows for an additional performance (and cost) benefit of hosting your site on a static host and makes it very easy to put a CDN in front of your website. _

_Now, what makes Agility CMS particularly a good fit for GatsbyJS sites is that Agility CMS not only provides native support for GatsbyJS via our [gatsby-source-agilitycms](https://github.com/agility/gatsby-source-agilitycms) but with our built-in [Page Management](https://help.agilitycms.com/hc/en-us/articles/360032554331-Page-Management-in-a-Headless-CMS), the plugin can automatically generate your pages based off your page tree in Agility CMS. Combine this with our Page Template/Page Module architecture, and this means editors can create their own pages, add/order/remove functionality and content on each page, all without requiring assistance from a developer.” _

### Are there any disadvantages to using GatsbyJS in comparison to a traditional website build? 

According to Kyle, there is a slight [drawback to Gatsbyjs](https://github.com/gatsbyjs/gatsby/issues/2258) when building apps because it assumes you're building "pages". So, if you're building more of a straight-up app without pages, then it can "limit your freedom somewhat." Besides this, I wanted to know some other drawbacks of using GastbyJS. James describes his thoughts below. 

_“Well, you should always use the right tool for the job. GatsbyJS shares some of the inherent challenges that come with building JAMstack websites. Because you have no backend (server-side) code, they are not a good fit if you have a dependency on lots of server-side code logic. The JAMstack pundits out there will tell you that you can move this logic to a server-less function (Netlify, AWS, and Azure all provide these types of services), and you can, but if you have legacy, or complex backend logic this could be challenging. Another thing to keep in mind as well as the amount of pages and content your website will have, and the frequency at which it will be updated. The only way a site's content can be updated is by doing a build and another deploy, to replace the static HTML files on your static host. If you have thousands of pages and content, this could range from 1-60 minutes in order for content to be updated.”_ 

### How can you get started with GatsbyJS and Agility CMS?  

Want to get a [Gatsby project](https://www.gatsbyjs.org/docs/sourcing-from-agilitycms/) up and running quickly with Agility CMS as the content management system? Use these repo steps below that will show you how to use the [Agility CMS Gatsby Source Plugin](https://github.com/agility/gatsby-source-agilitycms) with the Blog Template in Agility CMS and [instantly get started!](https://www.gatsbyjs.org/docs/sourcing-from-agilitycms/)

#### Create a FREE Agility Account 

Create an Agility CMS account with the Free Plan (this plan is free forever). [Sign up via Agility CMS](https://agilitycms.com/free-signup-developers?source=gatsbyblog). It only takes one second! 

Once your account is created, you’ll need to grab your GUID and API Keys. 

#### Get the Code 

Make sure you have the Gatsby CLI installed: 

`npm install -g gatsby-cli`

Clone the [Agility CMS Gatsby Starter](https://github.com/agility/agility-gatsby-starter) repo from GitHub that has all the code you need to get started: 

`git clone https://github.com/agility/agility-gatsby-starter.git`

Install the dependencies: 

`npm install `

Once you’ve the infrastructure set up, run the site in development mode: 

`gatsby develop `

The site is just a starter, but it has a bunch of interesting features that you can use to build from. The next step is to hook this code up to your new Agility CMS instance that you just created.

#### Hook it up to your Agility CMS Instance 

Edit the `gatsby-config.js` file and replace the `guid` and `apiKey` with yours. 

You can find your API keys on the Getting Started page in the Agility CMS Content Manager.

![Agility CMS - Dashboard - API Keys](https://thepracticaldev.s3.amazonaws.com/i/04llst9rwf6pqqfyrale.png)


If you use the `preview` key, you won’t have to publish to see the changes you’ve made show up. If you use the `fetch` key, make sure you’ve published any content you wish to see changed.

#### How it Works 

The Gatsby Source Plugin downloads all the Pages on the Agility CMS Sitemap, as well as any Shared Content that’s referenced on the `sharedContent` property in the `gatsby-config.js` file. 

All of those pages and content are then made available in GraphQL to the React Components you will write to render those pages. 

Check out the component called “Jumbotron”. This is an example of how to display a styled heading and sub-heading with content that comes from Agility CMS. Here is the Module that provides this content being edited in the Agility CMS Content Manager:

![Agility CMS - Example Module - Jumbotron](https://thepracticaldev.s3.amazonaws.com/i/qc31nokfhe00z4jfnzrj.png)


And here is the code used to render it. Notice that the `title` and `subTitle` fields are available as properties of the `item.fields `object.

*src/modules/Jumbotron.js*
```javascript 
Copysrc/modules/Jumbotron.js: copy code to clipboard

import React, { Component } from "react"

import { graphql, StaticQuery } from "gatsby"

import "./Jumbotron.css"

export default class Jumbotron extends Component {

 render() {

   return (

     <section className="jumbotron">

       <h1>{this.props.item.fields.title}</h1>

       <h2>{this.props.item.fields.subTitle}</h2>

     </section>  
   )
 }  
}
```

When you add new modules and content definitions to Agility CMS, the components used to render those modules will automatically receive the strongly typed data delivered to those modules as props.  

### Ending Notes 

Have you tried using Gatsby with Agility CMS? We’d love to hear your thoughts and firsthand experiences. Join the conversation via our [Slack community](https://agilitycms-community.slack.com/join/shared_invite/enQtNzI2NDc3MzU4Njc2LWI2OTNjZTI3ZGY1NWRiNTYzNmEyNmI0MGZlZTRkYzI3NmRjNzkxYmI5YTZjNTg2ZTk4NGUzNjg5NzY3OWViZGI) or follow us on [social media](https://twitter.com/AgilityCMS) and share them with us! If you have any questions, we’d invite you to [reach out to us](https://agilitycms.com/contact-us?source=gatsbyblog) as well and stay tuned for lots of other awesome stuff we have in store using Gatsby!