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

Process.env variables inside index.html #1037

Closed
CodeJjang opened this issue Nov 12, 2016 · 6 comments
Closed

Process.env variables inside index.html #1037

CodeJjang opened this issue Nov 12, 2016 · 6 comments

Comments

@CodeJjang
Copy link

I've read the docs thoroughly as well as issues here and haven't found such a suggestion.

How about injecting the process.env values to index.html too, just like PUBLIC_URL is being injected?

It's useful in cases you want to must embed variables (like API keys) in a script tag, e.g. when using react-google-autocomplete and you have to put the following in index.html:

 <script type="text/javascript"
            src="https://maps.googleapis.com/maps/api/js?key=%SOME_ENV_VARIABLE%&libraries=places"/>

I know I can do it by ejecting and adding parameters to webpack config, but I thought there should be a better way to perform this without ejecting.

Thanks

@thien-do
Copy link
Contributor

Based on our current code, you can see that only PUBLIC_URL is injected, explicitly (detail)

If you don't want to eject, then forking Create React App might be a good option!

@gaearon
Copy link
Contributor

gaearon commented Nov 20, 2016

I'm open to making all variables visible to JS also visible to HTML. Wanna send a PR?

@thien-do
Copy link
Contributor

thien-do commented Nov 20, 2016

@gaearon do you mean something like this: thien-do@ca8e0c5

If so I can make one. The above is just idea, I will add for config.prod.js and do some test before making a PR.

@gaearon
Copy link
Contributor

gaearon commented Nov 20, 2016

Hmm yea this seems about right probably

lyleg added a commit to lyleg/create-react-app that referenced this issue Dec 18, 2016
putting process.env into html gives parsing error, going to just
hardcode for now. Will either eject from cra or bring in their fix from
facebook#1037
@ddemolina
Copy link

ddemolina commented Jan 9, 2017

I'm trying to make a PR but I have some questions.

I have relized env['process.env'] values has been stringified. I think it's beacause webpack.DefinePlugin needs an object like this one:

{REACT_APP_CONFIG: ""value""}

Because of that, InterpolateHtmlPlugin is replacing %REACT_APP_CONFIG% by "value" (in quotes) instead of value (without quotes)

I have three ideas to make a solution but I don't know what to use:

A) In env.js/getClientEnviroment return another version of env['process.env'] with non-stringified values.

B) Modify InterpolateHtmlPlugin.js to JSON.parse(value) (to remove quotation marks)

C) Modify InterpolateHtmlPlugin.js to remove first and last character( to remove quotation marks)

If somebody knows why DefinePluging uses stringify values and how parse them, maybe I can port the solution to InterpolateHtmlPlugin

@gaearon gaearon added this to the 0.9.0 milestone Feb 11, 2017
@gaearon
Copy link
Contributor

gaearon commented Feb 11, 2017

This was implemented in #1440.

@gaearon gaearon closed this as completed Feb 11, 2017
@lock lock bot locked and limited conversation to collaborators Jan 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants