Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(docs): Environment Variables Examples #10406

Merged
merged 3 commits into from
Dec 12, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions docs/docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ In addition to these Project Environment Variables defined in `.env.*` files, yo
OS Env Vars. OS Env Vars which are prefixed with `GATSBY_` will become available in
browser JavaScript.

```shell:title=.env.*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, does the name of this file actually include an asterisk?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The asterisk means that all names are possible (it's a common way to write it like that), that's why it's already used in line 46.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, @LekoArts I think this is reasonable.

@shannonbux for clarification, we typically use .env.development and .env.production since those credentials could (and often do) change between environments.

GATSBY_API_URL=https://dev.example.com/api
```

#### Server-side Node.js

Gatsby runs several Node.js scripts at build time, notably `gatsby-config.js` and `gatsby-node.js`.
Expand Down Expand Up @@ -77,31 +81,47 @@ Now the variables are available on `process.env` as usual.

## Example

```shell
# Example .env.development file
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)).

API_URL=https://dev.example.com/api
```shell:title=.env.development
GATSBY_API_URL=https://dev.example.com/api
API_KEY=927349872349798
```

```shell
# Example .env.production file

API_URL=https://example.com/api
```shell:title=.env.production
GATSBY_API_URL=https://example.com/api
API_KEY=927349872349798
```

These variables will be available to your site as `process.env.API_URL`:
`GATSBY_API_URL` will be available to your site (Client-side and server-side) as `process.env.GATSBY_API_URL`:

```jsx
// usage
// In any front-end code
render() {
return (
<div>
<img src={`${process.env.API_URL}/logo.png`} alt="Logo" />
<img src={`${process.env.GATSBY_API_URL}/logo.png`} alt="Logo" />
</div>
)
}
```

`API_KEY` will be available to your site (Server-side) as `process.env.API_KEY`. If you commit your `.env.*` file containing `API_KEY` to source control it would also be available on the client-side. However we strongly advise against that! You should prefix your variable with `GATSBY_` (as shown above) instead and Gatsby automatically makes it available in the browser context.

```js
// In any server-side code, e.g. gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-source-patronus`,
options: {
apiKey: process.env.API_KEY,
},
},
],
}
```

## Reserved Environment Variables:

> You can not override certain environment variables as some are used internally
Expand All @@ -121,8 +141,7 @@ For instance. If you would like to add a `staging` environment with a custom Goo

### Example

```shell
# .env.staging
```shell:title=.env.staging
GA_TRACKING_ID="UA-1234567890"
API_URL="http://foo.bar"
```
Expand Down