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

Get dynamic config from process.env.REACT_SERVER_CONFIGS #871

Merged
merged 3 commits into from
Mar 9, 2017

Conversation

mocheng
Copy link
Contributor

@mocheng mocheng commented Feb 7, 2017

There is a hidden feature in packages/react-server/core/config.js, it read config.json file from folder specified by process.env.REACT_SERVER_CONFIGS and client side gets same config by rehydration. However, since it is json file, there is no way to config dynamically. For example, different API endpoint for dev env and production env.

This PR make react-server try to load config.js file in process.env.REACT_SERVER_CONFIGS folder first. If config.js doesn't exist, then config.json file is used.

An exmaple of .config.js file might be like below.

if (process.env.NODE_ENV==='production') {
  module.exports = {
     api: 'http://some-domain/'
  };
} else {
  module.exports = {
     api: 'http://dev.some-domain/'
  };
]

This might be helpful for a lot of application.

var configFile = fs.readFileSync(process.env.REACT_SERVER_CONFIGS + "/config.json");
/*eslint-disable no-process-env */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

eslint-disable in original code actually disable eslint check for all below code. So, I just update it with eslint-disable-next-line.

@karlhorky
Copy link
Contributor

The integration tests I introduced in #828 are failing. These are starting an actual react-server via the CLI - not sure if this is an issue.

@drewpc
Copy link
Collaborator

drewpc commented Feb 7, 2017

The guide for Running a Production Server describes how to use different configs per-environment. Do you think that accomplishes what you're trying to do here?

@mocheng
Copy link
Contributor Author

mocheng commented Feb 8, 2017

@drewpc It is feasible to have different REACT_SERVER_CONFIGS in npm script. But, in case there are a lot common config values in different config files, e.g. 100 config values is same for both development and beta env, it would be a burden to keep two config json file same. In this case, yaml file would be a better choice which need to be run by js not json.

@drewpc
Copy link
Collaborator

drewpc commented Feb 10, 2017

Closing & reopening PR to force a Travis rebuild.

@drewpc drewpc closed this Feb 10, 2017
@drewpc drewpc reopened this Feb 10, 2017
Copy link
Collaborator

@drewpc drewpc left a comment

Choose a reason for hiding this comment

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

How about instead of adding a second variable, simply change the fs.readFileSync() to be a require statement if the either config.js or config.json exist? Node can require() both file types and return the appropriate values: https://gist.github.com/bennadel/b1d15ab50a4fe80ba9bc#file-implicit-ext-js

@mocheng
Copy link
Contributor Author

mocheng commented Feb 11, 2017

@drewpc Nice suggestion.

@mocheng
Copy link
Contributor Author

mocheng commented Feb 13, 2017

Update code per @drewpc 's suggestion.

@mocheng
Copy link
Contributor Author

mocheng commented Feb 13, 2017

I have rebased this branch to master, but the travis unit test still failed.
Interestingly, I beheld some PR passed the travis check. Anybody has any clue to make it work?

@mocheng mocheng changed the title Get dynamic config from process.env.REACT_SERVER_CONFIG_SOURCE Get dynamic config from process.env.REACT_SERVER_CONFIGS Feb 13, 2017
Add Object.freeze() and remove duplicate declaration of configFilePath so it passes eslint checks.
@drewpc drewpc added the enhancement New functionality. label Feb 13, 2017
@drewpc drewpc added this to the 0.6.1 milestone Feb 13, 2017
// Node.js tries to load `config.js` file first. If `config.js` doesn't exist, Node.js
// then try to load `config.json`.
configFilePath = path.join(process.cwd(), configFilePath + "/config");
config = Object.freeze(configFilePath);
Copy link
Member

Choose a reason for hiding this comment

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

should this be Object.freeze(require("configFilePath"))?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Whoops. I had to add the Object.freeze and removed the require() part. Fixed!

@gigabo
Copy link
Contributor

gigabo commented Mar 9, 2017

@roblg Have your concerns been addressed here?

@roblg
Copy link
Member

roblg commented Mar 9, 2017

@gigabo yeah, seems reasonable to me

@gigabo
Copy link
Contributor

gigabo commented Mar 9, 2017

Thanks @mocheng. 👍

@gigabo gigabo merged commit 9f68b0f into redfin:master Mar 9, 2017
@mocheng mocheng deleted the dynamic_config branch March 10, 2017 02:50
gigabo added a commit to gigabo/react-server that referenced this pull request Mar 21, 2017
Looks like redfin#871 introduced a regression where absolute paths are no longer
supported in the `REACT_SERVER_CONFIGS` environment variable.

This can be addressed by using `path.resolve`:
```js
> path.resolve("/home/foo", "bar/baz", "config");
'/home/foo/bar/baz/config'
> path.resolve("/home/foo", "/bar/baz", "config");
'/bar/baz/config'
```

In place of `path.join`:
```js
> path.join("/home/foo", "bar/baz", "config");
'/home/foo/bar/baz/config'
> path.join("/home/foo", "/bar/baz", "config");
'/home/foo/bar/baz/config'
```

I also removed the path concatenation (`... + "/config"`).
gigabo added a commit that referenced this pull request Mar 21, 2017
Looks like #871 introduced a regression where absolute paths are no longer
supported in the `REACT_SERVER_CONFIGS` environment variable.

This can be addressed by using `path.resolve`:
```js
> path.resolve("/home/foo", "bar/baz", "config");
'/home/foo/bar/baz/config'
> path.resolve("/home/foo", "/bar/baz", "config");
'/bar/baz/config'
```

In place of `path.join`:
```js
> path.join("/home/foo", "bar/baz", "config");
'/home/foo/bar/baz/config'
> path.join("/home/foo", "/bar/baz", "config");
'/home/foo/bar/baz/config'
```

I also removed the path concatenation (`... + "/config"`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants