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

Proxy Setting via Environment Variables? #3783

Closed
vegtelenseg opened this issue Jan 14, 2018 · 13 comments
Closed

Proxy Setting via Environment Variables? #3783

vegtelenseg opened this issue Jan 14, 2018 · 13 comments

Comments

@vegtelenseg
Copy link

I have a question.

Instead of providing proxy information via the package.json, is it possible to achieve exactly the same thing by providing this information directly into environment variables?

@gaearon
Copy link
Contributor

gaearon commented Jan 14, 2018

I think this idea has been floated before. I'm not strictly opposed to it but the proxy functionality is getting a bit complicated so I think we should revisit this after simplifying the “advanced” configuration case: #3366.

@vegtelenseg
Copy link
Author

Alright, I see. Cool.

@peterj
Copy link

peterj commented Aug 24, 2018

@vegtelenseg the way I worked around it is that I've set my production proxy in package.json and then in start.js (assuming you ejected the scripts), I did this:

   const proxySetting =
            process.env.DEV_PROXY || require(paths.appPackageJson).proxy;

Finally, I have a script called local in my package.json that sets the DEV_PROXY=something and runs the start script.

@slorber
Copy link

slorber commented Sep 18, 2018

+1 for this.

My usecase is 2 distinct commands like:

"start-staging": "PROXY=https://staging.domain.com",
"start-preprod": "PROXY=https://preprod.domain.com",

@bakasmarius
Copy link

bakasmarius commented Oct 4, 2018

@gaearon I see #3366 is already closed, any updates on this?
I currently have two .env files that include only one parameter REACT_APP_APP_BACKEND_BASEURL - one for development and another one for production. I'm just not sure is there a way to use this variable inside package.json or should I simply remove proxy line and use REACT_APP_APP_BACKEND_BASEURL in my components?

@bakasmarius
Copy link

bakasmarius commented Oct 27, 2018

For anyone else searching for an answer, it turns out the proxy parameter in package.json is only useful in development mode. If you consider building the project and hosting it on a production server, I suggest to remove the proxy parameter and use .env files with development/production BASE URLs for calls to the backend.

@trungpham
Copy link

trungpham commented Nov 5, 2018

I created this pull request #5720.
You should be able to simply run PROXY=http://wherever.com:80 yarn start to override the proxy setting at run time. Or stick PROXY=http://wherever.com:80 in your .env file.

@Timer
Copy link
Contributor

Timer commented Nov 6, 2018

This is no longer necessary now that you can configure your own proxy and use any environment variable you wish.

@Timer Timer closed this as completed Nov 6, 2018
@ghost
Copy link

ghost commented Nov 7, 2018

This is no longer necessary now that you can configure your own proxy and use any environment variable you wish.

Do you show us sample code?

@ghost
Copy link

ghost commented Nov 7, 2018

For anyone else searching for an answer, it turns out the proxy parameter in package.json is only useful in development mode. If you consider building the project and hosting it on a production server, I suggest to remove the proxy parameter and use .env files with development/production BASE URLs for calls to the backend.

Also here, do you show us sample code how you define and use dev/prod base urls?

@Timer
Copy link
Contributor

Timer commented Nov 8, 2018

I suggest you file a question on Stack Overflow, you can link it here!

@Keksike
Copy link

Keksike commented Dec 4, 2018

I created this pull request #5720.
You should be able to simply run PROXY=http://wherever.com:80 yarn start to override the proxy setting at run time. Or stick PROXY=http://wherever.com:80 in your .env file.

This doesn't seem to work with current version of CRA.

@ksmithut
Copy link

ksmithut commented Dec 5, 2018

I was able to get this to work with a custom src/setupProxy.js file with the following contents:

const proxy = require('http-proxy-middleware')
const pkg = require('../package.json')
const target = process.env.PROXY || pkg.proxy

module.exports = app =>
  target &&
  app.use((req, res, next) =>
    req.accepts('text/html') ? next() : proxy({ target })(req, res, next)
  )

@lock lock bot locked and limited conversation to collaborators Jan 9, 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

9 participants