Create a blog with Netlify CMS and Gatsby
If you love the speed and developer experience of the Jamstack, this guide’s for you. We’ll combine the intuitive content editing of Netlify CMS with the React-powered framework, Gatsby, to build a blazing-fast blog. If you’re interested in learning more about Netlify’s CMS, click here for our deep dive.
Prerequisites
- Basic familiarity with JavaScript, React, and Git
- A Node.js installation (https://nodejs.org/)
- A GitHub account
- A Netlify account
Part 1: Project Setup
- Install Gatsby CLI:
- Bash
- npm install -g gatsby-cli
- Use code with caution.
- content_copy
- Create a Gatsby Project:
- Bash
- gatsby new my-awesome-blog
- cd my-awesome-blog
- Install Netlify CMS Dependencies:
- Bash
- npm install netlify-cms-app gatsby-plugin-netlify-cms
Part 2: Netlify CMS Configuration
- Create a Configuration File (config.yml)
- Create a file named static/admin/config.yml. Here’s a basic example:
- YAML
- backend:
- name: git-gateway
- branch: main
- media_folder: static/img
- public_folder: /img
- collections:
- – name: “blog”
- label: “Blog Posts”
- folder: “src/posts”
- create: true
- slug: “{{year}}-{{month}}-{{day}}-{{slug}}”
- fields:
- – { label: “Title”, name: “title”, widget: “string” }
- – { label: “Publish Date”, name: “date”, widget: “datetime” }
- – { label: “Body”, name: “body”, widget: “markdown” }
- Important Notes:
-
- Customize carefully: Adjust the media_folder, public_folder, collections, and fields to match your desired blog structure.
- More field types: Netlify CMS supports images, lists, and more: (https://www.netlifycms.org/docs/widgets/)
- Update gatsby-config.js:
- JavaScript
- module.exports = {
- // … existing config
- plugins: [
- // … other plugins,
- ‘gatsby-plugin-netlify-cms’
- ]
- }
Part 3: Creating Blog Post Templates
- Create a Template: Create a file like src/templates/blog-post.js:
- JavaScript
- import React from ‘react’
- import { graphql } from ‘gatsby’
- const BlogPost = ({ data }) => {
- const post = data.markdownRemark
- return (
- <div>
- <h1>{post.frontmatter.title}</h1>
- <div dangerouslySetInnerHTML={{ __html: post.html }} />
- </div>
- )
- }
- export const query = graphql`
- query($slug: String!) {
- markdownRemark(fields: { slug: { eq: $slug } }) {
- html
- frontmatter {
- title
- date
- }
- }
- }
- `
Part 4: Git and Netlify Integration
- Initialize Git and Push to GitHub:
- Bash
- git init
- git add .
- git commit -m “Initial commit”
- # Follow instructions to create a new GitHub repo and push
- Deploy to Netlify:
-
- Connect your GitHub repo to Netlify.
- Set the build command to gatsby build
Part 5: Accessing the CMS and Writing
- Access the CMS: Your CMS will be at https://your-site-url.netlify.app/admin/
- Authenticate: Log in with your GitHub credentials.
- Start Blogging: Click “New Blog Posts” and get writing!
Recent Posts