【问题标题】:Firebase hosting static site from cloud storage bucket?Firebase 从云存储桶托管静态站点?
【发布时间】:2020-06-30 16:37:15
【问题描述】:

我在云功能中使用打字稿构建了一个动态站点。我还有云函数触发器,每当 Firestore 中的数据发生变化时,相关页面都会重新编译为托管在 firebase 云存储桶中的静态页面。

桶看起来像这样:

gs://[bucketname].appspot.com
/app/index.html
/app/page1.html
/app/page2.html
/uploads/images.webp
...

目前,静态网站通过名为 app 的云功能中的快速应用程序提供服务。这里的问题在于云功能状态。它们并不总是很热,这使得初始加载时间非常慢。

firebase.json

"hosting": {
  "public": "public",
  "rewrites": [{ "source": "**", "function": "app" }],
}

我想要实现的是让 Firebase 托管指向存储桶中的文件,而不是在云功能中使用快速应用程序。言外之意:

firebase.json

"hosting": {
  "public": "public",
  "rewrites": [
    { "source": "**/:url", "destination": "[BUCKETURL]/app/:url.html" },
    { "source": "**", "destination": "[BUCKETURL]/app/index.html" }]
}

我的 URL 结构非常简单,它是 domain.com/page-name,我也想保持这种方式,最后不带 .html。任何建议将不胜感激。谢谢

【问题讨论】:

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


    【解决方案1】:

    正如您所描述的,允许从 Firebase 托管重写到云存储听起来很酷,但目前是不可能的。我推荐filing a feature request

    与此同时,您的选择是:

    • 使用 Cloud Functions 集成到 Firebase Hosting 作为重定向器,您似乎已经这样做了。这确实可能会导致偶尔的冷启动延迟。

    • 在需要时使用其他类型的 Cloud Function(或其他服务器端进程)生成静态内容并将其推送到 Cloud Storage 存储桶中。在这种情况下,您将不再使用 Firebase 托管,而是始终直接从 Cloud Storage 存储分区提供静态内容。

      如果您对此感兴趣,请查看:Upload single file to firebase hosting via CLI or other without deleting existing ones?

    【讨论】:

      【解决方案2】:

      类似的方法,可以使用 GCP 云存储和 HTTP 负载平衡器,但唯一不方便的是 .html 无法隐藏。

      对于这个方法,主要步骤是:

      • 创建对象可公开访问的存储桶
      • 设置负载平衡器和 SSL 证书。
      • 将您的负载平衡器连接到您的存储桶。
      • 使用A 记录将您的域指向负载均衡器。

      要在桶中创建静态网站,请关注this guide

      托管在存储桶中的静态站点不依赖于任何后端,您生成的动态文件可能是 configured not to be cached 并始终提供可用的最新版本。

      【讨论】:

      • 谢谢,Aldo 这个解决方案可以工作,这不是我想要的。 SEO 非常重要,因此拥有 .html 是不行的。理想情况下,我还希望将我的整个堆栈保留在 firebase 中,并避免使用 GC 的负载均衡器。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 2017-10-17
      • 2021-09-10
      • 2020-11-01
      • 2019-04-27
      • 1970-01-01
      相关资源
      最近更新 更多