Skip to content

How to understand a Gatsby codebase

Gatsby pulls content from sources (files, CMSs, APIs) into a GraphQL data layer at build time, then pages query that layer. Pages come from `src/pages/` or are created in code by `gatsby-node`, which is where much of the real logic hides.

How to tell it's Gatsby

  • gatsby-config.js (or .ts) and gatsby-node.js
  • graphql tagged queries exported from page and template files
  • gatsby-browser.js and gatsby-ssr.js

Where things usually live

gatsby-config.js
Site metadata and the plugin list, including data sources.
gatsby-node.js
Build-time hooks: createPages, schema customisation, node creation.
src/pages/
Pages created automatically from file names.
src/templates/
Components used by createPages for generated pages such as blog posts.
gatsby-browser.js / gatsby-ssr.js
Wrappers and hooks for the browser and server render.

Conventions, not rules. Real projects rename and move things, so confirm against the repository.

Where to start reading

  1. gatsby-config.js, to see where content comes from
  2. gatsby-node.js createPages, to see which pages are generated and from what
  3. The template's page query, to see what data it gets

How a request moves through the code

  1. 1Source plugins load content into Gatsby's data layer.
  2. 2gatsby-node createPages queries that data and creates a page per item with a template and context.
  3. 3Each template runs its page query with the context variables.
  4. 4Pages render to static HTML, then hydrate as a React app.

Questions to ask about the repository

  • Which pages are created in gatsby-node.js, and from which data?
  • Which plugins source data, and from where?
  • What do gatsby-browser and gatsby-ssr wrap the app in?

What trips people up

  • Data only exists at build time, so changing content requires a rebuild.
  • Page queries can only use variables passed in page context from createPages.
  • Gatsby development has slowed, and many sites are migrating; check plugin versions before upgrading.