具有单页应用程序的Firebase功能自定义域 [英] Firebase Functions Custom Domain with Single Page App

查看:107
本文介绍了具有单页应用程序的Firebase功能自定义域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Firebase hosted single page app.

我还具有3种Firebase功能(联系方式,计划,功能),应用程序和外部资源向其发出请求.

I also have 3 Firebase functions (contact, plans, features) that the app and external sources make requests of.

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

My app has a custom domain, which I'd like to access my functions via.

这是我当前的firebase.json配置

{
  "hosting": {
    "public": "www",
    "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

因此,当前所有流量都流向我的应用,并且路由是通过我的SPA处理的.目前,必须通过cloudfunctions.net URL来访问我的函数,这并不理想.

So currently all traffic goes to my app and routing is handled via my SPA. Access to my functions currently has to be done via the cloudfunctions.net URL which isn't ideal.

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

How can I add URL rewrite entries to this config so that my functions are accessible via my custom domain and my single page app handles the rest of the routes?

我已尝试对features端点进行以下操作:

I have tried the following for the features endpoint:

"rewrites": [
  {
    "source": "/features/**",
    "function": "features"
  },
  {
    "source": "!/features/**",
    "destination": "/index.html"
  }
]

在我的函数中index.js,我有:

...
exports.plans = functions.https.onRequest(plansApi);
exports.features = functions.https.onRequest(featuresApi);
exports.contact = functions.https.onRequest(contactApi);

但是我收到404 Cannot GET /features/123作为答复吗?

But I receive 404 Cannot GET /features/123 as the response?

推荐答案

几件事:

  1. 请确保您的featuresApi处理程序与完整网址路径匹配(例如,/features/123而不是/123). Firebase Hosting会将功能的完整路径转发给功能,而不仅仅是**部分.
  2. 按顺序解决了重写,因此无需为第二个重写源执行!/features/**. **应该很好,因为如果它匹配/features/**,它将已经匹配第一次重写并解析为该函数.
  1. Make sure that your featuresApi handler is matching for the full URL path (e.g. /features/123 not /123). Firebase Hosting forwards the full path to the function, not just the ** part.
  2. Rewrites are resolved in order, so there's no need to do !/features/** for your second rewrite source. ** should be fine since if it matches /features/** it will already match the first rewrite and resolve to the function.

根据错误消息,似乎(1)是这里的罪魁祸首.

Based on the error message, it seems likely that (1) is the culprit here.

这篇关于具有单页应用程序的Firebase功能自定义域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆