Firebase:在URL Rewrite中传递url Param

Jam*_*esG 9 url-rewriting firebase firebase-hosting

对于firebase文档中的代码示例,它表示要启动一个url重写,如下所示:

        "hosting": {
          // Add the "rewrites" section within "hosting"
          "rewrites": [ {
            "source": "**",
            "destination": "/index.html"
          } ]
        }
Run Code Online (Sandbox Code Playgroud)

我想将参数传递给索引页面时该怎么办?我试过了:

        "hosting": {
          // Add the "rewrites" section within "hosting"
          "rewrites": [ {
            "source": "/item/**",
            "destination": "/item.html?i=$1"
          } ]
        }
Run Code Online (Sandbox Code Playgroud)

但这没有任何作用..

我也尝试过以下答案:

 "hosting": {
  // Add the "rewrites" section within "hosting"
  "rewrites": [ {
    "source": "/item/:item",
    "destination": "/item.html?i=:item"
  } ]
}
Run Code Online (Sandbox Code Playgroud)

但这只会返回404页面.

小智 6

我知道这是一个老问题,但我遇到了类似的问题并且没有找到答案,所以我想我会分享我是如何解决它的。

{    
    "hosting": {
    // Add the "rewrites" section within "hosting"
        "rewrites": [ {
            "source": "/item/**",
            "destination": "/item.html"
        } ]
}
Run Code Online (Sandbox Code Playgroud)

我通过将动态 URL 重写为静态html页面来解决我的问题。由于是重写,因此 URL 将保持为sourceURL 模式,而不会destination像重定向那样更改为URL。

然后我们可以在页面上使用 Javascript读取sourceURL 。然后您可以使用返回一个数组来选择您需要的部分。window.location.pathnameitem.html.split('/')

希望这有助于任何寻找类似解决方案的人。

  • 是的,有一些肮脏的黑客,但这一定是我见过的最肮脏的黑客:) (2认同)

Jam*_*esG 4

我刚刚收到 Firebase 支持人员发来的一封电子邮件,内容如下:

Firebase 支持更新:

仅使用 Firebase Hosting 无法实现您的用例。为此,您还需要使用云功能。Firebase 最近刚刚发布了原生 Firebase Hosting + Functions 集成,这使得执行服务器端处理成为可能,因此您可以将 URL 重定向到一个函数,您可以在其中编写代码来剖析路径并生成您想要的任何响应。您可以查看我们的文档以详细了解它。

https://firebase.google.com/docs/hosting/functions

我已经给他们发了电子邮件,看看这是否是将来会添加的内容,因为运行一个页面的进程似乎有点过分了。

更新 28/07/2017

由于不支持此功能,我已为您提交了功能请求。但是,我目前无法分享任何细节或时间表。不过,我们会继续考虑您的反馈。

同时,您可以关注我们的发行说明。

祝你有美好的一天。

https://firebase.google.com/support/releases