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) andgatsby-node.jsgraphqltagged queries exported from page and template filesgatsby-browser.jsandgatsby-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
createPagesfor 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
gatsby-config.js, to see where content comes fromgatsby-node.jscreatePages, to see which pages are generated and from what- The template's page query, to see what data it gets
How a request moves through the code
- Source plugins load content into Gatsby's data layer.
gatsby-nodecreatePagesqueries that data and creates a page per item with a template and context.- Each template runs its page query with the context variables.
- Pages 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-browserandgatsby-ssrwrap 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.