---
name: gatsby
description: Builds static and hybrid sites with Gatsby, React, and GraphQL data layer. Use when creating content-heavy websites, blogs, e-commerce storefronts, or when user mentions Gatsby, static site generation with React, or GraphQL-powered sites.
---
# Gatsby
React-based framework for building fast, static and dynamic websites with a unified GraphQL data layer.
## Quick Start
```bash
# Create new project
npm init gatsby my-site
cd my-site
npm run develop
# Or with specific options
npm init gatsby -- -y my-site
```
## Project Structure
```
my-site/
gatsby-config.js # Site configuration & plugins
gatsby-node.js # Node APIs (page creation)
gatsby-browser.js # Browser APIs
gatsby-ssr.js # SSR APIs
src/
pages/ # File-based routing
index.js # -> /
about.js # -> /about
blog/{slug}.js # -> /blog/:slug (DSG)
components/ # Reusable components
templates/ # Page templates
images/ # Static images
static/ # Unprocessed assets
```
## Pages
### Basic Page
```jsx
// src/pages/index.js
import * as React from "react"
import { Link } from "gatsby"
import Layout from "../components/layout"
export default function HomePage() {
return (
Welcome to My Site
About
)
}
// Head API for SEO
export const Head = () => (
<>
Home | My Site
>
)
```
### Dynamic Routes (File System Route API)
```jsx
// src/pages/products/{Product.slug}.js
import * as React from "react"
import { graphql } from "gatsby"
export default function ProductPage({ data }) {
const product = data.product
return (
{product.name}
{product.description}
${product.price}
)
}
export const query = graphql`
query ($id: String!) {
product(id: { eq: $id }) {
name
description
price
slug
}
}
`
export const Head = ({ data }) => {data.product.name}
```
## GraphQL Data Layer
### Page Queries
```jsx
// src/pages/blog.js
import * as React from "react"
import { graphql, Link } from "gatsby"
export default function BlogPage({ data }) {
return (
Blog Posts
{data.allMarkdownRemark.nodes.map((post) => (
{post.frontmatter.title}
{post.excerpt}
))}
)
}
// Query runs at build time
export const query = graphql`
query {
allMarkdownRemark(sort: { frontmatter: { date: DESC } }) {
nodes {
id
excerpt(pruneLength: 200)
fields {
slug
}
frontmatter {
title
date(formatString: "MMMM DD, YYYY")
}
}
}
}
`
```
### useStaticQuery (Component Queries)
```jsx
// src/components/seo.js
import * as React from "react"
import { useStaticQuery, graphql } from "gatsby"
export function SEO({ title, description, children }) {
const { site } = useStaticQuery(graphql`
query {
site {
siteMetadata {
title
description
siteUrl
}
}
}
`)
const seo = {
title: title || site.siteMetadata.title,
description: description || site.siteMetadata.description,
}
return (
<>
{seo.title}
{children}
>
)
}
```
### Custom Hook Pattern
```jsx
// src/hooks/use-site-metadata.js
import { useStaticQuery, graphql } from "gatsby"
export function useSiteMetadata() {
const data = useStaticQuery(graphql`
query {
site {
siteMetadata {
title
description
siteUrl
social {
twitter
}
}
}
}
`)
return data.site.siteMetadata
}
// Usage
import { useSiteMetadata } from "../hooks/use-site-metadata"
function Footer() {
const { title, social } = useSiteMetadata()
return
}
```
## Configuration
### gatsby-config.js
```javascript
// gatsby-config.js
module.exports = {
siteMetadata: {
title: "My Gatsby Site",
description: "A blazing fast site built with Gatsby",
siteUrl: "https://example.com",
},
plugins: [
// TypeScript support
"gatsby-plugin-typescript",
// Image optimization
"gatsby-plugin-image",
"gatsby-plugin-sharp",
"gatsby-transformer-sharp",
// Source filesystem
{
resolve: "gatsby-source-filesystem",
options: {
name: "images",
path: `${__dirname}/src/images`,
},
},
{
resolve: "gatsby-source-filesystem",
options: {
name: "posts",
path: `${__dirname}/content/posts`,
},
},
// Markdown processing
{
resolve: "gatsby-transformer-remark",
options: {
plugins: [
"gatsby-remark-prismjs",
"gatsby-remark-images",
],
},
},
// CMS integration
{
resolve: "gatsby-source-contentful",
options: {
spaceId: process.env.CONTENTFUL_SPACE_ID,
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN,
},
},
// SEO & Performance
"gatsby-plugin-sitemap",
"gatsby-plugin-robots-txt",
{
resolve: "gatsby-plugin-manifest",
options: {
name: "My Gatsby Site",
short_name: "Gatsby",
start_url: "/",
background_color: "#ffffff",
theme_color: "#663399",
display: "standalone",
icon: "src/images/icon.png",
},
},
],
}
```
## Rendering Options
### Static Site Generation (SSG) - Default
```jsx
// Pages are statically generated at build time
export default function AboutPage() {
return
About Us
}
```
### Deferred Static Generation (DSG)
```jsx
// src/pages/products/{Product.slug}.js
import { graphql } from "gatsby"
export default function ProductPage({ data }) {
return
{data.product.name}
}
export const query = graphql`
query ($id: String!) {
product(id: { eq: $id }) {
name
}
}
`
// Defer generation until first request
export async function config() {
return ({ params }) => ({
defer: true,
})
}
```
### Server-Side Rendering (SSR)
```jsx
// src/pages/dashboard.js
import * as React from "react"
export default function DashboardPage({ serverData }) {
return (