Gatsby
Getting Started with Gatsby.js
Updated: 03 September 2023
Introduction
Gatsby.js is a Static Site Generator that makes use of React and can plug into a headless CMS to generate a Static Site with SPA support and functionality
I’m using the Gatsby.js Tutorial from the documentation
Prerequisites
- Node.js
- Git
- Gatsby CLI
- Optional: Yarn
To install the Gatsby CLI you can use npm
:
Or if using yarn
:
To view the Gatsby help menu once installed:
Create New Site
To create a new Gatsby.js site run:
Where gatsby-hello-world
is the name of the new directory for the site and will clone from the Gatsby GtiHub template
Next cd gatsby-hello-world
and run the following command to start development server you can use the gatsby-cli
Or npm
:
Or
You should then be able to launch the site on http://localhost:8000/
Looking at the initial site you should see the following files:
In the index.js
file you will see a simple React Component that is exported:
Editing this will live update the page as you edit and save the file, this uses HMR in the background and will update you browse live
Create a New Page
Gatsby organises pages similar to the way you would if you were using normal HTML instead. Inside of the pages
directory you can create an about.js
file with something like:
pages/about.js
And then we can add a link to this from the home
component using the React Link
component
pages/index.js
Clicking on the Link
on the index.js
page will take you to the about.js
page
Build the Site
To build the initial site you can just run
Or
Or
You can then simply deploy the public
directory using your preferred method
Adding Styles
To add styles we first need to create a src/styles/global.css
file, this will contain all the global CSS for our application - we can add some basic content to it to start off
global.css
Next in the src
project root directory create a file called gatsby-browser.js
, this is one of a few standard files that Gatsby uses. In this file import the global.css
file we just created with:
gatsby-browser.js
After adding this file you will need to restart the Gatsby development server
Now let’s create a component called Container
:
src/components/container.js
This file imports css file in the same directory called container.module.css
which is a CSS Module which means that the styles will be scoped to this component. We also use containerStyles.container
to apply the .container
class to the main element
container.module.css
We can then update the index.js
page to use this container:
index.js
Plugins
Using plugins in Gatsby involves three steps:
- Installing the plugin. For example we can install the
typography
plugin with:
Or
- Configuring the plugin which is done using the gatsby-config.js` file and a configuration file for the relevant plugin
gatsby-config.js
src/utils/typography.js
If you inspect the output HTML now after running gatsby develop
you should see some styles in the head
which are as a result of the typography
plugin, the generated styles will look like:
Data
The Gatsby Data Layer is a feature of Gatsby that enables you to build sites using a variety of CMSs
For the purpose of Gatsby, Data is anything that lives outside of a React component
Gatsby primarily makes use of GraphQL to load data into components however there are other data sources that can be used as well as custom plugins that can be used or custom written for this purpose
Common Site Metadata
The place for common site data, such as the site title is the gatsby-config.js
file, we can put this in the siteMetadata
object like so:
gatsby-config.js
We can then query for the data by using the GraphQL query constant that we export on a Page Component which states the data
required for the page itself
index.js
Other components can make use of the useStaticQuery
hook, we can import it from gatsby
Let’s add the a simple static query for the title
in the container
component
container.js
We can then use this in our component
Source Plugins
Source plugins are how we pull data into our site, Gatsby comes with a tool called GraphiQL
which can be accessed at http://localhost:8000/___graphql
when the development server is running
We can write a query to get the title
using the GraphiQL
UI:
Filesystem Plugin
We can access data from the File System using the gatsby-source-filesystem
And then in the gatsby-config.js
file:
If we restart the dev server we should see the allFile
and file
in the GraphiQL interface
We can then query for some data from the file system and log it to the console:
We can then build a simple table with the data:
Transformers
Transformers are used by Gatsby to transform the data that is read in, we can use the following transformer to transform markdown
gatsby-config.js
We can then use the remark
plugin combined with the GraphQL query to get markdown content from files in our application
In the above query the result will be the rendered html
node along with any metadata, for example in the file below:
src/pages/article-1.md
We can then use the query from above to create a page that lists all the markdown content we have in the site:
src/pages/blog.js
Create Pages Programatically
Using Gatsby we can create pages using the data output from a query
Generate Page Slugs
We can make use of the onCreateNode
and createPages
API’s that Gatsby exposes. To implement an API we need to export the function in the gatsby-node.js
file
The onCreateNode
function is run every time a new node is created or updated
We can add the following into the gatsby-node.js
file and can see each node that has been created
gatsby-node.js
We can then check when a node is the MarkdownRemark
and use the gatsby-source-filesystem
plugin to generate a slug for the file
Using the above, we can update a node with the createNodeField
function which is part of the actions
object that’s passed into the onCreateNode
field
We can then run the following query in the GraphiQL editor to see the slugs that were generated
Gatsby uses the createPages
API from plugins to create pages, we can additionally export the createPages
function from our gatsby-node.js
file. To create a page programatically we need to:
- Query the data
gatsby-node.js
- Map the query resilt to a page
We can first update the createPages
function to set the slug route to resolve to a specific component, in this case the src/templates/blog-post.js
We can then create the src/templates/blog-post.js
file to render the new data:
You should be able to view any created pages by navigating to a random route on your site which should open the development server’s 404 page which has a listing of the available pages
We can then also update the blog.js
file to query for the slug and create a Link
to the new page based on the slug
blog.js