如何将一个简单的 React 应用程序作为 "user page" 部署到 Github 页面?

How do I deploy a simple React app as a "user page" to Github Pages?

我看了很多 Whosebug 的答案,这些答案回答了关于将 React 页面发布到 Github 的各种不同场景,但其中 none 清楚地解释了如何发布基本用户页面。我有一个非常简单的标准 React 应用程序,我想在 Github.

上作为用户页面发布

将简单的 React 应用程序作为 用户页面 发布到 Github 的基本步骤是什么?

总共可以在两个文档来源中找到答案。

假设您已经创建了 React 应用程序,在本地一切正常,并且您的应用程序已准备好部署,下面是将简单的 React 应用程序部署为 用户页面 Github:

  1. 遵循 Github regarding Github Pages 给出的指导...特别要注意 用户页面 仅来自 master 分支的 ,因此,用户页面将在 https://{your-github-user-name}.github.io.
  2. 处提供

User pages must be built from the master branch.

  1. 接下来,按照 Create React Apps documentation regarding Github Pages deployment 中提供的指导进行操作,尤其是与 用户页面.[=63= 相关的部分]

Open your package.json and add a homepage field that matches where your user page will be served from:

"homepage": "https://myusername.github.io",

Install the gh-pages module:

npm install --save gh-pages ... 或 ... yarn add gh-pages

Add deploy (and predeploy) to scripts in package.json:

  "scripts": {
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build",
    ... 
  },

Modify your deploy script to force push the built application to the master branch:

"deploy": "gh-pages -b master -d build",

Deploy your site by running:

npm run deploy

  1. 要克服的最后一个障碍是在 Github 处进行缓存。您可能需要 运行 npm run deploy 不止一次,以绕过 Github 对先前部署的缓存。