英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论