Making Modifications to a Gatsby Site

Welcome to my blog ππ½
I'm a Front-End Developer with a passion for learning!
I write about Programming π©π½βπ» and Productivity Tips β
Search for a command to run...

Welcome to my blog ππ½
I'm a Front-End Developer with a passion for learning!
I write about Programming π©π½βπ» and Productivity Tips β
Thank you π
In this series, I will cover the basics of Gatsby, along with some core concepts. At the end of this series, you will learn how to deploy a Gatsby site using Netlify.
In this article, I will explain how to take a Gatsby site and go "live" by deploying it with Netlify. If you're new to Gatsby and haven't read my previous articles, feel free to check out my "Getting Started with Gatsby" series for further informatio...
Although my experience is primarily with front-end development, my current job is affording me the opportunity to do more full-stack work. Naturally, this meant that I needed to explore the different database GUIs available on the market. Here is a l...

A Note on the Series: Fast Fridays π is a series where you will find fast, short, and sweet tips/hacks that you may or may not be aware of. I will try to provide these (fairly) regularly on Fridays. In this Fast Friday tip, I'll explain how to crea...

Over the past year, Iβve started incorporating a Git GUI into my workflow. I still reach for the terminal about 90% of the time, but Iβve found that a GUI can really shine when it comes to things like viewing diffs, checking stashes, or resolving mer...

Simplifying Branch Management with Git Worktree

With all the AI tools popping up these days, I figured I'd try out some speech-to-text software in an attempt to boost my productivity. I've been hearing a lot about βdeveloping at the speed of thoughtβ, so here's a list of the top speech-to-text sof...

π£ This is the second article in my "Getting Started with Gatsby" series. This post will describe how to structure your projects and demonstrate how you can modify a starter to suit your needs. In case you missed it, check out the first article in this series for an introduction to Gatsby.
The repository containing the project files used throughout this tutorial can be found here.
For most websites, you need to navigate to separate pages within your site. Any React component defined in src/pages/*.js will automatically become a page.
As an example, let's say we want to create a new Blog page:
blog.js.blog.js file to the src/pages directory π<Blog /> component so that we have something to render when we visit the page.
Now that we have created a new "Blog" page, you should have a page accessible at http://localhost:8000/blog/.

The Gatsby
<Link />component is for linking between pages within your site. For external links to pages not handled by your Gatsby site, use the regular HTML<a>tag. -Gatsby Docs
Now that we know how to create pages, let's learn how to link between those pages. You can easily link between pages via the Link Component.
Steps
<Link /> component into the src/pages/index.js file.import { Link } from "gatsby"
<Link /> component to the index.js file.blog.js file, you can add the <Link /> component to index.js and point it to the new "Blog" page. <Link to="/blog/">View Blog Posts</Link>
blog.js.<Link to="/">Home</Link>

Layout components are for sections of your site that you want to share across multiple pages. For example, sites will commonly have a layout component with a shared header and footer. Other common things to add to layouts are a sidebar and/or navigation menu. -Gatsby Docs
It's recommended to add your Layout component to the /components/ folder with the rest of your components.
Add layout.js in the src/components/ folder.
Create a <Layout /> component and add the code that you want to be accessible across other pages.
//Sample Layout Component - src/components/layout π
import React from "react"
const Layout = (props) => {
return (
<div>
<nav className="navigation-bar">
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#blog">Blog</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
<main>{children}</main>
<footer>
Β© {new Date().getFullYear()}, Built with
{` `}
<a href="https://www.gatsbyjs.com">Gatsby</a>
</footer>
</div>
)
}
export default Layout;
3.Once you have added your code to layout.js, import the component into index.js.
<Layout /> component and wrap your page in it. A <Layout /> component can be applied to any page and allows you to easily access commonly used elements, like navigation menus, across multiple pages. //Adding Layout component to the main page - src/pages/index.js π
import React from "react"
import Layout from "../components/layout"
function Home() {
return (
<Layout>
<h1>This page is is wrapped in a Layout component</h1>
</Layout>
);
}
export default Home;
Most sites built with Gatsby use some combination of plugins. Check out the Gatsby Plugin Library for an exhaustive list of plugins to choose from.
Installing and configuring are the two primary steps when it comes to adding plugins to your site. To demonstrate how to add plugins to a Gatsby site, I will go through the process of configuring the gatsby-plugin-google-analytics.
npm install gatsby-plugin-google-analytics
After installing, you need to edit the gatsby-config file and add the plugin that you just installed.
gatsby-config.js is a special file that Gatsby looks for. This file contains plugin information and configuration, your site's metadata, and other miscellaneous configurations.
The gatsby-config.js file should be placed at the root level of your project.
gatsby new command, then you should already have a stub of this already included in your project. Most starters utilize this command so it's highly likely that you will have this file readily available in your project.gatsby-config.jsMany plugins have optional or required options to configure them. Instead of adding a name string to the
pluginsarray, add an object with its name and options. As an example, view the list of options forgatsby-plugin-google-analytics
Plugins can either be set with or without options.
If a plugin does not require options, you can simply add it to the array like so:
module.exports = {
plugins: [`gatsby-plugin-react-helmet`],
}
If a plugin does accept options, you can add it to the array like this:
module.exports = {
plugins: [
`gatsby-plugin-react-helmet`, //Plugin w/ no options
{
//Plugin with options
resolve: `gatsby-plugin-google-analytics`,
options: {
trackingId: `ADD YOUR TRACKING ID HERE`,
},
},
]
}
Gatsby is unopinionated when it comes to styling your site. You can use plain ol' CSS, CSS Modules for a scope-based CSS option, or go with CSS-in-JS library like styled-components. I personally use Tailwind CSS on my website but you can use whatever suits your fancy!
How to add global styles:
Add a global.css file. It's a common pattern to create a /styles directory that houses the sites CSS files, but Gatsby does not enforce this structure in order to work.
Create a gatsby-browser.js file at the root level of your project.
Import the global.css file into gatsby-browser.js
Example - Adding global style of background-color: lightblue to body tag
We learned the building blocks needed to style and modify a Gatsby site to suit your needs. In the next article, we'll take this one step further and I'll show you how to take a Gatsby site and deploy it using Netlify.
If you enjoy what you read, feel free to like this article or subscribe to my newsletter, where I write about programming and productivity tips.
Thanks for reading and stay tuned for the next one!