How to handle dynamic URL routing in Firebase hosting

こ雲淡風輕ζ 提交于 2019-12-01 07:55:36

问题


So let's say in my public folder in Firebase i have an index.html and a salon.html.

Now for a url like xyz.com/salon/43 I want to load salon.html and in the javascript I want to fetch salon 43 from the realtime database.

Right now I'm able to have urls like xyz.com/salon?id=43. I'm wondering if it is possible to do the former in Firebase hosting and if so how.


回答1:


You're looking for Firebase Hosting rewrites. From the 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"
  } ]
}


来源:https://stackoverflow.com/questions/44261372/how-to-handle-dynamic-url-routing-in-firebase-hosting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!