【问题标题】:Redirect Firebase Hosting root to a Cloud Function is not working将 Firebase 托管根目录重定向到云功能不起作用
【发布时间】:2017-12-05 20:48:10
【问题描述】:

我正在使用带有 firebase.json 文件的 Firebase 托管,该文件应该将所有流量转发到填充 SEO 的元和 og 标记的云功能(预渲染)。

{
  "hosting": {
      "public": "dist/prod",
      "rewrites": [
       {
          "source": "**",
          "function": "prerender"
       }
      ]
   }
}

我的prerender 函数正在处理请求并呈现 HTML 文件。这工作正常:

export const prerender = functions.https.onRequest((req, res) => {
   console.log('prerender function: "' + req.path + '"');
   ...
}

当到达https://xxx.cloudfunctions.net/prerender 的终点时,我在 Firebase 仪表板中的 Functions -> Logs 下正确地得到了调用:

prerender function: "null"

但是,在调用 https://mypage.firebaseapp.com 时,我没有收到任何日志,而且它似乎在我的 dist/prod 文件夹中呈现了 index.html

重写有什么我遗漏的吗?我尝试将/ 重写为相同的函数,但没有成功。任何提示都非常感谢!

【问题讨论】:

标签: javascript express firebase google-cloud-functions firebase-hosting


【解决方案1】:

您应该能够完全按照您显示的方式将所有 URL 路由到一个函数。我猜您的 dist/prod 目录中仍然有一个 index.html 文件。在我的测试项目中,我只是将根 index.html 重命名为其他内容,并且对 / 的请求被路由到我的函数。

事实证明,如果存在与客户端请求 URL 匹配的静态 Web 内容,则会提供该内容,而不是委托给该函数。对于任何传入的 URL 都是如此。真正确保所有请求被路由到你的函数的唯一方法是在部署之前从你的dist/prod文件夹中删除所有内容。

我相信关键信息在rewrites 的文档中:

仅当文件或文件夹不存在时才应用重写规则 指定的源,并返回文件的实际内容 目的地而不是 HTTP 重定向。

【讨论】:

    猜你喜欢
    • 2020-10-14
    • 2020-06-13
    • 1970-01-01
    • 2020-03-15
    • 2018-08-07
    • 2018-04-11
    • 2020-06-04
    • 1970-01-01
    • 2018-10-16
    相关资源
    最近更新 更多