【问题标题】:How do I deploy a simple React app as a "user page" to Github Pages?如何将一个简单的 React 应用程序作为“用户页面”部署到 Github Pages?
【发布时间】:2020-07-04 21:24:55
【问题描述】:

我查看了许多 StackOverflow 答案,它们回答了有关将 React 页面发布到 Github 的各种不同场景,但没有一个清楚地解释如何发布基本用户页面。我有一个非常简单的标准 React 应用程序,我想在 Github 上作为用户页面发布。

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

【问题讨论】:

    标签: reactjs github-pages


    【解决方案1】:

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

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

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

    用户页面必须从主分支构建。

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

    打开您的 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

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

    【讨论】:

    • 优秀的答案。它强调 github repo 名称必须是 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.
    猜你喜欢
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多