You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
add context to client data fetching page aiming to be easier to understand (#15020)
* add context to client data fetching page for hydration and build and run time
* Update docs/docs/client-data-fetching.md
A better explanation for build time and run time
Co-Authored-By: Marcy Sutton <[email protected]>
* Update docs/docs/client-data-fetching.md
Better explanation for hydration
Co-Authored-By: Marcy Sutton <[email protected]>
* fix: copy updates from PR review
Copy file name to clipboardExpand all lines: docs/docs/client-data-fetching.md
+14-4Lines changed: 14 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,23 @@
2
2
title: Client Data Fetching
3
3
---
4
4
5
-
Because a Gatsby site hydrates into a React app after loading statically, Gatsby is not just for static sites. You can fetch data dynamically on the client, as needed, as you would with any other React app.
5
+
## Context
6
+
7
+
This article touches on how to fetch data at both _build time_ and _run time_. It uses the plugin [`gatsby-source-graphql`](/packages/gatsby-source-graphql/) to fetch data at [build time](/docs/glossary#build) on the server, while it uses the [`axios`](https://github.com/axios/axios) package to fetch different data on the [client-side](/docs/glossary#client-side) when the page loads.
8
+
9
+
When this article mentions [hydration](/docs/glossary#hydration), it means that Gatsby (through React.js) builds static files to render server-side. When Gatsby's script bundle downloads and executes in the browser, it preserves the HTML markup built by Gatsby and turns the site into a full React web application that can manipulate the [DOM](/docs/glossary#dom). The result of this process creates fast loading pages and a nice user experience.
10
+
11
+
Compiling pages at [build-time](/docs/glossary#build) is useful when your website content won't change often, or when triggering a build process to recompile works fine. However, some websites with more dynamic needs require a [client-side](/docs/glossary#client-side)[runtime](/docs/glossary#runtime) to handle constantly changing content after the page loads, like a chat widget or an email client web application.
12
+
13
+
## Combining build-time and client run-time data
14
+
15
+
Because a Gatsby site [hydrates](/docs/glossary#hydration) into a React app after loading statically, Gatsby is not just for static sites. You can also fetch data dynamically on the client-side as needed, like you would with any other React app.
6
16
7
17
To illustrate this, we'll walk through a small example site that uses both Gatsby's data layer at build-time and data on the client at run-time. This example is based loosely on Jason Lengstorf's [Gatsby with Apollo](https://github.com/jlengstorf/gatsby-with-apollo) example. We'll be fetching character data for Rick (of Rick and Morty) and a random pupper image.
8
18
9
19
> Note: Check out the [full example here](https://github.com/amberleyromo/gatsby-client-data-fetching), if helpful.
10
20
11
-
## 1. Create a Gatsby page component
21
+
###1. Create a Gatsby page component
12
22
13
23
No data yet. Just the basic React page that we'll be populating.
14
24
@@ -33,7 +43,7 @@ class ClientFetchingExample extends Component {
33
43
export default ClientFetchingExample
34
44
```
35
45
36
-
## 2. Query for character info at build time
46
+
### 2. Query for character info at build time
37
47
38
48
To query for Rick's character info and image, we'll use the `gatsby-source-graphql` plugin. This will allow us to query the Rick and Morty API using Gatsby queries.
39
49
@@ -105,7 +115,7 @@ class ClientFetchingExample extends Component {
105
115
export default ClientFetchingExample
106
116
```
107
117
108
-
## 3. Fetch pupper info and image data on the client
118
+
###3. Fetch pupper info and image data on the client
109
119
110
120
Now we'll finish out by fetching pupper info from the [Dog CEO Dog API](https://dog.ceo/dog-api/). (We'll fetch a random pupper. Rick isn't picky.)
0 commit comments