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

Documentation for multiple environments #4858

Merged
merged 2 commits into from
Apr 11, 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
59 changes: 56 additions & 3 deletions docs/docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ If you want to access variables in `.env.*` files in your node.js code, use the
NPM package [dotenv](https://www.npmjs.com/package/dotenv). Install the package and
require it in your `gatsby-config.js` or `gatsby-node.js` the following way on top of your file:

```
```javascript
require('dotenv').config({
path: `.env.${process.env.NODE_ENV}`
});
Expand All @@ -33,13 +33,13 @@ Now the variables are available.

## Example

```
```shell
# Example .env.development file

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

```
```shell
# Example .env.production file

API_URL=https://example.com/api
Expand Down Expand Up @@ -71,3 +71,56 @@ Reserved environment variables:

* `NODE_ENV`
* `PUBLIC_DIR`

## Additional Environments (Staging, Test, etc)

`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 advised to make use of a secondary environment variable for additional environment support.

For instance. If you would like to add a staging environment with a custom Google Analytics Tracking ID. You can add `.env.staging` at the root of your project with the following modification to your `gatsby-config.js`

### Example

```shell
# .env.staging
GATSBY_GA_TRACKING_ID="UA-1234567890"
```

```javascript
// gatsby-config.js

let activeEnv = process.env.ACTIVE_ENV;

if (!activeEnv) {
activeEnv = 'development';
}

require('dotenv').config({
path: `.env.${activeEnv}`
});

module.exports = {
siteMetadata: {
title: 'Gatsby Default Starter'
},
plugins: [
{
resolve: `gatsby-plugin-google-analytics`,
options: {
trackingId: process.env.GATSBY_GA_TRACKING_ID,
// Puts tracking script in the head instead of the body
head: false,
// Setting this parameter is optional
anonymize: true,
// Setting this parameter is also optional
respectDNT: true
}
}
]
};
```

Local testing of staging is as simple as

```
ACTIVE_ENV=staging gatsby develop
```