Lau*_*ent 8 javascript firebase polymer firebase-hosting
我使用Firebase托管部署了一个Polymer webapp.
视图之间的路由工作但错误页面处理不起作用.
我已成功使用官方聚合物-2启动器套件示例在最小的示例中重现该问题:
https://fir-polymer-404-issue.firebaseapp.com/
例如,如果您打开以下URL,则不会显示错误页面:
https://fir-polymer-404-issue.firebaseapp.com/not-existing
相反,我收到以下错误:
my-not-existing.html:56 Uncaught SyntaxError: Identifier 'baseUrl' has already been declared
at my-not-existing.html:56
(anonymous) @ my-not-existing.html:56
Run Code Online (Sandbox Code Playgroud)
用于前一个示例的配置文件firebase.json位于:
{
"hosting": {
"public": ".",
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我希望Polymer能够处理错误页面.
请注意,相同的应用程序polymer serve可以正常工作.
似乎问题来自Firebase托管配置.所有流量都会重定向到index.html,因此当Polymer加载不存在的页面时,Firebase服务器会返回HTTP 200响应.不幸的是,我不知道如何解决问题.
我试图仅使用以下配置文件为非404响应创建重定向:
{
"hosting": {
"public": ".",
"redirects": [
{
"source": "**",
"destination": "/index.html",
"type": 200
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,type属性只能用于3xx代码:
Error: HTTP Error: 400, hosting.redirects[0].type is not one of enum values: 301,302,303,304,305,307,308
Run Code Online (Sandbox Code Playgroud)
另请注意,自定义404.html文件位于根目录下.
我看到的唯一解决方案是列出所有现有路线(每个文件),但它看起来很疯狂.
欢迎任何想法.
firebase 或polymer 都不会处理您的404页面的原因是,当您请求一个不存在的页面时,它不仅会返回状态代码200,还会返回页面的 HTML index,因此它会显示某事,虽然那事其实不算什么。
现在,聚合物的设置方式是在文件夹中查找视图src,因此您需要仅在根目录而不是文件夹中进行重写src。所以改变你firebase.json是
{
"hosting": {
"public": ".",
"rewrites": [{
"source": "/*",
"destination": "/index.html"
}]
}
}
Run Code Online (Sandbox Code Playgroud)
单个*表示文件而不是子文件夹,如果您在地址栏中输入它,这将允许您的路由工作,但如果找不到页面,则将404由聚合物路由处理。我已经使用聚合物入门包设置了一个 firebase 应用程序作为示例。
https://testforsoissue.firebaseapp.com/view2
将会工作并带你去,view2因为初始请求将被重写以返回index.html,但请求/src/my-view2.html不会
而未定义的路线
https://testforsoissue.firebaseapp.com/not-existing
将抛出 404(聚合物),因为初始请求将再次被重写以返回index.html,但请求/src/my-not-existing.html不会并且很乐意抛出 404!
PS错误的原因'baseUrl' has already been declared'是由于页面使用了两次index.html,它在顶部声明了baseUrl
编辑
如果你有子路径似乎你可以使用firebase.json类似的
{
"hosting": {
"public": ".",
"rewrites": [{
"source": "!/src/*",
"destination": "/index.html"
}]
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1739 次 |
| 最近记录: |