Skip to content

Commit 0986a67

Browse files
tsiq-swyxm-allanson
authored andcommitted
add first draft of API Lifecycle docs (#6229)
* first attempt at lifecycle sequence docs closes #6197 * address Kyle's review I hope this is right... * address Shannon's comments address Shannon's comments
1 parent 50dd934 commit 0986a67

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

docs/docs/gatsby-lifecycle-apis.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,49 @@ Gatsby's design principles include:
1313
trivial and encouraged.
1414
- Plugins are easy to open source and reuse. They're just NPM packages.
1515

16-
See the links along the left under "REFERENCE" for the full API documentation.
16+
# High level Overview
17+
18+
## Bootstrap sequence
19+
20+
During "bootstrap" gatsby:
21+
22+
- reads `gatsby-config.js` to load in your list of plugins
23+
- initializes its cache (stored in `/.cache`)
24+
- pulls in and preprocesses data ("source and transform nodes") into a GraphQL schema
25+
- creates pages in memory
26+
- from your `/pages` folder
27+
- from your `gatsby-node.js` if you implement `createPages`/`createPagesStatefully` (e.g. templates)
28+
- from any plugins that implement `createPages`/`createPagesStatefully`
29+
- extracts, runs, and replaces graphql queries for pages and `StaticQuery`s
30+
- writes out the pages to cache
31+
32+
In development this is a running process powered by [Webpack](https://github.com/gatsbyjs/gatsby/blob/dd91b8dceb3b8a20820b15acae36529799217ae4/packages/gatsby/package.json#L128) and [`react-hot-loader`](https://github.com/gatsbyjs/gatsby/blob/dd91b8dceb3b8a20820b15acae36529799217ae4/packages/gatsby/package.json#L104), so changes to any files get re-run through the sequence again, with [smart cache invalidation](https://github.com/gatsbyjs/gatsby/blob/ffd8b2d691c995c760fe380769852bcdb26a2278/packages/gatsby/src/bootstrap/index.js#L141). For example, `gatsby-source-filesystem` watches files for changes, and each change triggers re-running queries. Other plugins may also perform this service. Queries are also watched so if you modify a query your development app is hot reloaded.
33+
34+
The core of the bootstrap process is the "api-runner", which helps to execute APIs in sequence, with state managed in Redux. Gatsby exposes a number of lifecycle APIs which can either be implemented by you (or any of your configured plugins) in `gatsby-node.js`, `gatsby-browser.js` or `gatsby-ssr.js`.
35+
36+
The sequence of the **main** bootstrap Node API lifecycles are:
37+
38+
- [onPreBootstrap](https://www.gatsbyjs.org/docs/node-apis/#onPreBootstrap) e.g. implemented by [`gatsby-plugin-typography`](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-plugin-typography/src/gatsby-node.js)
39+
- [sourceNodes](https://www.gatsbyjs.org/docs/node-apis/#sourceNodes) e.g. implemented by [`gatsby-source-wikipedia`](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-wikipedia/src/gatsby-node.js)
40+
- within this `createNode` can be called multiple times, which then triggers [onCreateNode](https://www.gatsbyjs.org/docs/node-apis/#onCreateNode).
41+
- (the first schema build happens here)
42+
- [resolvableExtensions](https://www.gatsbyjs.org/docs/node-apis/#resolvableExtensions) for filetype/language extensions eg [`gatsby-plugin-typescript`](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-plugin-typescript/src/gatsby-node.js)
43+
- [createPages](https://www.gatsbyjs.org/docs/node-apis/#createPages) e.g. implemented by [`page-hot-reloader`](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/bootstrap/page-hot-reloader.js)
44+
- within this `createPage` can be called any number of times, which then triggers [onCreatePage](https://www.gatsbyjs.org/docs/node-apis/#onCreatePage)
45+
- [onPreExtractQueries](https://www.gatsbyjs.org/docs/node-apis/#onPreExtractQueries) e.g. implemented by [`gatsby-transformer-sharp`](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-transformer-sharp/src/gatsby-node.js) and [`gatsby-source-contentful`](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-contentful/src/gatsby-node.js)
46+
- (schema update happens here)
47+
- **extract queries from components** where the [queryCompiler](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/internal-plugins/query-runner/query-compiler.js#L189) replaces page GraphQL queries and `StaticQueries`
48+
- The [queries are run](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js#L100), and the [pages are written out](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/internal-plugins/query-runner/pages-writer.js)
49+
- [onPostBoostrap](https://www.gatsbyjs.org/docs/node-apis/#onPostBootstrap) is called (but it is not often used)
50+
51+
## Build sequence
52+
53+
(to be written)
54+
55+
## Client sequence
56+
57+
(to be written)
58+
59+
---
60+
61+
Please see the links along the left under "REFERENCE" for the full API documentation.

0 commit comments

Comments
 (0)