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

fix(docs): Environment Variables Examples #10406

merged 3 commits into from
Dec 12, 2018

Conversation

LekoArts
Copy link
Contributor

I followed the guide yesterday and stumbled across the issue that the guide is for using env vars in the browser is wrong. After prefixing the vars both in the .env.* and frontend files it worked, e.g.:
https://github.com/LekoArts/gatsby-source-tmdb-example/blob/master/src/components/DetailedView.jsx#L264

I added a little example to the client-side part, moved the comments to code block titles and expanded the example while fixing it.

@LekoArts LekoArts requested a review from a team December 11, 2018 10:37
</div>
)
}
```

`API_KEY` will be available to your site (Server-side) as `process.env.API_KEY`:
Copy link
Contributor

@pieh pieh Dec 11, 2018

Choose a reason for hiding this comment

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

This is not quite correct - API_KEY will be available on front-side code (if .env file is actually there). What probably need to be better documented is that people shouldn't commit .env files really and use options provided by various CD providers (for example Netlify) to define those. And because of that they should prefix things they want available for frontend with GATSBY_ as .env files won't be used by their CD when building gatsby site

Copy link
Contributor

Choose a reason for hiding this comment

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

☝️ is actually verified by https://github.com/gatsbyjs/gatsby/blob/0aade74a1862f7be9875e8f3d84eb049de132c10/e2e-tests/production-runtime/.env.production /

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`)
})

Copy link
Contributor

@shannonbux shannonbux left a comment

Choose a reason for hiding this comment

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

Will defer here to @pieh for technical review. Looks great from formatting perspective!

@@ -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.

@LekoArts
Copy link
Contributor Author

@pieh Rewrote the paragraph

@DSchau DSchau merged commit 94255a3 into master Dec 12, 2018
@DSchau DSchau deleted the LekoArts-patch-1 branch December 12, 2018 16:47
m-allanson added a commit to Bouncey/gatsby that referenced this pull request Dec 14, 2018
* master: (33 commits)
  fix(blog): youfit case study typofix
  Doc improvements to Visual testing with Storybook guide (gatsbyjs#10436)
  fix(gatsby-plugin-offline): prevent incorrect revisioning of static file by workbox (gatsbyjs#10416)
  fix(starters): ttag repo link
  fix typo in pull request template (gatsbyjs#10454)
  fix(www) Fix query for plugin links always ?=undefined (gatsbyjs#10453)
  chore(release): Publish
  fix(gatsby): fix extracting StaticQuery nested in shorthand fragment (gatsbyjs#10443)
  fix(www): avoid querying for no-cache=1 (gatsbyjs#10389)
  fix(gatsby-image): update typescript definitions - properly mark fields as optional (gatsbyjs#10419)
  refactor(gatsby): improve EnsureResources (gatsbyjs#10224)
  Fixed minor Typos and grammatical errors (gatsbyjs#9353)
  docs: add ClinciJS website into showcase (gatsbyjs#10437)
  docs(babel-preset-gatsby): document --save-dev flag in README (gatsbyjs#10434)
  fix(docs): Environment Variables Examples (gatsbyjs#10406)
  chore(release): Publish
  [gatsby-image] re: fade out base64 on full image load (gatsbyjs#7539)
  docs(starter-library): add example to starter library (gatsbyjs#10425)
  docs(gatsby-plugin-offline): specify to not HTTP-cache sw.js (gatsbyjs#10430)
  fix(docs): prompt => confirm (gatsbyjs#10431)
  ...
gpetrioli pushed a commit to gpetrioli/gatsby that referenced this pull request Jan 22, 2019
* Add example + fix code for ENV var doc

* Address michal's suggestions

* Update environment-variables.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants