Gatsby
Setting up a Gatsby site with Netlify is a standard and powerful combination for creating high-performance Jamstack websites. This guide covers the complete process, from initial installation to continuous deployment.
1. Local Gatsby Setup and Project Initialization
Gatsby is a React-based framework that requires Node.js and npm (or yarn/pnpm).
1.1. Install the Gatsby CLI
The Gatsby Command Line Interface (CLI) is the easiest way to create and manage Gatsby projects.
- Install Node.js (version 18+ recommended).
- Install the Gatsby CLI globally:
1.2. Create a New Gatsby Site
We will use the default Gatsby starter template for a clean start.
- Create the site: Note: The minimal starter provides a blank slate, ideal for custom development.
1.3. Local Test
Run the development server to check the basic setup.
Your site will be running at http://localhost:8000/
. Press Ctrl+C to stop the server.
2. Custom Layouts, Components, and Assets
Gatsby uses React components for templating. Pages are built from Layout Components that wrap UI Components (Partials).
2.1. Project File/Folder Structure Tree
Your project structure, after customization, will look like this:
my-gatsby-site/
├── node_modules/ # Node.js dependencies
├── public/ # ⚠️ Generated output folder (ignored by Git)
├── src/ # All source code, components, layouts, and pages
│ ├── components/ # Reusable UI components (partials)
│ │ ├── Footer.js # Site footer component
│ │ └── Header.js # Site header/navigation component
│ ├── pages/ # Top-level pages (creates site routes)
│ │ ├── 404.js # Not Found page
│ │ └── index.js # Site homepage (/)
│ └── templates/ # Reusable page wrappers (Layouts)
│ └── Layout.js # Primary site shell, wraps all content
├── static/ # Static assets copied directly to the final output (public/)
│ └── css/
│ └── style.css # Custom CSS file
├── gatsby-config.js # Main site configuration (plugins, metadata)
├── gatsby-node.js # API for programmatically creating pages (e.g., blog posts)
├── package.json # Node.js dependencies and run scripts
└── package-lock.json
2.2. Create the Layout Component
The Layout component serves as the wrapper for most or all pages, defining the common structural elements (header, footer, shared styling).
- Create the Components and Templates directories:
- Create the Main Layout:
- File:
src/templates/Layout.js
- File:
2.3. Create Components (Partials)
Components are reusable UI elements imported into the Layout.
- Create Header Component:
- File:
src/components/Header.js
- File:
- Create Footer Component:
- File:
src/components/Footer.js
- File:
2.4. Apply the Layout
Update your page components to use the new layout.
- Update the Homepage:
- Open
src/pages/index.js
and replace its contents with:
- Open
2.5. Manage Static Assets
Assets in the static/
folder are copied directly to the root of the output directory during the build. This is ideal for global CSS, fonts, and favicon files.
-
Create the Static directory:
-
Add a Custom CSS File:
- File:
static/css/style.css
- Add some basic styling:
- File:
-
Link the CSS: Add a
<link>
tag to the page header. Since Gatsby is a React app, the best way to inject global CSS is often in a file likesrc/templates/Layout.js
by using the GatsbyHelmet
or simply a<link>
tag directly within the head of the layout component (or a dedicated component imported into the layout).For simplicity, add this line at the very top of
src/templates/Layout.js
:
2.6. Final Local Check
Run the server again:
Verify that the components, layout, and global CSS are loading correctly.
3. GitHub Setup
Prepare the project for version control and Netlify deployment.
3.1. Verify .gitignore
The default starter includes a .gitignore
that correctly ignores the built site. Ensure it contains:
3.2. Commit and Push to GitHub
- Initialize Git (if not done in the initial step):
- Add all files and commit:
- Create a new repository on GitHub (e.g., named
my-gatsby-site
). - Link your local repo and push: (Replace placeholders)
4. Netlify Setup for Continuous Deployment
4.1. Sign Up and Import Project
- Go to the Netlify website and log in.
- In the Netlify dashboard, click "Add new site" then "Import an existing project".
- Select "GitHub" and authorize Netlify.
- Find and select your repository (
my-gatsby-site
).
4.2. Configure Build Settings
Netlify is optimized for Gatsby and will usually auto-detect the following settings.
Setting | Value (Default for Gatsby) | Note |
---|---|---|
Branch to deploy | main |
The branch Netlify monitors for changes. |
Build command | npm run build or gatsby build |
Runs the script defined in your package.json . |
Publish directory | public |
The folder Gatsby outputs the static files to. |
4.3. Deploy the Site
Click the "Deploy site" button. Netlify will:
- Fetch your GitHub code.
- Install Node.js dependencies.
- Run the build command (
npm run build
). - Publish the contents of the
public
directory.
Your high-performance Gatsby site will be live on a temporary Netlify URL.
5. Ongoing Workflow
Your Continuous Deployment (CD) pipeline is now fully automated:
- Make changes locally (update components, add new pages).
- Commit your changes:
- Push to GitHub:
- Netlify automatically detects the push, runs the Gatsby build, and deploys the new version live.