67

我刚刚使用 Firebase CLI 启动了一个静态托管项目。当您启用“配置为单页应用程序”选项时究竟会发生什么?我正在寻找关于修改了哪些文件的描述,以及这对 Firebase 后端有什么影响。

firebase init 命令截图

4

5 回答 5

100

该选项只是在firebase.json文件中设置一个标志以将所有 URL 重定向到/index.html.

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

有关更多信息,请参阅Firebase 托管的文档,其中还包含这个更完整的示例:

"hosting": {
  // ...

 // Add the "rewrites" attribute within "hosting"
  "rewrites": [ {
    // Serves index.html for requests to files or directories that do not exist
    "source": "**",
    "destination": "/index.html"
  }, {
    // Serves index.html for requests to both "/foo" and "/foo/**"
    // Using "/foo/**" only matches paths like "/foo/xyz", but not "/foo"
    "source": "/foo{,/**}",
    "destination": "/index.html"
  }, {
    // Excludes specified pathways from rewrites
    "source": "!/@(js|css)/**",
    "destination": "/index.html"
  } ]
}
于 2016-06-07T16:06:49.280 回答
29

完整示例:

{
  "hosting": {
    "public": ".",
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}
于 2017-01-16T19:49:25.007 回答
17

如果您将其设置为是,那么所有无效的 URL 都www.example.com/some-invalid-url将被重定向到index.html您的站点,这是一件好事。您也可以将其设置为您的自定义404.html.

firebase.json

{

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

奖励:设置为cleanUrls从您部署的网站 urltrue中删除扩展名,否则所有没有的 url将重定向到..html.htmlindex.html

于 2019-11-22T16:21:14.793 回答
3

注意:如果您想要服务器端渲染 (SSR),请键入No并设置rewrites如下:

"rewrites": [
  {
    "function": "angularUniversalFunction",
    "source": "**"
  }
]

毕竟,无论您选择什么,您都可以随时在firebase.json文件中进行更改。

于 2019-07-29T07:37:43.497 回答
3

Firebase 官方解释:

我们去年(Q1 和 Q2)使用过这个选项,但它似乎什么也没做,但现在当我们应用它时,肯定会发生很大的不同。关于它的作用的完整官方解释在这里:

https://firebase.google.com/docs/hosting/url-redirects-rewrites#section-rewrites

在同一页面的下一部分中,甚至还有一些关于 Headers 使用的有用信息。

于 2018-08-15T03:28:53.950 回答