Using runtime env with React and heroku

霸气de小男生 提交于 2020-03-01 11:51:08

问题


SoI have a staging and production apps on heroku.

I also use create-react-app, where the environmental variables are embedded during the build time. Since the build happens on staging and then the app is released to production, I'm having staging env vars on production.

Is there a way to get the runtime envs on production (from heroku config) without rebuild the whole app?


回答1:


No is the short answer.

Basically when you build frontend applications they turn from a node server (in the case of create react app) into static files. This means the js-css-html is all hard baked into static files. Then when you change from staging to prod these same files will just be transferred with the "hard coded" variables from staging.

There is a messy solution to this where you write a script to find and replace your variables in the minified javascript files but this is nasty.....

A better solution would be to rebuild the frontend, I mean it doesn't take that long, with your production variables. This also gives you a chance to unit test etc before deploying to production.

I hope this helps!



来源:https://stackoverflow.com/questions/52890469/using-runtime-env-with-react-and-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!