React Router v4:无法在 GitHub 页面上加载页面

React Router v4: Cant load page on GitHub pages

在本地我可以浏览到 /buttons 就好了我什至可以刷新它并且它也很好,但是当我将构建目录上传到 github 页面时我无法访问 /buttons 而我得到了GitHub 404 页面,不是我自己的 'notfound' 页面。

如果我从主页创建一个 link 到 /buttons,按钮将加载,但直接浏览它不会加载。

import React, { useState } from 'react';
import { BrowserRouter, Switch, Route } from 'react-router-dom';
import { Context } from "./components/context";
import { Layout } from "./components/layout";
import { Home } from './routes/home';
import { Buttons } from './routes/buttons';
import { NotFound } from './routes/notfound';

const Router: React.FC = () => {

  const [global, setGlobal] = useState({
    language: localStorage.getItem("language") || 'en',
    apiUrl: 'https://api.domain.com/api',
    loggedIn: localStorage.getItem("jwt") ? true : false,
    redirectUrl: '',
    modal: false,
    modalState: '',
  });

  return (
    <Context.Provider value={{ global, setGlobal }}>
      <BrowserRouter basename={process.env.PUBLIC_URL}>
        <Route render = {({ location }) => (
          <Layout location = { location }>
            <Switch location = { location }>
              <Route exact path = '/' component = { Home } />
              <Route exact path = '/buttons/' component = { Buttons } />
              <Route component = { NotFound }/>
            </Switch>
          </Layout>
        )} />
      </BrowserRouter>
    </Context.Provider>
  );
}
export { Router };

在 package.json 中,我确实将主页定义为:

"homepage": "https://myName.github.io/myRepo",

根据 https://create-react-app.dev/ 的部署文档,对于 GH 页面:

GitHub Pages doesn’t support routers that use the HTML5 pushState history API under the hood (for example, React Router using browserHistory). This is because when there is a fresh page load for a url like <a href="http://user.github.io/todomvc/todos/42" rel="noreferrer">http://user.github.io/todomvc/todos/42</a>, where /todos/42 is a frontend route, the GitHub Pages server returns 404 because it knows nothing of /todos/42.

如果您想将路由器添加到 GitHub 页面上托管的项目,这里有几个解决方案:

  • 您可以从使用 HTML5 历史记录 API 切换到使用哈希路由。如果你使用 React Router,你可以切换到 hashHistory 来实现这个效果,但是 URL 会更长更冗长(例如,http://user.github.io/todomvc/#/todos/42?_k=yknaj) Read more 关于 react-router 的不同历史实现。

更改路由:


    <BrowserRouter basename={process.env.PUBLIC_URL}>
      <Route render = {({ location }) => (
         <Layout location = { location }>
             <Switch location = { location }>
                <Route exact path = '/' component = { Home } />
                <Route exact path = '/buttons/' component = { Buttons } />
                <Route component = { NotFound }/>
              </Switch>
           </Layout>
       )} />
    </BrowserRouter>

收件人:

import { HashRouter, Route, Link } from "react-router-dom";

...


    <HashRouter basename={process.env.PUBLIC_URL}>
      <Route render = {({ location }) => (
         <Layout location = { location }>
             <Switch location = { location }>
                <Route exact path = '/' component = { Home } />
                <Route exact path = '/buttons/' component = { Buttons } />
                <Route component = { NotFound }/>
              </Switch>
           </Layout>
       )} />
    </HashRouter>
...

  • 或者,您可以使用一个技巧来教 GitHub 页面处理 404,方法是使用特殊的重定向参数重定向到您的 index.html 页面。在部署项目之前,您需要将带有重定向代码的 404.html 文件添加到构建文件夹,并且您需要将处理重定向参数的代码添加到 index.html。您可以在 this guide.
  • 中找到此技术的详细说明