我在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添加到根目录根本不是很优雅。

期望cleanUrls可以完成我将cleanUrlstrue设置为trailingSlash的技巧,然后站点立即进入无尽的重定向循环并无法加载。如何阻止Firebase在实际js或图像资产的根和尾部添加“ index.html”?

编辑:

按照弗兰克的要求,这是我正在使用的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"
        }]

    }]
}


附注:我尝试将trailingSlashcleanUrls都设置为true,并且还尝试分别设置这些true/false

最佳答案

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


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


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

10-05 21:20
查看更多