This repository uses the gh-pages npm package to build and deploy a React application. See a live, interactive version of the website here.
-
Navigate to the desired directory where the project will be stored.
-
Clone the repository by executing the following command.
git clone https://github.com/csong202/check-my-attendance.git
-
Install the required dependencies by executing the following command.
npm i
It is assumed that
npmis already installed. If not, refer to the official documentation.
-
Start a local server with the following command.
npm start
Executing the command above will open a live server which features hot reloading at the following address:
localhost:3000.
-
Commit and push any untracked files to the GitHub repository.
-
Build the app.
npm run deploy
By executing the command above, the
predeployanddeployscripts will run and the React app will be deployed. Internally, thepredeployscript creates a distributable version of the app and thebuildscript pushes the compiled app to a commit in thegh-pagesbranch. Thegh-pagespackage will deploy the application to the specified URL whenever thenpm run deploycommand is executed. A GitHub workflow will link the GitHub page with the source files in thegh-pagesbranch, and once it is completed, the deployed app with the newest changes will be reflected here.
More than likely, images and/or videos will not render in the deployed site using common src linking:
<img src="./images/img-1.jpg" />Since the website is deployed under the homepage URL, it will not recognize the source file for the image or video using local pathing. To overcome this, follow the steps below to change all src linking, depending on the use case.
-
Diagnose the type of media.
-
Background Image
-
Open the
.cssfile that imports an image with thebackground-imageproperty. -
Change the format of the
url()value.* { background-image: url("https://csong202.github.io/check-my-attendance/images/img-1.jpg"); }
The
url()value should follow this format:https://{github_username}.github.io/{repo_name}/{file_path}
-
-
Image/Video Tag
-
Open the
.htmlfile that utilizes the<img/>or<video/>tag. -
Change the format of the
srcattribute.<img src={process.env.PUBLIC_URL + "/images/img-1.jpg"} />
The
srcattribute's value should follow this format:process.env.PUBLIC_URL + "/{file_path}"
-
Alternatively, run the following two commands to match the two cases above:
cd $(git rev-parse --show-cdup)/src
grep -RIlxP --include=\*.{html,css,js} '^.*\b(?:src=|background\-image:).*$'
grepis a utility for searching strings through multiple text files. Here, it is invoked with the following parameters:R— reads all files under each directory, recursively, across all symbolic linksI— ignore binary files; process a binary file as if it did not contain matching datal— print the name of each file for which a match was foundx— select only those matches that exactly match the whole lineP— interpret patterns as Perl-compatible regular expressions (PCREs)--include=— search only files whose base name matches the pattern- Regex — find an explanation for the regular expression here
-
-
Push these changes to the remote repository and deploy the application. The global
process.env.PUBLIC_URLvariable allows for the images to be displayed when running locally and when being deployed to the remote.
Upon opening the deployed GitHub Pages site, the page might be blank until refreshing the browser. Follow the steps below to fix this.
-
Identify any
BrowserRoutertags in the codebase. -
Add the following property.
basename={process.env.PUBLIC_URL}Alternatively, run the following two commands to identify and replace the property:
cd $(git rev-parse --show-cdup)/src
grep -RIl --include=\*.js '<BrowserRouter' | xargs sed -i 's/<BrowserRouter/<BrowserRouter basename={process.env.PUBLIC_URL}/g'
Specifying the basename={process.env.PUBLIC_URL} in the routing root allows the router to extract the base URL of the project and properly display the site.