Proper way to configure Firebase Hosting i18n with Angular SPA

huangapple go评论56阅读模式
英文:

Proper way to configure Firebase Hosting i18n with Angular SPA

问题

我正在尝试配置Firebase来托管一个带有多种语言的Angular单页应用程序。我希望Firebase能够检测用户的首选语言,并为用户提供相应语言版本的页面。

我已经按照官方文档的说明进行了配置,并成功地为路径"/"提供了服务,因为它提供了index.html文件。如果您从根路径开始导航,似乎一切正常,但当您刷新页面或直接输入不同的路径时,问题就出现了,会返回一个404错误。

我的firebase.json文件如下:

{
    "hosting": {
        "public": "website/dist",
        "i18n": {
            "root": "/localized-files"
        },
        ...
    },
    ...
}

我的项目结构如下:

/website(Angular项目)
    /dist(编译后的文件)
        /localized-files
            /en_ALL
                /assets
                index.html
            /es_ALL
                /assets
                index.html
            /it_ALL
                /assets
                index.html

    /src
    ...

问题是,如何配置使所有路径都指向正确的index.html文件。

英文:

I am trying to configure Firebase to host an Angular SPA with several languages. I want to Firebase detect the preferred user language and serve its version to the user.

I have followed the official documentation and it works perfect to serve the path "/" because it serves the index.html. If you start your navigation from the root it seems to work fine, but the problem comes when you refresh the page or enter a different path directly. It returns a 404.

My firebase.json looks like:

{
    "hosting": {
        "public": "website/dist",
        "i18n": {
            "root": "/localized-files"
        },
    ...
    },
    ...
}

My project structure is:

/website (Angular project)
    /dist (Compiled files)
         /localized-files
             /en_ALL
                 /assets
                 index.html
             /es_ALL
                 /assets
                 index.html
             /it_ALL
                 /assets
                 index.html

    /src
    ...

The question is, how to configure that all paths to point to the proper index.html.

答案1

得分: 0

如果有人面临相同的问题,解决方法非常简单:

只需将以下设置添加到hosting对象中:

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

Firebase 应该指定这个规则将会重写到适当的 i18n 文件夹中的 index.html 文件。

英文:

If anyone is facing the same problem, the solution is quite easy:

Just add this settings inside the hosting object:

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

Firebase should specify that this rule is going to rewrite to the index.html file in the proper i18n folder.

huangapple
  • 本文由 发表于 2023年2月24日 00:14:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定