7

我在 Firebase Hosting 上看到带有斜杠的意外行为(至少在我看来)。我希望 Firebase 提供带有斜杠的 URL(托管目录中存在的实际文件除外)。为此,我trailingSlashtruefirebase.json 中设置。

这导致:

example.com                     redirect to     example.com/index.html/
example.com/js/script.js        redirect to     example.com/js/script.js/
example.com/images/image.png    redirect to     example.com/images/image.png/

预期的行为将是:

example.com                     redirect to example.com/
example.com/js/script.js        served as it is without any redirect
example.com/images/image.png    served as it is without any redirect

在实际文件 URL 中添加尾部斜杠会使浏览器/服务器认为script.js是目录而不是文件,并导致 404。将 index.html 添加到根目录并不优雅。

期望cleanUrls执行我设置的技巧cleanUrlstrue并且trailingSlash该站点立即进入无休止的重定向循环并且无法加载。如何阻止 Firebase 在根目录中添加“index.html”并为实际的 js 或图像资产添加斜杠?

编辑:

按照弗兰克的要求,这是我正在使用的 firebase.json:

{
    "firebase"          : "example",
    "public"            : "public",
    "cleanUrls"         : false,
    "trailingSlash"     : true,
    "ignore"            : [ "firebase.json", "**/.*", "**/node_modules/**" ],
    "rewrites": [{

        "source"        : "**",
        "destination"   : "/index.html"

    }],
    "headers": [{

        "source"    : "**/*.@(eot|otf|ttf|ttc|woff|font.css)",
        "headers"   : [{

            "key"   : "Access-Control-Allow-Origin",
            "value" : "*"
        }]

    }, {

        "source"    : "**/*.@(jpg|jpeg|gif|png)",
        "headers"   : [{
            "key"   : "Cache-Control",
            "value" : "max-age=600"
        }]

    }, {

        "source"    : "**/*.@(html|js)",
        "headers"   : [{
            "key"   : "Cache-Control",
            "value" : "max-age=300"
        }]

    }]
}

PS:我已经尝试设置trailingSlashcleanUrls设置true,也尝试true/false单独设置这些。

4

4 回答 4

3

https://firebase.google.com/docs/hosting/full-config#trailingslash

如果为 true,它将重定向 URL 以添加尾部斜杠。当为 false 时,它​​将重定向 URL 以删除尾部斜杠。未指定时,尾部斜杠仅用于目录索引文件(例如 about/index.html)。

只是不要添加该选项,它只会影响网址

于 2017-05-14T01:08:47.223 回答
0

至于你的问题,如果你想调用命令“rewrites”,当他在你的根目录中找不到任何 index.html 时,Server 可以执行“rewirtes”命令。

解决方案一:您只需将 index.html 放在“公共”文件夹下的根目录中。我切换到您的公用文件夹并部署您的服务器。

解决方案二:将 index.html 放在可以命名的某个文件夹下。然后使用 JSON 格式:"rewrites": [{ "source" : "**", "destination" : "/folderName/index.html" }],但您不能将任何 index.html 放在您的根文件夹下。

解决方案三:创建根文件夹,将 JS/CSS/IMG/ 文件夹和 index.html 放在根文件夹下。然后像这样部署服务器 JSON 文件。

{
"firebase": "you app name",
"public": ".",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
于 2016-01-29T18:50:15.970 回答
0

我敢肯定这对 Vinny 来说为时已晚,但我刚刚遇到了同样的问题。添加<base href='/'>到我的 html 文件中可以防止我出现额外的意外斜杠行为。

于 2019-09-24T04:38:06.360 回答
0
"hosting": {
  "public": "...",
  "ignore": [...],
  "trailingSlash": false
}

只需添加trailingSlash属性并将其设置为false将在 firebase 重定向时删除尾部斜杠。在此处查看他们的详细文档

于 2021-02-16T01:45:53.910 回答