简体   繁体   English

将Create-React-App部署到github

[英]Deploying Create-React-App to github

I am having a hard time deploying my create-react-app application to gh-pages. 我很难将我的create-react-app应用程序部署到gh-pages。 My landing page works just fine, but my two other pages dont load and I get a 404 error. 我的目标网页运行正常,但其他两个页面无法加载,并且出现404错误。 I found the documentation and it says gh-pages doesnt support routers that use HTML 5 pushState. 我找到了文档,并且说gh-pages不支持使用HTML 5 pushState的路由器。 What does that mean and how can I get my application to work without any issues? 这是什么意思?如何使我的应用程序正常运行?

app.js: app.js:

import React, { Component } from 'react';
import {HashRouter, Switch, Route} from 'react-router-dom';
import Navigation from './Components/Navigation';
import Landing from './Components/Landing';
import ToiletInfo from './Components/ToiletInfo';
import Location from './Components/Location';
import {data} from './data';
import './App.css';

class App extends Component {
render() {

 return (
  <HashRouter>
    <Switch>
      <div className="App">
      <Navigation />
      <Route 
        exact path='/' 
        render={(props) => <Landing {...props} data={data} />}
        />
        <Route 
        exact path='/ToiletInfo' 
        render={(props) => <ToiletInfo {...props} data={data} />}
        />
        <Route 
        exact path='/Location' 
        render={(props) => <Location {...props} data={data} />}
        />
        {/* // <Landing data={data}/> */}
      </div>
    </Switch>
  </HashRouter>
);
}
}


export default App;

In most of the single page apps having client side routing like react-router you need to tell your server to serve/render index.html in case of unmatched routes, which is something not possible in github pages and you need to follow this https://github.com/facebook/create-react-app/issues/1765 . 在大多数具有客户端路由的单页应用程序中,例如react-router您需要告诉服务器在路由不匹配的情况下提供/呈现index.html ,这在github页面中是不可能的,您需要遵循以下https: //github.com/facebook/create-react-app/issues/1765

Example for express: 快递示例:

app.get('/*', (req, res) => {
  res.sendFile(path.join(__dirname, '../build/index.html'));
});

But you can find another use cases here: https://angular.io/guide/deployment 但是您可以在这里找到其他用例: https : //angular.io/guide/deployment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM