From 9721839b0570e88c0786362d7c84e91795d1a11c Mon Sep 17 00:00:00 2001 From: 11smiley11 Date: Tue, 29 Aug 2023 21:58:56 -0600 Subject: [PATCH] Added handling for reloading on different endpoints. Test my website as a example go to to /mechanical and try reloading the page. --- README.md | 227 ++++++++++++++++++++++++---------------------- public/404.html | 50 ++++++++++ public/index.html | 30 ++++++ 3 files changed, 199 insertions(+), 108 deletions(-) create mode 100644 public/404.html diff --git a/README.md b/README.md index 94201e361..974950c63 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Deploying a React App* to GitHub Pages +# Deploying a React App\* to GitHub Pages \* created using `create-react-app` @@ -16,21 +16,22 @@ If you follow along with this tutorial, you'll end up with a new React app—hos 1. [Node and npm](https://nodejs.org/en/download/) are installed. Here are the versions I'll be using while making this tutorial: - ```shell - $ node --version - v16.13.2 + ```shell + $ node --version + v16.13.2 - $ npm --version - 8.1.2 - ``` - > Installing npm adds two commands to the system—`npm` and `npx`—both of which I'll be using while making this tutorial. + $ npm --version + 8.1.2 + ``` + + > Installing npm adds two commands to the system—`npm` and `npx`—both of which I'll be using while making this tutorial. 2. [Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) is installed. Here's the version I'll be using while making this tutorial: - ```shell - $ git --version - git version 2.29.1.windows.1 - ``` + ```shell + $ git --version + git version 2.29.1.windows.1 + ``` 3. A [GitHub](https://github.com/signup) account. :octocat: @@ -41,25 +42,27 @@ If you follow along with this tutorial, you'll end up with a new React app—hos 1. Sign into your GitHub account. 2. Visit the [Create a new repository](https://github.com/new) form. 3. Fill in the form as follows: - - **Repository name:** You can enter any name you want\*. - > \* For a [project site](https://pages.github.com/#project-site), you can enter any name you want. For a [user site](https://pages.github.com/#user-site), GitHub [requires](https://docs.github.com/en/pages/getting-started-with-github-pages/about-github-pages#types-of-github-pages-sites) that the repository's name have the following format: `{username}.github.io` (e.g. `gitname.github.io`) - - > The name you enter will show up in a few places: (a) in references to the repository throughout GitHub, (b) in the URL of the repository, and (c) in the URL of the deployed React app. + - **Repository name:** You can enter any name you want\*. - > In this tutorial, I'll be deploying the React app as a project site. + > \* For a [project site](https://pages.github.com/#project-site), you can enter any name you want. For a [user site](https://pages.github.com/#user-site), GitHub [requires](https://docs.github.com/en/pages/getting-started-with-github-pages/about-github-pages#types-of-github-pages-sites) that the repository's name have the following format: `{username}.github.io` (e.g. `gitname.github.io`) + + > The name you enter will show up in a few places: (a) in references to the repository throughout GitHub, (b) in the URL of the repository, and (c) in the URL of the deployed React app. + + > In this tutorial, I'll be deploying the React app as a project site. + + I'll enter: `react-gh-pages` - I'll enter: `react-gh-pages` - - **Repository privacy:** Select _Public_ (or _Private_\*). - > \* For [GitHub Free](https://docs.github.com/en/get-started/learning-about-github/githubs-products#github-free-for-user-accounts) users, the only type of repository that can be used with GitHub Pages is _Public_. For [GitHub Pro](https://docs.github.com/en/get-started/learning-about-github/githubs-products#github-pro) users (and other paying users), both _Public_ and _Private_ repositories can be used with GitHub Pages. + > \* For [GitHub Free](https://docs.github.com/en/get-started/learning-about-github/githubs-products#github-free-for-user-accounts) users, the only type of repository that can be used with GitHub Pages is _Public_. For [GitHub Pro](https://docs.github.com/en/get-started/learning-about-github/githubs-products#github-pro) users (and other paying users), both _Public_ and _Private_ repositories can be used with GitHub Pages. - I'll choose: _Public_ + I'll choose: _Public_ - **Initialize repository:** Leave all checkboxes empty. - > That will make it so GitHub creates an empty repository, instead of pre-populating the repository with a `README.md`, `.gitignore`, and/or `LICENSE` file. + > That will make it so GitHub creates an empty repository, instead of pre-populating the repository with a `README.md`, `.gitignore`, and/or `LICENSE` file. + 4. Submit the form. At this point, your GitHub account contains an empty repository, having the name and privacy type that you specified. @@ -68,138 +71,146 @@ At this point, your GitHub account contains an empty repository, having the name 1. Create a React app named `my-app`: - > In case you want to use a different name from `my-app` (e.g. `web-ui`), you can accomplish that by replacing all occurrences of `my-app` in this tutorial, with that other name (i.e. `my-app` --> `web-ui`). - - ```shell - $ npx create-react-app my-app - ``` + > In case you want to use a different name from `my-app` (e.g. `web-ui`), you can accomplish that by replacing all occurrences of `my-app` in this tutorial, with that other name (i.e. `my-app` --> `web-ui`). + + ```shell + $ npx create-react-app my-app + ``` - > That command will create a React app written in JavaScript. To create one written in [TypeScript](https://create-react-app.dev/docs/adding-typescript/#installation), you can issue this command instead: - > ```shell - > $ npx create-react-app my-app --template typescript - > ``` + > That command will create a React app written in JavaScript. To create one written in [TypeScript](https://create-react-app.dev/docs/adding-typescript/#installation), you can issue this command instead: + > + > ```shell + > $ npx create-react-app my-app --template typescript + > ``` - That command will create a new folder named `my-app`, which will contain the source code of a React app. + That command will create a new folder named `my-app`, which will contain the source code of a React app. - > In addition to containing the source code of the React app, that folder is also a Git repository. That characteristic of the folder will come into play in Step 6. + > In addition to containing the source code of the React app, that folder is also a Git repository. That characteristic of the folder will come into play in Step 6. - > #### Branch names: `master` vs. `main` - > - > The Git repository will have one branch, which will be named either (a) `master`, the default for a fresh Git installation; or (b) the value of the Git configuration variable, `init.defaultBranch`, if your computer is running Git version 2.28 or later _and_ you have [set that variable](https://github.blog/2020-07-27-highlights-from-git-2-28/#introducing-init-defaultbranch) in your Git configuration (e.g. via `$ git config --global init.defaultBranch main`). - > - > Since I have not set that variable in my Git installation, the branch in my repository will be named `master`. In case the branch in your repository has a different name (which you can check by running `$ git branch`), such as `main`; you can **replace** all occurrences of `master` throughout the remainder of this tutorial, with that other name (e.g. `master` → `main`). + > #### Branch names: `master` vs. `main` + > + > The Git repository will have one branch, which will be named either (a) `master`, the default for a fresh Git installation; or (b) the value of the Git configuration variable, `init.defaultBranch`, if your computer is running Git version 2.28 or later _and_ you have [set that variable](https://github.blog/2020-07-27-highlights-from-git-2-28/#introducing-init-defaultbranch) in your Git configuration (e.g. via `$ git config --global init.defaultBranch main`). + > + > Since I have not set that variable in my Git installation, the branch in my repository will be named `master`. In case the branch in your repository has a different name (which you can check by running `$ git branch`), such as `main`; you can **replace** all occurrences of `master` throughout the remainder of this tutorial, with that other name (e.g. `master` → `main`). 2. Enter the newly-created folder: - - ```shell - $ cd my-app - ``` + + ```shell + $ cd my-app + ``` At this point, there is a React app on your computer and you are in the folder that contains its source code. All of the remaining commands shown in this tutorial can be run from that folder. ### 3. Install the `gh-pages` npm package 1. Install the [`gh-pages`](https://github.com/tschaub/gh-pages) npm package and designate it as a [development dependency](https://docs.npmjs.com/specifying-dependencies-and-devdependencies-in-a-package-json-file): - - ```shell - $ npm install gh-pages --save-dev - ``` + + ```shell + $ npm install gh-pages --save-dev + ``` At this point, the `gh-pages` npm package is installed on your computer and the React app's dependence upon it is documented in the React app's `package.json` file. ### 4. Add a `homepage` property to the `package.json` file -1. Open the `package.json` file in a text editor. - +1. Open the `package.json` file in a text editor. + ```shell $ vi package.json ``` > In this tutorial, the text editor I'll be using is [vi](https://www.vim.org/). You can use any text editor you want; for example, [Visual Studio Code](https://code.visualstudio.com/). -2. Add a `homepage` property in this format\*: `https://{username}.github.io/{repo-name}` +2. Add a `homepage` property in this format\*: `https://{username}.github.io/{repo-name}` - > \* For a [project site](https://pages.github.com/#project-site), that's the format. For a [user site](https://pages.github.com/#user-site), the format is: `https://{username}.github.io`. You can read more about the `homepage` property in the ["GitHub Pages" section](https://create-react-app.dev/docs/deployment/#github-pages) of the `create-react-app` documentation. + > \* For a [project site](https://pages.github.com/#project-site), that's the format. For a [user site](https://pages.github.com/#user-site), the format is: `https://{username}.github.io`. You can read more about the `homepage` property in the ["GitHub Pages" section](https://create-react-app.dev/docs/deployment/#github-pages) of the `create-react-app` documentation. - ```diff - { - "name": "my-app", - "version": "0.1.0", - + "homepage": "https://gitname.github.io/react-gh-pages", - "private": true, - ``` -At this point, the React app's `package.json` file includes a property named `homepage`. + ```diff + { + "name": "my-app", + "version": "0.1.0", + + "homepage": "https://gitname.github.io/react-gh-pages", + "private": true, + ``` + + At this point, the React app's `package.json` file includes a property named `homepage`. ### 5. Add deployment scripts to the `package.json` file 1. Open the `package.json` file in a text editor (if it isn't already open in one). - - ```shell - $ vi package.json - ``` + + ```shell + $ vi package.json + ``` 2. Add a `predeploy` property and a `deploy` property to the `scripts` object: - ```diff - "scripts": { - + "predeploy": "npm run build", - + "deploy": "gh-pages -d build", - "start": "react-scripts start", - "build": "react-scripts build", - ``` + ```diff + "scripts": { + + "predeploy": "npm run build", + + "deploy": "gh-pages -d build", + "start": "react-scripts start", + "build": "react-scripts build", + ``` -At this point, the React app's `package.json` file includes deployment scripts. +At this point, the React app's `package.json` file includes deployment scripts. ### 6. Add a "remote" that points to the GitHub repository 1. Add a "[remote](https://git-scm.com/docs/git-remote)" to the local Git repository. - You can do that by issuing a command in this format: - - ```shell - $ git remote add origin https://github.com/{username}/{repo-name}.git - ``` - - To customize that command for your situation, replace `{username}` with your GitHub username and replace `{repo-name}` with the name of the GitHub repository you created in Step 1. + You can do that by issuing a command in this format: - In my case, I'll run: + ```shell + $ git remote add origin https://github.com/{username}/{repo-name}.git + ``` - ```shell - $ git remote add origin https://github.com/gitname/react-gh-pages.git - ``` + To customize that command for your situation, replace `{username}` with your GitHub username and replace `{repo-name}` with the name of the GitHub repository you created in Step 1. + + In my case, I'll run: + + ```shell + $ git remote add origin https://github.com/gitname/react-gh-pages.git + ``` - > That command tells Git where I want it to push things whenever I—or the `gh-pages` npm package acting on my behalf—issue the `$ git push` command from within this local Git repository. + > That command tells Git where I want it to push things whenever I—or the `gh-pages` npm package acting on my behalf—issue the `$ git push` command from within this local Git repository. At this point, the local repository has a "remote" whose URL points to the GitHub repository you created in Step 1. -### 7. Push the React app to the GitHub repository +### 7. Set up 404.html for single-page apps (React) + +To ensure that endpoints other than the index.html route (username.github.io/endpoint) are found and handled correctly by Github Pages, you need to ensure that the custom 404.html file is set correctly for your scenario. **Ensure you correctly set the pathSegmentsToKeep variable**. +This code and idea came from [spa-github-pages](https://github.com/rafgraph/spa-github-pages) + +### 8. Push the React app to the GitHub repository 1. Push the React app to the GitHub repository - ```shell - $ npm run deploy - ``` + ```shell + $ npm run deploy + ``` - > That will cause the `predeploy` and `deploy` scripts defined in `package.json` to run. - > - > Under the hood, the `predeploy` script will build a distributable version of the React app and store it in a folder named `build`. Then, the `deploy` script will push the contents of that folder to a new commit on the `gh-pages` branch of the GitHub repository, creating that branch if it doesn't already exist. + > That will cause the `predeploy` and `deploy` scripts defined in `package.json` to run. + > + > Under the hood, the `predeploy` script will build a distributable version of the React app and store it in a folder named `build`. Then, the `deploy` script will push the contents of that folder to a new commit on the `gh-pages` branch of the GitHub repository, creating that branch if it doesn't already exist. - > By default, the new commit on the `gh-pages` branch will have a commit message of "Updates". You can [specify a custom commit message](https://github.com/gitname/react-gh-pages/issues/80#issuecomment-1042449820) via the `-m` option, like this: - > ```shell - > $ npm run deploy -- -m "Deploy React app to GitHub Pages" - > ``` + > By default, the new commit on the `gh-pages` branch will have a commit message of "Updates". You can [specify a custom commit message](https://github.com/gitname/react-gh-pages/issues/80#issuecomment-1042449820) via the `-m` option, like this: + > + > ```shell + > $ npm run deploy -- -m "Deploy React app to GitHub Pages" + > ``` At this point, the GitHub repository contains a branch named `gh-pages`, which contains the files that make up the distributable version of the React app. However, we haven't configured GitHub Pages to _serve_ those files yet. -### 8. Configure GitHub Pages +### 9. Configure GitHub Pages 1. Navigate to the **GitHub Pages** settings page 1. In your web browser, navigate to the GitHub repository 1. Above the code browser, click on the tab labeled "Settings" 1. In the sidebar, in the "Code and automation" section, click on "Pages" -1. Configure the "Build and deployment" settings like this: +1. Configure the "Build and deployment" settings like this: 1. **Source**: Deploy from a branch - 2. **Branch**: + 2. **Branch**: - Branch: `gh-pages` - Folder: `/ (root)` 1. Click on the "Save" button @@ -208,7 +219,7 @@ At this point, the GitHub repository contains a branch named `gh-pages`, which c At this point, the React app is accessible to anyone who visits the `homepage` URL you specified in Step 4. For example, the React app I deployed is accessible at https://gitname.github.io/react-gh-pages. -### 9. (Optional) Store the React app's _source code_ on GitHub +### 10. (Optional) Store the React app's _source code_ on GitHub In a previous step, the `gh-pages` npm package pushed the distributable version of the React app to a branch named `gh-pages` in the GitHub repository. However, the _source code_ of the React app is not yet stored on GitHub. @@ -216,13 +227,13 @@ In this step, I'll show you how you can store the source code of the React app o 1. Commit the changes you made while you were following this tutorial, to the `master` branch of the local Git repository; then, push that branch up to the `master` branch of the GitHub repository. - ```shell - $ git add . - $ git commit -m "Configure React app for deployment to GitHub Pages" - $ git push origin master - ``` + ```shell + $ git add . + $ git commit -m "Configure React app for deployment to GitHub Pages" + $ git push origin master + ``` - > I recommend exploring the GitHub repository at this point. It will have two branches: `master` and `gh-pages`. The `master` branch will contain the React app's source code, while the `gh-pages` branch will contain the distributable version of the React app. + > I recommend exploring the GitHub repository at this point. It will have two branches: `master` and `gh-pages`. The `master` branch will contain the React app's source code, while the `gh-pages` branch will contain the distributable version of the React app. # References @@ -234,11 +245,11 @@ In this step, I'll show you how you can store the source code of the React app o - Special thanks to GitHub (the company) for providing us with the GitHub Pages hosting service for free. - And now, time to turn the default React app generated by `create-react-app` into something unique! -- This repository consists of two branches: - - `master` - the _source code_ of the React app - - `gh-pages` - the React app _built from_ that source code +- This repository consists of two branches: + - `master` - the _source code_ of the React app + - `gh-pages` - the React app _built from_ that source code - # Contributors +# Contributors Thanks to these people for contributing to the maintenance of this tutorial. @@ -288,4 +299,4 @@ Note: I specified the avatars using HTML because, when I did so using Markdown, jackweyhrich -This list is maintained manually—for now—and includes (a) each person who submitted a pull request that was eventually merged into `master`, and (b) each person who contributed in a different way (e.g. providing constructive feedback) and who approved of me including them in this list. \ No newline at end of file +This list is maintained manually—for now—and includes (a) each person who submitted a pull request that was eventually merged into `master`, and (b) each person who contributed in a different way (e.g. providing constructive feedback) and who approved of me including them in this list. diff --git a/public/404.html b/public/404.html new file mode 100644 index 000000000..391c49c44 --- /dev/null +++ b/public/404.html @@ -0,0 +1,50 @@ + + + + + Single Page Apps for GitHub Pages + + + + diff --git a/public/index.html b/public/index.html index aa069f27c..084644e02 100644 --- a/public/index.html +++ b/public/index.html @@ -25,6 +25,36 @@ Learn how to configure a non-root public URL by running `npm run build`. --> React App + + +