我刚刚使用 Firebase CLI 启动了一个静态托管项目。当您启用“配置为单页应用程序”选项时究竟会发生什么?我正在寻找关于修改了哪些文件的描述,以及这对 Firebase 后端有什么影响。
5 回答
该选项只是在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" } ] }
完整示例:
{
"hosting": {
"public": ".",
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
如果您将其设置为是,那么所有无效的 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
.html
index.html
注意:如果您想要服务器端渲染 (SSR),请键入No并设置rewrites
如下:
"rewrites": [
{
"function": "angularUniversalFunction",
"source": "**"
}
]
毕竟,无论您选择什么,您都可以随时在firebase.json文件中进行更改。
Firebase 官方解释:
我们去年(Q1 和 Q2)使用过这个选项,但它似乎什么也没做,但现在当我们应用它时,肯定会发生很大的不同。关于它的作用的完整官方解释在这里:
https://firebase.google.com/docs/hosting/url-redirects-rewrites#section-rewrites
在同一页面的下一部分中,甚至还有一些关于 Headers 使用的有用信息。