Firebase 函数自定义域与单页应用程序

Gre*_*reg 3 firebase single-page-application firebase-hosting google-cloud-functions

我有一个 Firebase 托管的单页应用程序。

我还有 3 个 Firebase 功能(联系人、计划、功能),应用程序和外部资源会向我提出请求。

我的应用程序有一个自定义域,我想通过它访问我的功能。

这是我目前的firebase.json配置

{
  "hosting": {
    "public": "www",
    "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

所以目前所有流量都流向我的应用程序,路由是通过我的 SPA 处理的。目前必须通过cloudfunctions.net不理想的URL访问我的功能。

如何向此配置添加 URL 重写条目,以便我的功能可以通过我的自定义域访问并且我的单页应用程序处理其余的路由?

我已经为features端点尝试了以下方法:

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

在我的功能中,index.js我有:

{
  "hosting": {
    "public": "www",
    "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

但我收到404 Cannot GET /features/123的回应是?

Mic*_*igh 6

一些东西:

  1. 确保您的featuresApi处理程序与完整的 URL 路径匹配(例如/features/123not /123)。Firebase 托管转发函数的完整路径,而不仅仅是**部分。
  2. 重写是按顺序解决的,因此无需!/features/**为您的第二个重写源执行任何操作。**应该没问题,因为如果匹配/features/**,它将已经匹配第一次重写并解析为函数。

根据错误消息,似乎 (1) 是罪魁祸首。