In order to help you put into practice what you're learning, we will be building a project together. Throughout this book, we will be working to build a personal portfolio, something that every developer needs and therefore something I think will be relevant for most readers. The portfolio will contain blog pages to aid your learning in public, project pages to demonstrate your work, a stats page showcasing interesting metrics on your site, as well as many more features that will help your portfolio stand out from the crowd.
Throughout this book, you will be faced with options. We will discuss different implementations for styling your site, as well as data sources you may want to implement. This should give you the flexibility to align it with your current knowledge. Alternatively, you can throw yourself in the deep end – the choice is up to you. Everywhere there is a choice, I will also provide my personal recommendation for what might be best if you can't decide.
To see a finished version of the portfolio we will be building, visit this link:
https://elevating-react-with-gatsby.sld.codes/
Tip
Refer to the code repository (https://github.com/PacktPublishing/Elevating-React-Web-Development-with-Gatsby-4) that accompanies this book if you're struggling at any point. It includes a copy of the project as it should appear after every chapter.
To start using Gatsby, we need to ensure we have a few prerequisite tools set up on our machines. Most of these prerequisites are most likely already on your device if you are a React developer, although I would still encourage you to read through this list, as some of your tools may need an update.
Node.js version 14.15.0+
As of version 4.0, Gatsby supports all Node.js versions greater than 14.15.0. You can quickly check if you have Node.js installed by opening up a terminal window and typing the following:
node -v
If you have Node.js installed, this should print a version number. However, if you receive an error, you can download Node.js by navigating to the Node.js website (https://nodejs.org). Node.js comes bundled with npm
, a package repository, package manager, and command-line tool that we will be using to install Gatsby.
Tip
You're most likely already using Node.js, and some of your pre-existing projects may require a different version than the requirements specified here. If you need to manage multiple versions of Node.js on the same device, you should check out the Node.js Version Manager (NVM)(https://github.com/nvm-sh/nvm). It gives you access to valuable commands, including installing new versions and switching between minor and major versions of Node.js.
Gatsby command-line interface
The Gatsby Command-Line Interface (CLI) is a tool built by the core Gatsby team; it allows you to perform standard functions, such as creating new Gatsby projects, setting up local development servers, and building your production site. Although you can use it on a per-project basis, it is far more common to install the CLI globally so that you can use its features across multiple Gatsby projects without having to install it as a package in each project – got to save that hard-drive space!
To install the CLI globally, npm install
it with the global flag:
npm i -g gatsby-cli
To verify its installation, open up a terminal window and type the following:
gatsby --help
If running this provides a list of commands and does not error out, then you're good to go.
Important Note
Throughout this book, I use npm
as my package manager. If you prefer Yarn, you can use the Yarn equivalent commands.
Directory and package setup
Here, we will begin to create the files and folders we need to start our project, as well as install necessary dependencies such as React and Gatsby.
First, create a folder to house our project. You can call it whatever you like. Throughout this book, I will refer to this folder as the root
folder of the application. Open a terminal and navigate to your root
folder. Initialize a new package in this folder by running the following:
npm init -y
With the package now initialized, let's install React and Gatsby:
npm i gatsby react react-dom
Open your root
folder in your favorite Integrated Development Environment (IDE). You should notice that it now contains three new items, package.json
, package-lock.json
, and a node-modules
folder. Opening your package.json
, you should see the following:
{
"name": "gatsby-site",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"gatsby": "^4.4.0",
"react": "^17.0.2",
"react-dom": "^17.0.2"
}
}
In the preceding example, you can see that this file now contains references to the dependencies we have just installed.
Development scripts
Let's start by modifying package.json
so that it contains some useful scripts that will speed up our development process:
{
"name": "gatsby-site",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "gatsby build",
"develop": "gatsby develop",
"start": "npm run develop",
"serve": "gatsby serve",
"clean": "gatsby clean"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"gatsby": "^4.4.0",
"react": "^17.0.2",
"react-dom": "^17.0.2"
}
}
Let's break down these scripts:
build
: Runs the Gatsby CLI's build
command. This creates a compiled, production-ready build of our site. We will learn more about this in Chapter 9, Deployment and Hosting.
develop
: Runs the Gatsby CLI's develop
command. We will review it in detail in the next section, Creating your first few pages.
start
: The start
script redirects to the develop
script. This is in place as it is common to start packages with a start
script.
serve
: Runs the Gatsby CLI's serve
command to serve up a Gatsby build
folder. This is a useful way to review a production build.
clean
: The clean
script utilizes the Gatsby CLI's clean
command. This deletes the local Gatsby cache and any build data. It will be rebuilt with the next develop
or build
command.
All of these scripts can be run from the root
folder with the following command:
npm run script-name
Simply replace script-name
with the name of the script you would like to run.
You'll notice the absence of a test script. Don't worry – we will get into how to test a Gatsby application in Chapter 7, Testing and Auditing Your Site.
Framework files and folders
As mentioned, Gatsby is a framework. Frameworks require certain files to exist in order to work. Let's set up our project with the files and folders where Gatsby expects to find them.
Create a gatsby-config.js
file in your root
directory and add the following:
module.exports = {
plugins: [],
};
As the name might suggest, the gatsby-config.js
file is the core configuration file for Gatsby. We will be coming back to this file frequently as we build out our project. By the time we are done with it, it will be full of plugins, metadata, styling, and even offline support.
Create gatsby-browser.js
and gatsby-node.js
files in your root
directory. Both of these files can be left blank for now. The gatsby-browser.js
file contains any code we would like to run on the client's browser. In the next chapter, we will be using this file to add styles to our website. The gatsby-node.js
file contains code we would like to run during the process of building our site.
Finally, create an src
folder in your root
directory. This folder will contain the majority of our development work, much like in a traditional React application. Pages we create and components we define will all be contained within this folder.
Before we go any further, let's make sure we have our version control tracking the right files.
Using version control
I suspect many of you would like to use version control while you build out your Gatsby site. To ensure Git tracks only the files that matter, create a .gitignore
file and add the following:
node_modules/
.cache/
public
These lines stop our dependencies, Gatsby builds, and cache folders from being tracked.
Creating your first few pages
We now have all the underlying code we need set up to allow us to start creating pages. In this section, we will create a three-page website using Gatsby. It's important to note that this is a basic example purely designed to solidify your understanding of how Gatsby works before we worry about styling and additional functionality.
Navigate to your src
directory and create a new folder called pages
. Any JS files we create within the pages
folder will be treated as a route by Gatsby. This also applies to subfolders within the pages
folder. There is, however, one exception – files called index.js
are treated as the root of their directory. Let's make sense of this with a few examples:
src/pages/index.js
will map to yourwebsite.com.
src/pages/about.js
will map to yourwebsite.com/about.
src/pages/blog/my-first-post.js
will map to yourwebsite.com/docs/my-first-post. While we won't be setting up a page at this URL now, we will start using routes such as this one in Chapter 3, Sourcing and Querying Data (from Anywhere!).
src/pages/404.js
will map to any page that does not resolve on yourwebsite.com.Important Note
Any React components you place in the pages
folder will become navigable routes on your site. As such, it is best to separate your components from your pages. A common pattern is to create a components
folder that sits next to your pages
folder in the src
directory and import components you want to use in your pages.
The index page
Create an index.js
file in your pages
folder. As the index of the pages
folder, this will become the landing of your website. We can now populate this file with the following code:
import React from "react"
const Index = () => {
return (
<div>
<h1>My Landing Page</h1>
<p>This is my landing page.</p>
</div>
)
}
export default Index
The contents of this file should look familiar; it's just a simple stateless ReactJS component.
We could have also defined it as:
import React from "react"
export default function Index(){
return (
<div>
<h1>My Landing Page</h1>
<p>This is my landing page.</p>
</div>
)
}
Both examples will output the exact same result, so it's just personal preference.
The about page
In a similar fashion, we can create an about
page. Here, you have a choice – you can either create this page at src/pages/about.js
or at src/pages/about/index.js
. The question I always ask myself when deciding which option to go with is whether the page will have sub-pages. In the case of an about
page, I think it's unlikely to contain any sub-pages, so I will opt for src/pages/about.js
:
import React from "react"
export default function About(){
return (
<div>
<h1>My About Page</h1>
<p>This is a sentence about me.</p>
</div>
)
}
Here, we have defined another simple React component containing a heading and paragraph to create our about
page.
The 404 page
Gatsby expects to find a 404.js
file in your pages
directory. This page is special. It contains the page that will be shown when Gatsby cannot find a page that was requested. I am sure you have come across "Page not found" pages before. Without this page, on requesting a non-existent route, the browser will not find any resource and show a browser error to the user. While the 404
page is another form of displaying the same error, by creating this page, we can manage the error ourselves. We can link to working pages on our site or even suggest the page they might have been trying to visit (more on this in Chapter 3, Sourcing and Querying Data (from Anywhere!)).
Let's create our 404
page now in src/pages/404.js
:
import React from "react"
export default function NotFound(){
return (
<div>
<h1>Oh no!</h1>
<p>The page you were looking for does not
exist.</p>
</div>
)
}
You should be starting to see a pattern. Creating pages is as simple as defining React components – something you should be familiar with already.
Trying the develop command
At this point, you've actually already created a fully working website. Congratulations! To test it out, open a terminal at your root
directory and run the following:
npm run start
As you will recall from our package.json
, this will run the gatsby develop
command. This will take a few seconds to run, but you should then see some terminal output that looks like this:
You can now view gatsby-site in the browser.
http://localhost:8000/
You can now open a browser of your choice and navigate to http://localhost:8000/
, and you should be greeted with something like this:
Figure 1.5 – The landing page preview
This is the rendered version of our index.js
page component. You can modify the URL in your browser to http://localhost:8000/about
to see your about
page and http://localhost:8000/404
to see your 404
page. You can also see your 404
page in development by navigating to any invalid route and pressing the Preview custom 404 page button.
Tip
If you don't want to manually navigate to the browser and type in the URL, you can modify our scripts by appending the gatsby develop
command with the -o
option. This instructs Gatsby to open your default browser and navigate to the site automatically when you run the develop
command.
gatsby develop in detail
Running gatsby develop
starts the Gatsby development server. This might be a little confusing, as we have previously mentioned how a Gatsby site is delivered as static content, but it's actually there to speed up your development process.
Imagine your site contains 10,000 pages; building the entirety of your site every time you make a small change to one page would take a long time. To get around this in development, Gatsby uses a Node.js server to build only what you need as and when it's requested. Due to it building on demand, it can negatively affect the performance of a page and you should never test performance on a page in development for this reason.
Once the server is up, you can continue to edit your code without rerunning the command. The development server supports hot reloading, a concept that should be familiar to you.
The develop
command has a number of built-in options that allow you to customize it:
-H, --host
: Allows you to modify the host
-p, --port
: Allows you to modify the port Gatsby runs on
-o, --open
: Opens your project in the browser
-S, --https
: Turns on HTTPS
You can view your site on any device connected to the same network by using the host option. This can be useful when you want to compare how your site behaves on mobile browsers with that of a desktop experience. To achieve this, run the following command:
gatsby develop -H 0.0.0.0
If the command is successful, you will see a subtle difference in the output:
You can now view gatsby-site in the browser.
Local: http://localhost:8000/
On Your Network: http://192.168.1.14:8000/
The develop
command has added a URL for testing on your network. Typing this into a browser on any device connected to the same network will render your site.
Connecting your pages
Now that you have multiple pages, you may want to navigate between them. There are two different ways of achieving this – with the Gatsby Link component or via programmatic navigation. To some of you, these components and functions may sound familiar; this is because Gatsby wraps the reach-router
(https://reach.tech/router) library for navigation. For those who haven't used reach-router
before, the library comes with support for server-side rendering and routing accessibility functionality built in. Gatsby has built on and enhanced this functionality to meet its high standards for user accessibility, ensuring a great website experience regardless of who you are.
The Gatsby Link component
It's important to use the Gatsby <Link/>
component as a replacement for the <a/>
tag whenever you are linking to a page that is internal. The <Link/>
component works just like an <a/>
tag, with one important distinction – it enables prefetching. Prefetching is the act of loading a resource before it is required. This means that when the resource is requested, the time waiting for that resource is decreased. By prefetching the links on your page, your next click navigates to content that is already loaded and is therefore practically instant. This is particularly noticeable on mobile devices in areas with reduced network conditions that would normally have a delay when loading pages.
The first place you could add a Link
component is to your 404
page. It's common for these pages to have a button that says something like "Take me home" that, when clicked, navigates to the landing page:
import React from "react"
import {Link} from "gatsby"
export default function NotFound(){
return (
<div>
<h1>Oh no!</h1>
<p>The page you were looking for does not
exist.</p>
<Link to="/">Take me home</Link>
</div>
)
}
As you can see in the preceding code block, the Link
component has a prop called to
; this needs to be passed to the page that you want to navigate to relative to the root of your website. By passing the "/"
prop, Gatsby will navigate to the root of your website.
You can also add a link to the about
page from the index
page:
import React from "react"
import {Link} from "gatsby"
export default function Index() => {
return (
<div>
<h1>My Landing Page</h1>
<p>This is my landing page.</p>
<Link to="/about">About Me</Link>
</div>
)
}
You can see here that we instead pass "/about"
to the to
prop in the <Link/>
component; this will navigate to our previously created about
page.
Programmatic navigation
Occasionally, you may need to trigger navigation with something other than a click. Perhaps you need to navigate as a result of a fetch
request, or when a user submits a form. You can achieve this behavior by making use of the Gatsby navigate
function:
import React from "react"
import {navigate} from "gatsby"
export default function SomePage() => {
const triggerNavigation = () => {
navigate('/about')
}
return (
<div>
<p>Triggering page navigation via onClick.</p>
<button onClick={()=> triggerNavigation()}>
About Me
</button>
</div>
)
}
Like the <Link/>
component, the navigate
function will only work for navigating to internal pages.
We now have a basic Gatsby site set up with the ability to navigate between pages.