What is a github page?
GitHub Pages is a service created by GitHub that allows publishing websites or web applications by hosting it in the free GitHub repository. You can host a complete website and unlimited project pages, which can be considered a “page” on the website. The website code is stored in the specified GitHub repository, then GitHub will be published for viewing on any computer or tablet. For static websites or small web applications, there are some free hosting plans that you can start up and run online right away. Usually they require less setup than a paid server.
GitHub Pages is one of such free solutions, in this article I will mention deploying a static page using Reactjs on github page.
User manual:
In order to use the github page, you must have a github account.
Once you have a github account, we proceed to create a repo to store the source code After creating the repo, we proceed to clone to the local machine, so it’s done preparing
Now to the code, to create a reactjs project we use the command
1 2 3 | cd /path/to/repo create-react-app . |
After you have finished running, use the following command to check if the project is working
1 2 | yarn start |
Next we install the gh-pages
package to complie the code and serve for deploying
1 2 | yarn add gh-pages --save-dev |
Then we go to package.json
config file as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <span class="token punctuation">{</span> <span class="token property">"homepage"</span> <span class="token operator">:</span> <span class="token string">"http://iamPhong.github.io/mycv"</span> <span class="token punctuation">,</span> <span class="token property">"name"</span> <span class="token operator">:</span> <span class="token string">"mycv"</span> <span class="token punctuation">,</span> <span class="token property">"version"</span> <span class="token operator">:</span> <span class="token string">"0.1.0"</span> <span class="token punctuation">,</span> <span class="token property">"private"</span> <span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">,</span> <span class="token property">"dependencies"</span> <span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token property">"@testing-library/jest-dom"</span> <span class="token operator">:</span> <span class="token string">"^4.2.4"</span> <span class="token punctuation">,</span> <span class="token property">"@testing-library/react"</span> <span class="token operator">:</span> <span class="token string">"^9.3.2"</span> <span class="token punctuation">,</span> <span class="token property">"@testing-library/user-event"</span> <span class="token operator">:</span> <span class="token string">"^7.1.2"</span> <span class="token punctuation">,</span> <span class="token property">"gh-pages"</span> <span class="token operator">:</span> <span class="token string">"^2.2.0"</span> <span class="token punctuation">,</span> <span class="token property">"react"</span> <span class="token operator">:</span> <span class="token string">"^16.12.0"</span> <span class="token punctuation">,</span> <span class="token property">"react-dom"</span> <span class="token operator">:</span> <span class="token string">"^16.12.0"</span> <span class="token punctuation">,</span> <span class="token property">"react-scripts"</span> <span class="token operator">:</span> <span class="token string">"3.4.0"</span> <span class="token punctuation">}</span> <span class="token punctuation">,</span> <span class="token property">"scripts"</span> <span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token property">"predeploy"</span> <span class="token operator">:</span> <span class="token string">"yarn run build"</span> <span class="token punctuation">,</span> <span class="token property">"deploy"</span> <span class="token operator">:</span> <span class="token string">"gh-pages -d build"</span> <span class="token punctuation">,</span> <span class="token property">"start"</span> <span class="token operator">:</span> <span class="token string">"react-scripts start"</span> <span class="token punctuation">,</span> <span class="token property">"build"</span> <span class="token operator">:</span> <span class="token string">"react-scripts build"</span> <span class="token punctuation">,</span> <span class="token property">"test"</span> <span class="token operator">:</span> <span class="token string">"react-scripts test"</span> <span class="token punctuation">,</span> <span class="token property">"eject"</span> <span class="token operator">:</span> <span class="token string">"react-scripts eject"</span> <span class="token punctuation">}</span> <span class="token punctuation">,</span> <span class="token property">"eslintConfig"</span> <span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token property">"extends"</span> <span class="token operator">:</span> <span class="token string">"react-app"</span> <span class="token punctuation">}</span> <span class="token punctuation">,</span> <span class="token property">"browserslist"</span> <span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token property">"production"</span> <span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token string">">0.2%"</span> <span class="token punctuation">,</span> <span class="token string">"not dead"</span> <span class="token punctuation">,</span> <span class="token string">"not op_mini all"</span> <span class="token punctuation">]</span> <span class="token punctuation">,</span> <span class="token property">"development"</span> <span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token string">"last 1 chrome version"</span> <span class="token punctuation">,</span> <span class="token string">"last 1 firefox version"</span> <span class="token punctuation">,</span> <span class="token string">"last 1 safari version"</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
Here we add a property as homepage: http://iamPhong.github.io/mycv
with the url formatted like this: http://{username}.github.io/{repo-name}
, username
is The user is entering github, repo-name
is the name of the repo just created, my here is mycv
. The next parameter to watch out for is:
1 2 3 | "predeploy": "npm run build", "deploy": "gh-pages -d build" |
This is the configuration that uses the gh-page
package to deploy to github. After configuring, we push the code to github
1 2 3 4 | git add . git commit -m "Your awesome message" git push origin master |
Finally, deploy deploy with the command
1 2 | yarn run deploy |
Once deployed, we will go to the settings of the current repository on github to see the following settings:
We can see that our github page is public at url: https://{username}.github.io/{repo-name}
, and the source code of this static page is the gh-pages
branch.
So what did package gh-pages
actually do ?. We proceed to checkout to gh-pages
Here we see all the code in the master branch will be complied back into files and folders so that the github page can understand, so later, just merge the code into the master and deploy with the yarn run
command, branch gh-pages
will be updated again.
So now we can work with reactjs normally, deploy task has pakage gh-pages
lo )
Conclude:
So I have introduced you how to deploy a static web application with reactjs on github pages. You can enjoy creative with github page and reactjs without worrying about the cost.
Here I also wrote a cv page with reactjs and typescript on the github page here
A few screenshots
It looks dangerous =))
Because new to reactjs, so the code is very disgusting, if you are interested, you can clone your source code at https://github.com/iamPhong/mycv and modify it accordingly.
I wish you a new day of working effectively and full of energy
Refer:
https://dev.to/yuribenjamin/how-to-deploy-react-app-in-github-pages-2a1f