首页 > 解决方案 > URL 中的 Firebase 托管路径参数不起作用

问题描述

id在 Firebase 上托管时,我需要通过使用路径参数从 URL 中获取动态内容来提供动态内容。例如:

mydomain.com/apps/4480023

在这种情况下,我想提取4480023为我正在寻找的资源的 ID。我在 firebase.json 文件中尝试了以下更改:

{
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      },
      {
        "source": "/apps/**",
        "destination": "/apps.html"
      }
    ],
    "cleanUrls": true
  }
}

在这种情况下,当用户浏览该资源时,我可以使用 javascript 函数从 URL 中检索 ID。我的问题是,这种重写不起作用,它将用户引导到 index.html 页面,并且所有 CSS/JS 文件最终都无法正常运行。

如何修改它以启用此功能?

标签: firebaseurl-rewritingfirebase-hosting

解决方案


按顺序检查重写。这意味着匹配所有请求的第一次重写将始终由 index.html 提供服务。

您所要做的就是更改重写的顺序,以便在捕获其他所有内容/apps/**之前有可能进行匹配。/**

"rewrites": [
  {
    "source": "/apps/**",
    "destination": "/apps.html"
  },
  {
    "source": "**",
    "destination": "/index.html"
  }
]

推荐阅读