在深度刷新Firebase之后反应生产路由器404

Abe*_*vez 4 firebase reactjs firebase-hosting

我的index.js中有这条路线

 <Router history={customHistory}>
        <div className="row">
            <Switch>
                <Route exact path="/login" component={Login}/>
                <Route path="/home" component={Home}/>
                <Route path="/politicas" component={Policies}/>

                <Redirect exact from="/" to="/login"/>
                <Route exact path="*" status={404} component={NotFound}/>

            </Switch>

        </div>
    </Router>
Run Code Online (Sandbox Code Playgroud)

在本地环境中总能正常工作,但是我有一个firebase应用程序,要部署我使用的firebase项目,我可以使用:

npm运行构建

火力基地部署

但是在firebase应用中,深度刷新返回404后,唯一可行的路由是“ /”,我该怎么做才能使路由始终与任何路径兼容?

Abe*_*vez 10

我回答自己,有必要将以下内容添加到文件firebase.json中

"hosting": {
  // Add the "rewrites" section within "hosting"
  "rewrites": [ {
    "source": "**",
    "destination": "/index.html"
  } ]
}
Run Code Online (Sandbox Code Playgroud)

  • Firebase 有他自己的服务器,当您导航到 **/something** 时,服务器认为该路径中有一个目录或文件,因此,当您像我在示例中放入的那样重写文件 firebase.json 时,所有流量都会index.html 文件和 React 路由器会接管所有这些流量。 (2认同)