Skip to content
This repository was archived by the owner on Jan 26, 2019. It is now read-only.

Commit 8edfcb5

Browse files
authored
Better GH Pages guide
1 parent 6fca0fe commit 8edfcb5

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

packages/react-scripts/template/README.md

+17-4
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,9 @@ This will let Create React App correctly infer the root path to use in the gener
936936
937937
>Note: this feature is available with `react-scripts@0.2.0` and higher.
938938
939-
**THE BELOW STEP IS IMPORTANT!**<br>
939+
#### Step 1: Add `homepage` to `package.json`
940+
941+
**The below step is important!**<br>
940942
**If your skip it, your app will not deploy correctly.**
941943
942944
Open your `package.json` and add a `homepage` field:
@@ -947,7 +949,7 @@ Open your `package.json` and add a `homepage` field:
947949
948950
Create React App uses the `homepage` field to determine the root URL in the built HTML file.
949951
950-
**Have you added the `homepage` field? Then let's move on.**
952+
#### Step 2: Install `gh-pages` and add `deploy` to `scripts` in `package.json`
951953
952954
Now, whenever you run `npm run build`, you will see a cheat sheet with instructions on how to deploy to GitHub Pages.
953955
@@ -969,17 +971,28 @@ Add the following script in your `package.json`:
969971
970972
(Note: the lack of whitespace is intentional.)
971973
974+
#### Step 3: Deploy the site by running `npm run deploy`
975+
972976
Then run:
973977
974978
```sh
975979
npm run deploy
976980
```
977981
978-
**If you see 404 errors for JS and CSS, re-read the note at the beginning of this section saying "THE BELOW STEP IS IMPORTANT".**
982+
#### Step 4: Ensure your project's settings use `gh-pages`
983+
984+
Finally, make sure **GitHub Pages** option in your GitHub project settings is set to use the `gh-pages` branch:
985+
986+
<img src="http://i.imgur.com/HUjEr9l.png" width="500" alt="gh-pages branch setting">
987+
988+
#### Step 5: Optionally, configure the domain
979989
980990
You can configure a custom domain with GitHub Pages by adding a `CNAME` file to the `public/` folder.
981991
982-
Note that 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 `http://user.github.io/todomvc/todos/42`, where `/todos/42` is a frontend route, the GitHub Pages server returns 404 because it knows nothing of `/todos/42`. If you want to add a router to a project hosted on GitHub Pages, here are a couple of solutions:
992+
#### Notes on client-side routing
993+
994+
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 `http://user.github.io/todomvc/todos/42`, where `/todos/42` is a frontend route, the GitHub Pages server returns 404 because it knows nothing of `/todos/42`. If you want to add a router to a project hosted on GitHub Pages, here are a couple of solutions:
995+
983996
* You could switch from using HTML5 history API to routing with hashes. If you use React Router, you can switch to `hashHistory` for this effect, but the URL will be longer and more verbose (for example, `http://user.github.io/todomvc/#/todos/42?_k=yknaj`). [Read more](https://github.com/reactjs/react-router/blob/master/docs/guides/Histories.md#histories) about different history implementations in React Router.
984997
* Alternatively, you can use a trick to teach GitHub Pages to handle 404 by redirecting to your `index.html` page with a special redirect parameter. You would need to add a `404.html` file with the redirection code to the `build` folder before deploying your project, and you’ll need to add code handling the redirect parameter to `index.html`. You can find a detailed explanation of this technique [in this guide](https://github.com/rafrex/spa-github-pages).
985998

0 commit comments

Comments
 (0)