Khaled Garbaya

Engineering Leader, Software Developer & Educator

Moving from create-react-app to Gatsby.js

Man moving boxes

create-react-app is a build cli, it helps you bootstrap a new react app without the need to configure tools. Like Webpack or Babel.

They are preconfigured and hidden so that you can focus on the code.

If you came across gatsby you will notice that there is a lot of similarity between them. In this Blog post I will explain the key difference between the two.

What is Gatsby?

gatsby-logo

Gatsby is a blazing fast static site generator for React. Actually, it is more than that. Think of it as a PWA (Progressive Web App) Framework with best practices backed in. For example: you get code and data splitting out-of-the-box.

Why Moving to Gatsby?

tools-picture

Gatsby.js let's use modern web stack without the setup headache. With its flexible plugin system it let's you bring your own data source. Like Contentful, Databases or your filesystem.

When you build your Gatsby.js website you will end up with static files. They are easy to deploy on a lot of services like Netlify, Amazon S3 and more.

Gatsby.js provides code and data splitting out-of-the-box. It loads first your critical HTML and CSS. Once that loaded it prefetches resources for other pages. That way clicking around feels so fast.

Gatsby.js uses React component as a view layer so you can share and reuse them across pages/projects. Once it loads the page's javascript code, your website becomes a full React app.

Gatsby.js uses GraphQL to share data across pages. You only get the data you need in the page. At build time Gatsby will resolve the query and embed it in your page.

Gatsby.js project folder structure

1 2├── LICENSE 3├── README.md 4├── gatsby-config.js 5├── gatsby-node.js 6├── node_modules 7├── package-lock.json 8├── package.json 9├── src 10│   ├── layouts 11│   ├── pages 12│   └── templates 13└── static 14 15

From React Routes to Gatsby Pages

road-601871 1280

There are 2 types of routes, static when you know all the part that will define your route like /home. And dynamic  when part of your route is only know at runtime like blog/:slug

Let's assume you have the following static routes in our create-react-app project:

1 2<Route exact path='/' component={Home}/> 3<Route path='/blog' component={Blog}/> 4<Route path='/contact' component={Contact}/> 5 6

In Gatsby.js, to have these routes you need to create a component with the name like the route path in the pages folder. It create the routes for you. The good news is the react components are already created so it is a matter of copying/pasting them. Except for the home page you need to name it index.js.  You will end up with something like this

1 2├── LICENSE 3├── README.md 4├── gatsby-config.js 5├── gatsby-node.js 6├── node_modules 7├── package-lock.json 8├── package.json 9├── src 10│   ├── layouts 11│   ├── pages 12│   │    ├──  index.js 13│   │    ├──  blog.js 14│   │    ├──  contact.js 15│   └── templates 16└── static 17 18

Now that you converted your static routes let's tackle the dynamic routes.

I will take an example of blog posts in this case loaded from Contentful. Every blog post has a uniq slug used to load its content.

In a normal react app the route will look something like this.

1 2<Route path='/blog/:slug' component={BlogPost}/> 3 4

And your BlogPost component will look something like this:

1// a function that request a blog post from the Contentful's API 2import { getBlogPost } from './contentful-service' 3import marked from 'marked' 4 5class BlogPost extends Component { 6 7 constructor(...args) { 8 super(args) 9 this.state = { status: 'loading', data: null } 10 } 11 componentDidMount() { 12 getBlogPost(this.props.match.slug) 13 .then((data) => this.setState({ data })) 14 .catch((error) => this.setState({ state: 'error' })) 15 } 16 render() { 17 if (!this.state.status === 'error') { 18 return <div>Sorry, but the blog post was not found</div> 19 } 20 return ( 21 <div> 22 <h1>{this.state.data.title}</h1> 23 <div dangerouslySetInnerHTML={{ __html: marked(this.state.data.content) }} /> 24 </div> 25 ) 26 } 27} 28 29

To create pages dynamically in Gatsby.js you need to write some logic in the gatsby-node.js  file. To get an idea on what is possible to do at build time checkout the Gatsb.js Node.js API docs.

We will use the createPages function.

Following out Contentful example we need to create a page for each article. To do that first we need to get a list of all blog posts and create pages for them based on their uniq slug.

The code will look like this:

1const path = require("path"); 2 3exports.createPages = ({ graphql, boundActionCreators }) => { 4 const { createPage } = boundActionCreators 5 return new Promise((resolve, reject) => { 6 const blogPostTemplate = path.resolve(`src/templates/blog-post.js`) 7 // Query for markdown nodes to use in creating pages. 8 resolve( 9 graphql( 10 ` 11 { 12 allContentfulBlogPost(limit: 1000) { 13 edges { 14 node { 15 slug 16 } 17 } 18 } 19 } 20 ` 21 ).then(result => { 22 if (result.errors) { 23 reject(result.errors) 24 } 25 26 // Create blog post pages. 27 result.data.allContentfulBlogPost.edges.forEach(edge => { 28 createPage({ 29 path: `${edge.node.slug}`, // required 30 component: blogPostTemplate, 31 context: { 32 slug: edge.node.slug // in react this will be the `:slug` part 33 }, 34 }) 35 }) 36 37 return 38 }) 39 ) 40 }) 41} 42

Since you already have the BlogPost component, form your react project. Move it to src/template/blog-post.js.

Your Gatbsy project will look like this:

1 2├── LICENSE 3├── README.md 4├── gatsby-config.js 5├── gatsby-node.js 6├── node_modules 7├── package-lock.json 8├── package.json 9├── src 10│   ├── layouts 11│   ├── pages 12│   │    ├──  index.js 13│   │    ├──  blog.js 14│   │    ├──  contact.js 15│   └── templates 16│   │    ├──  blog-post.js 17└── static 18 19

You need to make some slight modification to your Blogpost component.

1 2import React from "react"; 3 4class BlogPost extends React.Component { 5 render() { 6 const post = this.props.data.contentfulBlogPost; 7 8 return ( 9 <div> 10 <h1>{post.title}</h1> 11 <div dangerouslySetInnerHTML={{ __html: post.content.childMarkdownRemark.html }} /> 12 </div> 13 ); 14 } 15} 16 17export default BlogPost 18 19export const pageQuery = graphql` 20 query BlogPostBySlug($slug: String!) { 21   contentfulBlogPost(fields: { slug: { eq: $slug } }) { 22     title 23 24      content { 25 26        childMarkdownRemark { 27 28          html 29 30       } 31 32      } 33   } 34 } 35` 36 37

note the $slug part that's passed through the context when creating the page to be able to use it in the GraphQL query.

Gatsby.js will pick the exported pageQuery const and will know it's a GraphQL query string by the graphql tag.

From the React state to GraphQL

files-1614223 1280

I will not go in depth with how to manage a React state since there is a lot of ways to achieve that. There is the new React 16 Context API or using Redux etc... Using Gatsby.js you can request the data you need using the GraphQL data layer as shown in the previous example. this option is only available in the root components. This will change in v2 using static queries feature. You can still use Redux with Gatsby.js depends on your use if GraphQL is not enough.

Deployment

server-2160321 1280

Since Gatsby.js builds "static" files you can host them on tons of services. One of my favourites is Netlify. There is also AWS S3  and more.

Resources

Next one in your inbox

no spam only content