Skip to content

Commit a7df599

Browse files
Marcy SuttonLB
Marcy Sutton
and
LB
authoredMar 10, 2020
Update Feedback/Env Var docs to follow style guide (#22146)
* update docs to follow style guide * Apply suggestions from code review Co-Authored-By: LB <laurie@gatsbyjs.com> Co-authored-by: LB <laurie@gatsbyjs.com>
1 parent 552ece2 commit a7df599

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed
 

‎docs/docs/cli-feedback.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
title: Gatsby CLI Feedback
33
---
44

5-
Gatsby users will be prompted every 3 months (at the most) to give feedback to Gatsby on our products and services. This feedback is incredibly valuable and will help us shape Gatsby.
5+
Gatsby users will be prompted every 3 months (at the most) to give feedback on Gatsby's products and services. This feedback is incredibly valuable and will help the team shape Gatsby.
66

77
## How to opt-out
88

9-
Users may always opt-out from the feedback prompts with `gatsby feedback --disable` or by setting the environment variable `GATSBY_TELEMETRY_DISABLED` to `1`
9+
Users may always opt-out from the feedback prompts with `gatsby feedback --disable` or by setting the [environment variable](/docs/environment-variables/) `GATSBY_TELEMETRY_DISABLED` to `1`.

‎docs/docs/environment-variables.md

+25-24
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22
title: Environment Variables
33
---
44

5-
## Environments and Environment Variables
6-
7-
You can provide environment variables to your site to customise its behavior in different environments.
5+
You can provide environment variables to your site to customize its behavior in different environments.
86

97
Environment variables can be distinguished between different types.
8+
109
There are environment variables that are defined in special places intended to be used in different deployment environments. You can call these “Project Env Vars”.
10+
1111
And there are true OS-level environment variables that might be used in command-line calls. You can call these “OS Env Vars”.
1212

1313
In both cases you want to be able to access the relevant value of these variables for the environment you are in.
1414

15-
By default gatsby supports only 2 environments:
15+
By default Gatsby supports 2 environments:
1616

17-
- If you run `gatsby develop`, then you will be in the 'development' environment.
18-
- If you run `gatsby build` or `gatsby serve`, then you will be in the 'production' environment.
17+
- **Development.** If you run `gatsby develop`, then you will be in the 'development' environment.
18+
- **Production.** If you run `gatsby build` or `gatsby serve`, then you will be in the 'production' environment.
1919

20-
If you want to define other environments then you'll need to do a little more work. See ["Additional Environments" below](#additional-environments-staging-test-etc). You can also have a look at our [environment variables codesandbox](https://codesandbox.io/s/6w9jjrnnjn) while reading the examples below.
20+
If you want to define other environments then you'll need to do a little more work. See ["Additional Environments" below](#additional-environments-staging-test-etc). You can also have a look at the [environment variables CodeSandbox](https://codesandbox.io/s/6w9jjrnnjn) while reading the examples.
2121

2222
## Accessing Environment Variables in JavaScript
2323

2424
All of the Project and OS Env Vars are only directly available at build time, or
25-
when Node.Js is running. They aren't immediately available at run time of the client code; they
26-
need to be actively captured and embedded into our client-side JavaScript.
25+
when Node.js is running. They aren't immediately available at run time of the client code; they
26+
need to be actively captured and embedded into client-side JavaScript.
2727
This is achieved during the build using Webpack's [DefinePlugin](https://webpack.js.org/plugins/define-plugin/).
2828

2929
Once the environment variables have been embedded into the client-side code, they are accessible from the
@@ -39,8 +39,7 @@ or rebuild your site after changing them.
3939

4040
For Project Env Vars that you want to access in client-side browser JavaScript, you can define
4141
an environment config file, `.env.development` and/or `.env.production`, in your root folder.
42-
Depending on your active environment, the correct one will be found and its values embedded as environment variables in the
43-
browser JavaScript.
42+
Depending on your active environment, the correct one will be found and its values embedded as environment variables in the browser JavaScript.
4443

4544
In addition to these Project Environment Variables defined in `.env.*` files, you could also define
4645
OS Env Vars. OS Env Vars which are prefixed with `GATSBY_` will become available in
@@ -54,7 +53,7 @@ GATSBY_API_URL=https://dev.example.com/api
5453

5554
Gatsby runs several Node.js scripts at build time, notably `gatsby-config.js` and `gatsby-node.js`.
5655
OS Env Vars will already be available when Node is running, so you can add environment variables the
57-
normal ways e.g. by adding environment variables through your hosting/build tool, your OS, or when
56+
typical ways, e.g. by adding environment variables through your hosting/build tool, your OS, or when
5857
calling Gatsby on the command line.
5958

6059
In Linux terminals this can be done with:
@@ -67,8 +66,8 @@ In Windows it's a little more complex. [Check out this Stack Overflow article fo
6766

6867
Project environment variables that you defined in the `.env.*` files will _NOT_ be immediately available
6968
in your Node.js scripts. To use those variables, use NPM package [dotenv](https://www.npmjs.com/package/dotenv) to
70-
examine the active `.env.*` file and attached those values,
71-
It's already a dependency of Gatsby, so you can require it in your `gatsby-config.js` or `gatsby-node.js` like this:
69+
examine the active `.env.*` file and attach those values.
70+
`dotenv` is already a dependency of Gatsby, so you can require it in your `gatsby-config.js` or `gatsby-node.js` like this:
7271

7372
```javascript:title=gatsby-config.js
7473
require("dotenv").config({
@@ -78,9 +77,9 @@ require("dotenv").config({
7877

7978
Now the variables are available on `process.env` as usual.
8079

81-
## Example
80+
## Example of using an environment variable
8281

83-
Please note that you shouldn't commit `.env.*` files to your source control and rather use options given by your CD provider (e.g. Netlify with its [build environment variables](https://www.netlify.com/docs/continuous-deployment/#build-environment-variables)).
82+
Please note that you shouldn't commit `.env.*` files to your source control and rather use options given by your Continuous Deployment (CD) provider. An example is Netlify with its [build environment variables](https://www.netlify.com/docs/continuous-deployment/#build-environment-variables).
8483

8584
```text:title=.env.development
8685
GATSBY_API_URL=https://dev.example.com/api
@@ -93,6 +92,7 @@ API_KEY=927349872349798
9392
```
9493

9594
Note: since Gatsby uses the [Webpack DefinePlugin](https://webpack.js.org/plugins/define-plugin/) to make the environment variables available at runtime, they cannot be destructured from `process.env`; instead, they have to be fully referenced.
95+
9696
`GATSBY_API_URL` will be available to your site (Client-side and server-side) as `process.env.GATSBY_API_URL`.:
9797

9898
```jsx
@@ -106,7 +106,7 @@ render() {
106106
}
107107
```
108108

109-
In Node, your site has access to your `API_KEY` (Server-side) using the identifier `process.env.API_KEY`. To access it client-side, you can use a `.env.*` file containing `API_KEY`. However, we **strongly** advise against checking these files into source control as it's a security issue to expose the API key. As a more secure alternative, you can prefix your variable with `GATSBY_` (as shown above). With this prefix, Gatsby automatically embeds the variable as process.env.GATSBY\_\* in compiled JS making it available in the browser context without exposing it elsewhere.
109+
In Node, your site has access to your `API_KEY` (Server-side) using the identifier `process.env.API_KEY`. To access it client-side, you can use a `.env.*` file containing `API_KEY`. However, you are **strongly** advised against checking these files into source control as it's a security issue to expose the API key. As a more secure alternative, you can prefix your variable with `GATSBY_` (as shown above). With this prefix, Gatsby automatically embeds the variable as `process.env.GATSBY\_\*` in compiled JS making it available in the browser context without exposing it elsewhere.
110110

111111
```js
112112
// In any server-side code, e.g. gatsby-config.js
@@ -125,7 +125,7 @@ module.exports = {
125125
## Reserved Environment Variables:
126126

127127
> You can not override certain environment variables as some are used internally
128-
> for optimizations during build
128+
> for optimizations during build, such as:
129129
130130
- `NODE_ENV`
131131
- `PUBLIC_DIR`
@@ -136,26 +136,27 @@ Gatsby also allows you to specify another environment variable when running the
136136

137137
If set to true, this will expose a `/__refresh` webhook that is able to receive `POST` requests to _refresh_ the sourced content. This exposed webhook can be triggered whenever remote data changes, which means you can update your data without re-launching the development server.
138138

139-
You can trigger this endpoint locally for example on Unix-based operating systems (like Ubuntu and MacOS) you can use `curl -X POST http://localhost:8000/__refresh`.
139+
You can trigger this endpoint locally, for example, on Unix-based operating systems (like Ubuntu and MacOS) using `curl -X POST http://localhost:8000/__refresh`.
140140

141141
## Build Variables
142142

143143
Gatsby uses additional environment variables in the build step to fine-tune the outcome of a build. You may find these helpful for more advanced configurations, such as using [CI/CD](https://en.wikipedia.org/wiki/CI/CD) to deploy a Gatsby site.
144144

145145
For example, you can set `CI=true` as an environment variable to allow Gatsby's build script to tailor the terminal output to an automated deployment environment. Some CI/CD tooling may already set this environment variable. This is useful for limiting the verbosity of the build output for [dumb terminals](https://en.wikipedia.org/wiki/Computer_terminal#Dumb_terminals), such as terminal in progress animations.
146146

147-
Gatsby detects an optimal level of parallelism for the render phase of `gatsby build` based on the reported number of physical CPUs. For builds that are run in virtual environments, you may need to adjust the number of worker parallelism with the `GATSBY_CPU_COUNT` environment variable. See [Multi-core builds](https://www.gatsbyjs.org/docs/multi-core-builds/).
147+
Gatsby detects an optimal level of parallelism for the render phase of `gatsby build` based on the reported number of physical CPUs. For builds that are run in virtual environments, you may need to adjust the number of worker parallelism with the `GATSBY_CPU_COUNT` environment variable. See [Multi-core builds](/docs/multi-core-builds/).
148148

149-
## Additional Environments (Staging, Test, etc)
149+
## Additional Environments (Staging, Test, etc.)
150150

151151
As noted above `NODE_ENV` is a reserved environment variable in Gatsby as it is needed by the build system to make key optimizations when compiling React and other modules. For this reason it is necessary to make use of a secondary environment variable for additional environment support, and manually make the environment variables available to the client-side code.
152152

153153
You can define your own OS Env Var to track the active environment, and then to locate the relevant Project Env Vars to load. Gatsby itself will not do anything with that OS Env Var, but you can use it in `gatsby-config.js`.
154-
Specifically, you can use `dotenv` and your individual OS Env Var to locate the `.env.myCustomEnvironment` file, and then use module.exports to store those Project Env Vars somewhere that the client-side JavaScript can access the values (via GraphQL queries).
155154

156-
For instance: if you would like to add a `staging` environment with a custom Google Analytics Tracking ID, and a dedicated `apiUrl`. You can add `.env.staging` at the root of your project with the following modification to your `gatsby-config.js`
155+
Specifically, you can use `dotenv` and your individual OS Env Var to locate the `.env.myCustomEnvironment` file, and then use `module.exports` to store those Project Env Vars somewhere that the client-side JavaScript can access the values (via GraphQL queries).
156+
157+
### Google Analytics env var example
157158

158-
### Example
159+
If you would like to add a `staging` environment with a custom Google Analytics Tracking ID, and a dedicated `apiUrl` you can add `.env.staging` at the root of your project. In order to do so, use the following modification to your `gatsby-config.js`:
159160

160161
```text:title=.env.staging
161162
GA_TRACKING_ID="UA-1234567890"

0 commit comments

Comments
 (0)
Please sign in to comment.