I've built an isomorphic React app based loosely on the starter kit in this repo. It uses webpack to build the production code.
Thing is, I need to expose the value of a few environment variables on the server to the client code in the browser without rebuilding the production code. I want to be able to change the value of the env vars, and have it the effect the client on next page refresh, without having to rebuild anything. And I don't want to complicate testing in order to do it.
I've found a few solutions, none of which are great:
Problems with these approaches:
I've done both 2 and 3 in the past and never was really intellectually satisfied by those solutions.
Any suggestions? Seems like this should be a common/solved problem. Maybe I'm just overthinking it and 3 is the way to go.
In Isomorphic process,when Developer authorizes client facing module ,there is a dependency of the module which requires a file containing secret data,Application is bundled with that file and sent to client with all secret and private data which an attacker can easily access by searching the bundle file .
Definition of Isomorphic React App: Isomorphic React App is a web app that consists of code that can run on both server and client-side. It usually takes full advantage of performance and SEO friendliness from the server. And combine it with browser capabilities to handle complex user interactions.
You can access environment variables (with the REACT_APP_ prefix) from your React app via the Node. js process. env. object.
The boiler plate use express, you can expose server env variable to the client using express' local.
var serverVariable = 'this is a server variable'
app.get('/somelink', function (req, res, next) {
res.locals.data = {
FluxStore: { serverlocal: serverVariable }
}
next()
})
You then pass the local either through React.renderToString which will be pick by FluxStore on the client. The other method, you can use data fetching api like falcor which can be pick by client side Action Store through falcor-http-datasource, you don't need express local for falcor you build it with falcor-express and falcor-router
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With