在React和Heroku中使用运行时环境 [英] Using runtime env with React and heroku

查看:84
本文介绍了在React和Heroku中使用运行时环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在heroku上有一个演出和生产应用程序。

SoI have a staging and production apps on heroku.

我还使用 create-react-app ,在构建期间嵌入环境变量的位置。由于构建是在阶段上进行的,然后将应用发布到生产,因此我的 staging 生产中的环境变量。

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.

是否有一种方法可以在生产中获得运行时环境(来自heroku配置)而不重建整个应用程序?

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

推荐答案

没有简短的答案。

基本上,当您构建前端应用程序时,它们会从节点服务器(对于create react app而言)变成静态文件。这意味着js-css-html都被硬烘焙到静态文件中。然后,当您从暂存更改为生产时,这些相同的文件将与暂存中的硬编码变量一起传输。

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.

有一个凌乱的解决方案,您编写一个脚本来查找和替换缩小的javascript文件中的变量,但这很讨厌.....

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.

我希望这会有所帮助!

这篇关于在React和Heroku中使用运行时环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆