如何在 Firebase 托管中处理动态 URL 路由
How to handle dynamic URL routing in Firebase hosting
假设我在 Firebase 的 public 文件夹中有一个 index.html
和一个 salon.html
。
现在对于像 xyz.com/salon/43
这样的 url 我想加载 salon.html
并且在 javascript 我想从实时数据库中获取沙龙 43。
现在我可以拥有 url 个 xyz.com/salon?id=43
。我想知道是否可以在 Firebase 托管中执行前者,如果可以的话如何。
您正在寻找 Firebase 托管重写。来自 documentation:
Use a rewrite when you want to show the same content for multiple URLs. Rewrites are particularly useful with pattern matching, as you can accept any URL that matches the pattern and let the client-side code decide what to display. Rewrite rules can be used to support apps using HTML5 pushState for navigation. When a browser attempts to open a specified source URL it will be given the contents of the file at the destination URL.
URL rewrites can be specified by defining a rewrites
section within hosting in the firebase.json
file:
"hosting": {
// Add the "rewrites" section within "hosting"
"rewrites": [ {
"source": "**",
"destination": "/index.html"
} ]
}
假设我在 Firebase 的 public 文件夹中有一个 index.html
和一个 salon.html
。
现在对于像 xyz.com/salon/43
这样的 url 我想加载 salon.html
并且在 javascript 我想从实时数据库中获取沙龙 43。
现在我可以拥有 url 个 xyz.com/salon?id=43
。我想知道是否可以在 Firebase 托管中执行前者,如果可以的话如何。
您正在寻找 Firebase 托管重写。来自 documentation:
Use a rewrite when you want to show the same content for multiple URLs. Rewrites are particularly useful with pattern matching, as you can accept any URL that matches the pattern and let the client-side code decide what to display. Rewrite rules can be used to support apps using HTML5 pushState for navigation. When a browser attempts to open a specified source URL it will be given the contents of the file at the destination URL.
URL rewrites can be specified by defining a
rewrites
section within hosting in thefirebase.json
file:"hosting": { // Add the "rewrites" section within "hosting" "rewrites": [ { "source": "**", "destination": "/index.html" } ] }