【问题标题】:How to handle dynamic URL routing in Firebase hosting如何在 Firebase 托管中处理动态 URL 路由
【发布时间】:2017-10-30 21:03:25
【问题描述】:

假设在我的 Firebase 公用文件夹中,我有一个 index.html 和一个 salon.html

现在对于像 xyz.com/salon/43 这样的网址,我想加载 salon.html 并在 javascript 中从实时数据库中获取沙龙 43。

现在我可以拥有像xyz.com/salon?id=43 这样的网址。我想知道是否可以在 Firebase 托管中进行前者,如果可以,如何做。

【问题讨论】:

  • 您也可以使用动态托管:firebase.google.com/docs/hosting/functions
  • 谢谢!我不知道云功能可以这样使用。 ...you can have a URL like /blog/<id-for-blog-post>. This URL pattern can be pointed to a function that dynamically uses the URL blog post ID parameter to retrieve content dynamically from your Firebase Realtime Database.

标签: javascript firebase frontend firebase-hosting


【解决方案1】:

您正在寻找 Firebase 托管重写。来自documentation

如果您想为多个 URL 显示相同的内容,请使用重写。重写对模式匹配特别有用,因为您可以接受任何与模式匹配的 URL,并让客户端代码决定要显示的内容。重写规则可用于支持使用 HTML5 pushState 进行导航的应用程序。当浏览器尝试打开指定的源 URL 时,它会在目标 URL 处获得文件的内容。

可以通过在 firebase.json 文件的主机中定义 rewrites 部分来指定 URL 重写:

"hosting": {
  // Add the "rewrites" section within "hosting"
  "rewrites": [ {
    "source": "**",
    "destination": "/index.html"
  } ]
}

【讨论】:

  • 谢谢!太棒了。我已经看到了,但我没有意识到 URL 会与重定向不同。应该在问问题之前尝试一下。
  • 不用担心。我几乎用重定向回答,然后意识到它不会保持状态。
猜你喜欢
  • 2020-05-12
  • 2021-11-04
  • 1970-01-01
  • 2015-04-23
  • 2022-10-21
  • 1970-01-01
  • 2018-12-21
  • 2019-10-09
  • 1970-01-01
相关资源
最近更新 更多