Firebase托管

时间:2016-01-27 17:32:38

标签: firebase firebase-hosting

我在Firebase Hosting上看到了意外行为(至少对我而言)有斜杠。我希望Firebase能够提供带有斜杠的URL(托管目录中存在的实际文件除外)。为此,我在firebase.json中将trailingSlash设置为true

这导致:

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添加到root中并不优雅。

期待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"
        }]

    }]
}

P.S。:我已尝试将trailingSlashcleanUrls设置为true,并尝试单独设置这些true/false

4 个答案:

答案 0 :(得分:1)

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

  

如果为true,则会重定向URL以添加尾部斜杠。什么时候假   它将重定向URL以删除尾部斜杠。如果未指定,   尾部斜杠仅用于目录索引文件(例如   约/ index.html中)。

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

答案 1 :(得分:0)

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

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

解决方案二:  你把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/**"
],

答案 2 :(得分:0)

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

答案 3 :(得分:0)

"hosting": {
  "public": "...",
  "ignore": [...],
  "trailingSlash": false
}

只需添加 trailingSlash 属性并将其设置为 false 即可在 firebase 重定向时删除尾部斜杠。查看他们的详细信息documentation here

相关问题