React-Router:Gitlab页面刷新时显示404错误

时间:2019-01-11 20:54:25

标签: reactjs react-router gitlab gitlab-pages

我是新来响应和gitlab页面托管的对象,目前我正在gitlab-pages上托管我的第一个应用程序。几天前,我添加了react-router-dom在特定页面/路线之间导航。路由工作正常,但是当我尝试刷新gitlab上的任何页面时,期望的是“ /”主域,我收到404错误(例如,我在/ booked路由上按im,然后按F5,我的gitlab给我404错误)。我的配置有什么问题吗?在下面看到它:

...
import { Route, BrowserRouter as Router } from "react-router-dom";
const routing = (
 <Router basename={process.env.PUBLIC_URL}>
  <div>
    <Route exact path="/" component={Home} />

    <Route path="/booking" render={props => <Child {...props} />} />
    <Route
     path="/booked" render={props => <ChildChild{...props} />}
    />
    <Route path="/bookedd" component={ChildChildChild} />
   </div>
  </Router>
   );

ReactDOM.render(routing, document.getElementById("root"));   

这是我的gitlab-ci.yml:

test:
  image: node:9.6.1
  stage: test
  script:
     - npm install;  npm run test
  pages:
  image: node:9.6.1
  stage: deploy
  variables:
    PUBLIC_URL: "/pageName"
  script:
    - rm package.json; mv package.json.pages package.json; 
      npm install;npm install react-scripts@1.1.1 -g;npm run build
    - rm -rf public; mv build public
  artifacts:
  paths:
    - public 
  only:
    - master 

如果没有针对404错误的真正解决方案,是否可以将任何刷新尝试重定向到“ /”本地路由?

1 个答案:

答案 0 :(得分:0)

最简单的解决方案是使用HashRouter而不是BrowserRouter ...它将在项目名称后的URL中放置#