【问题标题】:Hosting rewrite with custom domain for function doesn't work使用自定义域的功能进行托管重写不起作用
【发布时间】:2021-03-29 08:41:43
【问题描述】:

我正在尝试使用域设置 firebase,以便:

  • 我的函数在 mydomain.com/api/functionName 上可用
  • 我的主机在 mydomain.com 上可用(或者最好是 mydomain.com/client/,但另一个也可以)

我的firebase.json 看起来像这样:

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

部署它并在浏览器中访问域后,//api/helloWorld 路由将始终将我带到我的客户端应用程序,而不是我的函数。

对我来说真正奇怪的是,当在本地运行模拟器时,托管根本不起作用,但localhost:5000/api/helloWorld 按预期工作并调用该函数。

我在这里做错了什么?感觉好像我的firebase.json 根本没有部署。

更新

这是我的函数定义,如果与它有关的话:

exports.helloWorld = functions
  .region("europe-west3")
  .https.onRequest((_, response) => {
    response.send("Hello from Firebase!");
  });

【问题讨论】:

    标签: firebase google-cloud-functions firebase-hosting firebase-cli


    【解决方案1】:

    以“第一场比赛获胜”的顺序重写作品。由于您在顶部有一个**,它将匹配所有 URL,并且永远不会访问第二次重写。如果您切换顺序,您应该会看到它按预期工作:

    {
      "hosting": {
        // ...
        "rewrites": [
          {
            "source": "/api/helloWorld",
            "function": "helloWorld"
          },
          {
            "source": "**",
            "destination": "/index.html"
          }
        ]
      }
    }
    

    【讨论】:

    • 原来你不能使用托管+函数重写与us-central1之外的函数。这就是为什么它会导致这种奇怪的行为。在us-central1 上部署我的功能可以很好地满足您的回答。
    猜你喜欢
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 2014-03-26
    • 1970-01-01
    相关资源
    最近更新 更多