Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

关于前端配置的暴露问题 #6906

Unanswered
yuan1238y asked this question in Q&A
Discussion options

我的项目有一些配置希望在 CSR 阶段暴露不同的内容,防止敏感的配置暴露到浏览器端。
当前我的做法是,定义了一个 config/server.ts 文件,读取 process.env 环境变量。
同时定义一个 config/client.ts 读取 process.env 环境变量。
然后使用 source.globalVars 暴露到浏览器端。
但是这样的代码很不优雅。
演示代码:https://github.com/yuan1238y/config-demo
请问有没有其他更合适的配置管理方案,既可以在服务端(Node.js)使用,又能暴露部分到浏览器端。

You must be logged in to vote

Replies: 2 comments 2 replies

Comment options

没能看懂你的代码,希望能给到仓库方便我们理解

You must be logged in to vote
2 replies
Comment options

Comment options

讨论了一波,Modern.js 后面会支持 .env 文件在服务端也被读取。比较好奇现在你 server.ts 里是怎么读取到 .env 里的内容的。

另外其实可以不用配置 source.globalVars,目前在 .env 中 MODERN_ 起始的环境变量都是默认会注入到浏览器端的。
https://modernjs.dev/zh/guides/basic-features/env-vars.html#%E7%BA%A6%E5%AE%9A%E5%91%BD%E5%90%8D

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /