【发布时间】:2020-07-04 21:24:55
【问题描述】:
我查看了许多 StackOverflow 答案,它们回答了有关将 React 页面发布到 Github 的各种不同场景,但没有一个清楚地解释如何发布基本用户页面。我有一个非常简单的标准 React 应用程序,我想在 Github 上作为用户页面发布。
将简单的 React 应用作为用户页面发布到 Github 的基本步骤是什么?
【问题讨论】:
标签: reactjs github-pages
我查看了许多 StackOverflow 答案,它们回答了有关将 React 页面发布到 Github 的各种不同场景,但没有一个清楚地解释如何发布基本用户页面。我有一个非常简单的标准 React 应用程序,我想在 Github 上作为用户页面发布。
将简单的 React 应用作为用户页面发布到 Github 的基本步骤是什么?
【问题讨论】:
标签: reactjs github-pages
总共可以在两个文档来源中找到答案。
假设您已经创建了 React 应用,本地一切正常,并且您的应用已准备好部署,以下是将简单的 React 应用部署为用户页面的步骤> 在 Github 上:
master分支,因此,用户页面将在https://{your-github-user-name}.github.io 提供。用户页面必须从主分支构建。
打开您的 package.json 并添加一个
homepage字段,该字段与您的用户页面将从何处提供:
"homepage": "https://myusername.github.io",
安装 gh-pages 模块:
npm install --save gh-pages ... 或 ... yarn add gh-pages
在 package.json 中将
deploy(和predeploy)添加到scripts:
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
...
},
修改您的
deploy脚本以强制将构建的应用程序推送到master分支:
"deploy": "gh-pages -b master -d build",
通过运行部署您的站点:
npm run deploy
npm run deploy 才能绕过 Github 对以前部署的缓存。【讨论】:
your-github-username.gitub.io。从您提供的第一个链接:Enter username.github.io as the repository name. Replace username with your GitHub username. For example, if your username is octocat, the repository name should be octocat.github.io.