[英]Uncaught SyntaxError: Identifier 'baseUrl' has already been declared
I have a Polymer webapp deployed using Firebase hosting. 我有一个使用Firebase托管部署的Polymer Webapp。
The routing between views works but the error page handling is not. 视图之间的路由有效,但错误页面处理无效。
I have succeeded to reproduce the issue on a minimal example using the official polymer-2-starter-kit example: 我已经使用官方的polymer-2-starter-kit示例成功地通过一个最小示例重现了该问题:
https://fir-polymer-404-issue.firebaseapp.com/ https://fir-polymer-404-issue.firebaseapp.com/
For instance, if you open the following URL, the error page is not displayed: 例如,如果您打开以下URL,则不会显示错误页面:
https://fir-polymer-404-issue.firebaseapp.com/not-existing https://fir-polymer-404-issue.firebaseapp.com/not-existing
Instead, I get the error below: 相反,我得到以下错误:
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
The configuration file firebase.json used for the previous example is here: 前一示例使用的配置文件firebase.json在这里:
{
"hosting": {
"public": ".",
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
I would like to have the error page handling made by Polymer. 我想要由Polymer进行错误页面处理。
Please note that the same app served by polymer serve
works properly. 请注意,由
polymer serve
的同一应用polymer serve
正常运行。
It seems the problem comes from the Firebase hosting configuration. 看来问题来自Firebase托管配置。 All traffic is redirected to index.html so when Polymer load a not-existing page, the Firebase server returns an HTTP 200 response.
所有流量都会重定向到index.html,因此,当Polymer加载不存在的页面时,Firebase服务器将返回HTTP 200响应。 Unfortunately, I have no idea how to fix the problem.
不幸的是,我不知道如何解决该问题。
I have tried to create a redirection for non-404 responses only with the following configuration file: 我尝试仅使用以下配置文件为非404响应创建重定向:
{
"hosting": {
"public": ".",
"redirects": [
{
"source": "**",
"destination": "/index.html",
"type": 200
}
]
}
}
Unfortunately, the type property can be used for 3xx code only: 不幸的是, type属性只能用于3xx代码:
Error: HTTP Error: 400, hosting.redirects[0].type is not one of enum values: 301,302,303,304,305,307,308
Please also note that a custom 404.html file is placed at the root . 另请注意,自定义404.html文件位于根目录中 。
The only solution I see is to list all existing routes (per file) but it looks just crazy. 我看到的唯一解决方案是列出所有现有路由(每个文件),但是看起来很疯狂。
Any idea is welcome. 任何想法都欢迎。
The reason neither firebase or polymer is going to handle your 404
page is that when you request a non-existing page, it's not only going to return with the status code 200
but it's also going to return with the HTML of the index
page, so it will display something, although that something is really nothing. Firebase或Polymer无法处理
404
页面的原因是,当您请求不存在的页面时,它不仅会返回状态码200
,还会返回index
页面的HTML,因此它会显示一些东西,尽管实际上什么都不是。
Now the way polymer is set up it looks for views in the src
folder, so you want a rewrite just on the root and not in the src
folder. 现在,聚合物的设置方式使它在
src
文件夹中查找视图,因此您只想在根目录而不是在src
文件夹中进行重写。 So change your firebase.json
to be 因此,将您的
firebase.json
更改为
{
"hosting": {
"public": ".",
"rewrites": [{
"source": "/*",
"destination": "/index.html"
}]
}
}
The single *
will mean files but not subfolders, this will allow your routeing to work if you enter it in the address bar but if the page is not found the 404
will be handled by polymers routeing. 单个
*
表示文件,但不表示子文件夹,如果在地址栏中输入它,将允许您进行路由选择,但是如果找不到该页面,则404
将由聚合物路由处理。 I've set up a firebase application using the polymer starter pack to for an example. 我以聚合物入门包为例,设置了一个Firebase应用程序。
https://testforsoissue.firebaseapp.com/view2 https://testforsoissue.firebaseapp.com/view2
Will work and will take you to view2
as the initial request will be rewritten to return index.html but the request for /src/my-view2.html
will not 将起作用,并带您进入
view2
因为初始请求将被重写以返回index.html,但对/src/my-view2.html
的请求将不会
Whereas a route that is not defined 而未定义的路线
https://testforsoissue.firebaseapp.com/not-existing https://testforsoissue.firebaseapp.com/not-existing
Will throw a 404 (in polymer) as the initial request will again be rewritten to return index.html
but the request for /src/my-not-existing.html
will not and will happily throw a 404! 将抛出404(在聚合物中),因为初始请求将再次被重写以返回
index.html
但对/src/my-not-existing.html
的请求将不会并且愉快地抛出404!
PS the reason for the error 'baseUrl' has already been declared'
is due to the fact the page was using the index.html twice which declares the baseUrl at the top PS错误
'baseUrl' has already been declared'
原因'baseUrl' has already been declared'
是由于以下事实:页面两次使用index.html,从而在顶部声明了baseUrl
Edit 编辑
If you have subpaths seems like you can use the firebase.json
like so 如果您有子路径好像你可以使用
firebase.json
像这样
{
"hosting": {
"public": ".",
"rewrites": [{
"source": "!/src/*",
"destination": "/index.html"
}]
}
}
As per documentation , you do not need to do the handling; 根据文档 ,您无需进行处理; just create a 404.html:
只需创建一个404.html:
You can specify a custom 404/Not Found error to be served when a user tries to access a page that does not exist.
您可以指定当用户尝试访问不存在的页面时要提供的自定义404 /未找到错误。 Simply add a 404.html page to the your project's public directory and the contents of this page will be displayed when Firebase Hosting needs to display a 404 Not Found error to the user.
只需将404.html页面添加到项目的公共目录中,当Firebase Hosting需要向用户显示404 Not Found错误时,将显示此页面的内容。
To avoid the baseUrl error, if you have access to this line of code: 为了避免baseUrl错误,如果您可以访问以下代码行:
let baseUrl = document.querySelector('base').href;
Try changing it for: 尝试将其更改为:
baseUrl = baseUrl || document.querySelector('base').href;
It's not very neat, but it should not complain about the value being defined already. 它不是很整洁,但是它不应该抱怨已经定义的值。
Although I think this is unrelated to your problem and may stop showing this error once a proper 404 page is defined. 尽管我认为这与您的问题无关,并且在定义正确的404页面后可能会停止显示此错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.