-
Notifications
You must be signed in to change notification settings - Fork 184
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
Conversation
packages/react-server/core/config.js
Outdated
var configFile = fs.readFileSync(process.env.REACT_SERVER_CONFIGS + "/config.json"); | ||
/*eslint-disable no-process-env */ |
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.
eslint-disable
in original code actually disable eslint check for all below code. So, I just update it with eslint-disable-next-line
.
5d3da3a
to
4129c6b
Compare
The integration tests I introduced in #828 are failing. These are starting an actual |
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? |
@drewpc It is feasible to have different |
4129c6b
to
e18acb6
Compare
Closing & reopening PR to force a Travis rebuild. |
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.
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
@drewpc Nice suggestion. |
e18acb6
to
9019f07
Compare
9019f07
to
4499043
Compare
Update code per @drewpc 's suggestion. |
I have rebased this branch to master, but the travis unit test still failed. |
Add Object.freeze() and remove duplicate declaration of configFilePath so it passes eslint checks.
packages/react-server/core/config.js
Outdated
// 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); |
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.
should this be Object.freeze(require("configFilePath"))
?
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.
Whoops. I had to add the Object.freeze and removed the require() part. Fixed!
@roblg Have your concerns been addressed here? |
@gigabo yeah, seems reasonable to me |
Thanks @mocheng. 👍 |
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"`).
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"`).
There is a hidden feature in
packages/react-server/core/config.js
, it readconfig.json
file from folder specified byprocess.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 loadconfig.js
file inprocess.env.REACT_SERVER_CONFIGS
folder first. Ifconfig.js
doesn't exist, thenconfig.json
file is used.An exmaple of
.config.js
file might be like below.This might be helpful for a lot of application.