Navigate back to the homepage

GatsbyJS for Dummies

Sudaraka Jayathilaka
April 10th, 2024 · 4 min read

Recently I wanted to revamp my personal website. While in the quest for the Ideal template, I came found this super clean-looking template by, Narative called, Novela.

But it seemed the theme dev stopped somewhere in 2019 and they have taken the repository offline with only leaving only an npm package. I really wanted to build my personal website/blog using this theme, but again,I didn’t want to base my blog on an unmaintained Gatsby theme from NPM (which) doesn’t have a public repository.

So I took matters into my own hands and created my own fork and ported it to the latest React and Gatsby versions. While doing this, I learned a lot about GatsbyJS and how it works. So I thought of sharing my experience with you all.

What is GatsbyJS?

GatsbyJS is a framework for building static websites. Gatsby is powered by several popular technoliges like React, graphql and webpack. In essence, GatsbyJS lets you provide data in several formats and let’s you render websites using the data as static websites. The journey starts with data sourcing.

Data Sources

source-plugins

GatsbyJS allows you to provide data in several different formats such as,

  • Markdown Files
  • MDX Files
  • Via CMS like,
    • Contentful
    • WordPress
    • Drupal

I just mentioned a few. Not only that, Gatsby simply allows you to create your own source plugin as well and has a very good documentation on how to do it.

Once you configure a source plugin, GatsbyJS sources the data provided and exposes the data via GraphQL. This data can be queried by pages for rendering information.

Lifecycle APIs

Once the data is sourced, the data might not be ready for rendering just yet. Gatsby provides multiple lifestyle hooks which gets executed at different stages of the build process. These hooks can be used to transform the data, create new pages, etc.

One example case would be the onCreateNode API which gets executed when a new node is created. This can be used to transform the data before it gets exposed via GraphQL.

Another important hook is the createPages hook which gets executed after the data is sourced and transformed. This hook can be used dynamically to create pages from the sourced data.

The below documentation lists down all the lifecycle hooks available in GatsbyJS and when they get executed.

Rendering Content

Once the data is sourced and transformed, the next step is to render the content. You can use React components to render the content. GatsbyJS provides a way to create pages using React components. When creating pages in the createPage hook, you can provide react components as the template for the page. You can check out the examples in the official documentation.

Plugins

Although I just explained the most basic concepts of Gatsby, there is so much more to GatsbyJS. Apart from all the Gatsby built-in features, there are a lot of plugins available that can be used to extend the functionality of GatsbyJS. You can find the plugins in the official GatsbyJS plugin library. For example, you can use

to use emotion CSS in your Gatsby project which helps adding css in js.

Conclusion

Gatsby is a very powerful tool for building static websites. It has very good documentation and very good community support, not to mention a bunch of plugins are available to extend the functionality. If you are looking to build a static website, GatsbyJS is a very good choice. You can check out my fork of the Novela theme which I used for my personal website below,

More articles from Sudaraka Jayathilaka

Installing OpenJDK on M1 Mac

I recently got hold of a M1 MacBook Pro and faced with the challenge of setting up java developer env. I wanted to build and run a spring…

December 10th, 2021 · 2 min read

Go Embed instead of Packr

Many of the microservices I am working right now, are written in Golang. Most of these microservices have internal dashboards and the UI…

December 6th, 2021 · 2 min read
© 2021–2024 Sudaraka Jayathilaka
Link to $mailto:sudarakayasindu@gmail.comLink to $https://github.com/sudaraka94Link to $https://www.linkedin.com/in/sudarakajayathilaka/Link to $https://x.com/sudaraka94Link to $https://medium.com/@sudarakayasindu