-
Notifications
You must be signed in to change notification settings - Fork 10.3k
fix(docs): Environment Variables Examples #10406
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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.* | ||||||||||||||||
| 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`. | ||||||||||||||||
|
|
@@ -77,31 +81,45 @@ Now the variables are available on `process.env` as usual. | |||||||||||||||
|
|
||||||||||||||||
| ## Example | ||||||||||||||||
|
|
||||||||||||||||
| ```shell | ||||||||||||||||
| # Example .env.development file | ||||||||||||||||
|
|
||||||||||||||||
| 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`: | ||||||||||||||||
|
||||||||||||||||
| it(`Uses env vars`, () => { | |
| cy.visit(`/env-vars`).waitForAPI(`onRouteUpdate`) | |
| cy.getTestElement(`process.env`).contains(`{}`) | |
| cy.getTestElement(`process.env.EXISTING_VAR`).contains(`"foo bar"`) | |
| cy.getTestElement(`process.env.NOT_EXISTING_VAR`).should(`be.empty`) | |
| }) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.developmentand.env.productionsince those credentials could (and often do) change between environments.