使用自定义域进行firebase函数http调用

Tom*_*lva 7 dns firebase google-cloud-functions

有没有办法使用自定义域的firebase云功能http钩子.

cloudfunctions的默认网址如下所示:

https://us-central1-my-awesome-app.cloudfunctions.net/ios-oauth/

我想让它看起来像这样:

https://myawesomeapp.com/ios-oauth/

我环顾四周,是否有其他人在寻找相同的解决方案,我确实找到了这个:

https://stackoverflow.com/questions/43482224/firebase-cloud-functions-custom-domain

Cra*_*les 19

接受的答案是正确的,我去年创建了这个存储库来演示功能:https : //github.com/cjmyles/firebase-react-express

为了按照https://firebase.google.com/docs/hosting/full-config#rewrites保留 HTML pushState 功能,您可能需要扩展规则以允许所有其他请求通过您的 index.html 页面,这解决了@Boris 在他的回答中面临的问题。

"rewrites": [
    {
        "source": "/api/**",
        "function": "app"
    },
    {
        "source": "!/@(api)/**",
        "destination": "/index.html"
    }
]
Run Code Online (Sandbox Code Playgroud)

这真的不需要,因为重写规则旨在匹配请求的第一次出现(因此顺序很重要),但这对我有用,因为它允许所有非 API 相关的请求通过。

请注意:在撰写本文时,Firebase 文档指出:Firebase 托管仅在 us-central1 中支持 Cloud Functions。

  • @SebastianG 无论您的主机位于何处,如果主机重写了某个函数,那么该函数必须位于 us-central1 中。 (2认同)

Tom*_*lva 15

我已经联系了firebase支持以获得有关此问题的答案.我在文档中转发到了这一部分.

https://firebase.google.com/docs/hosting/functions#create_an_http_function_to_your_hosting_site

您可以将自己的域与firebase-cloud-functions一起使用.要做的是使用firebase-hosting.

  1. 将自定义域连接到firebase托管
  2. 将自定义函数路由添加到firebase.json

    {
      "hosting": {
        "public": "public",
    
        // Add the following rewrites section *within* "hosting"
        "rewrites": [{
          "source": "/bigben", "function": "bigben"
        }]
    
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 部署到firebase

  • [现在是 2021 年 1 月,该解决方案仍然仅适用于 `us-central1`](https://firebase.google.com/docs/hosting/functions#create_an_http_function_to_your_hosting_site) (8认同)
  • 是否可以创建动态路线,从而不必定义每个功能?像`/ api / ***`这样的东西,其中`***`是函数名?希望我在这里没有违反规则,只是看不出为此提出一个完整问题的理由。 (2认同)
  • 这是如何解决的?仅 us-central1 支持此功能。 (2认同)