我有三个 Firebase 项目,分别代表 Firebase 托管上的开发、暂存和生产环境.每个环境都使用自己部署的 Firebase 功能,如下所示:
I have three Firebase projects representing Development, Staging and Production environments hosted on Firebase hosting. Each environment utilizes its own deployed Firebase functions like so:
- 开发函数端点:us-central1-my-app-dev.cloudfunctions/someFunction
- 暂存函数端点:us-central1-my-app-staging.cloudfunctions/someFunction
- 生产功能端点:us-central1-my-app.cloudfunctions/someFunction
我无法弄清楚 Firebase 托管的静态客户端 React 应用程序应如何调用这些函数,因为每个函数的 URI 端点都会根据代码执行的环境而变化.
I can't figure out how the static, Firebase-hosted client React application should invoke these functions because the URI endpoints of each changes depending on which environment the code is executing from.
理想情况下,我可以为每个 Firebase 托管环境设置特定于环境的配置;不幸的是 在 Firebase 托管中执行此操作的唯一方法是在 Firebase 函数本身中.
Ideally I could set environment-specific configuration for each Firebase Hosting environment; unfortunately the only way to do this in Firebase Hosting is from within Firebase Functions themselves.
如何检索每个 Firebase 函数的特定于环境的端点?
How can I retrieve the environment-specific endpoint for each Firebase Function?
推荐答案这里有几个选项.
首先,您可以随意配置您的 React 应用程序.每个系统组件(后端、前端)都有自己的配置不一定是坏事.
First, you could just configure your React app any way you like. It's necessarily not a bad thing for each system component (backend, frontend) to have its own configuration.
其次,由于您使用 Firebase 托管来提供静态内容,因此您也可以将其用于 为您的函数 API 端点提供服务.这意味着您的静态内容和 API 端点都通过相同的主机名提供服务,这意味着您在发出请求时不再需要指定主机.所有请求都可以相对于该主机.您可以通过 Hosting 重写规则来实现这一点.
Second, since you're using Firebase Hosting to serve your static content, you can also use it to serve your functions API endpoints. This means that both your static content and API endpoints are all served through the same hostname, which means you no longer have to specify the host when making a request. All the requests can be relative to that host. You can achieve this via Hosting rewrite rules.
更多推荐
如何从 Firebase 托管应用程序访问特定于环境的 Firebase 功能端点?
发布评论