Reactjs Firebase主机与react路由器
我有一个react应用程序,它使用react路由器和一个看起来像:Reactjs Firebase主机与react路由器,reactjs,firebase,react-router,firebase-hosting,Reactjs,Firebase,React Router,Firebase Hosting,我有一个react应用程序,它使用react路由器和一个看起来像: <Router> <div> <Route exact path="/" component={Homepage} /> <Route path="/map/:uid" component={Userpage} /> <Footer /> </div> </Router> 我不再收到“未找到页面”页面;但是,我的
<Router>
<div>
<Route exact path="/" component={Homepage} />
<Route path="/map/:uid" component={Userpage} />
<Footer />
</div>
</Router>
我不再收到“未找到页面”页面;但是,我的react应用程序的内容从未加载,我在控制台中注意到一个错误:
Uncaught SyntaxError: Unexpected token <
未捕获的语法错误:意外标记<
更新#2
我现在明白为什么我会出错了。查看Chrome开发工具中的Sources选项卡,只有当我直接导航到/map/{known uid}
时,我的static/文件夹才有一个奇怪的名称(static/css
),而不是static
)。当我导航到主页时,所有加载都正常
这就是错误的原因。我仍然不知道如何修复。在
路由器中指定basename
怎么样?沿着这一点:
<Router basename='/map/5AJA3RefFuTZ8z4Gn6BjMgZRgPZ2'>
对我来说,我可以看到根url,但其他路线,如“/pricing”给了我404。我把它添加到我的firebase.json文件中,现在它可以工作了。另外,请确保允许firebase/auth在域上工作。firebase的auth部分中有一个设置
"rewrites": [ {
"source": "**",
"destination": "/index.html"
}],
我的完整firebase.json
{
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"hosting": {
"public": "build",
"rewrites": [ {
"source": "**",
"destination": "/index.html"
}],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
},
"storage": {
"rules": "storage.rules"
}
}
迟到,但请尝试从package.json中删除“homepage”键(或确保它相对于主页的存储位置是正确的)。迟到回答,但我面临同样的问题。我通过两个步骤解决了这个问题:
像这样更新firebase.json
{
“托管”:{
“站点”:“myproject”,
“公共”:“构建”,
“忽略”:[
“firebase.json”,
"**/.*",
“**/node_模块/**”
],
“重写”:[
{
“来源”:“**”,
“目的地”:“/index.html”
}
]
}
}
回答晚了,但它解决了我的问题:
执行firebase init
时,它会询问它是单页应用程序还是否。默认答案是no
,但是,只需选择Yes
即可
{
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"hosting": {
"public": "build",
"rewrites": [ {
"source": "**",
"destination": "/index.html"
}],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
},
"storage": {
"rules": "storage.rules"
}
}