作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想根据环境代理到不同的 api - 我尝试了以下主题的一些变体,但没有任何运气。如果可能的话,正确的方法是什么?
[build.environment]
API_URI="https://dev-api.foo.com/:splat"
[context.production.environment]
API_URI="https://prod-api.foo.com/:splat"
[[redirects]]
from = "/api/*"
to = "$API_URI"
status = 200
force = true
这不起作用。
虽然当我将 URI 硬编码到 to
字段中时上述配置有效,但当我尝试插入环境变量时它却失败了。
最佳答案
它不受支持,但 Netlify 在其文档中建议了一种解决方法 ( https://www.netlify.com/docs/netlify-toml-reference ):
Using Environment Variables directly as values ($VARIABLENAME) in your netlify.toml file is not supported. However, the following workflow can be used to substitute values based on environment variables in the file, assuming you are only trying to change headers or redirects. The rest of the file is read BEFORE your build - but those sections are read AFTER the build process.
- Add a placeholder like API_KEY_PLACEHOLDER somewhere in the netlify.toml redirects or headers sections.
- Create an Build Environment Variable, for example API_KEY, with the desired value. You can do this in the toml file or in our UI in the Build and Deploy Settings section of your configuration. You might use the latter to keep sensitive values out of your repository.
- Add a command like this one to your build command: sed -i s/API_KEY_PLACEHOLDER/$API_KEY/g netlify.toml && normal build command.
关于netlify - 如何在 netlify.toml 配置中插入环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50797497/
我是一名优秀的程序员,十分优秀!