简体   繁体   中英

GitHub Pages User Page Showing Blank Screen

I recently created a barebones React.js website and deployed it to GitHub pages as a user page, meaning the page should have a url of propertycashflowapp.github.io . The GitHub repo is public and can be found here .

Here is the directory tree:

├── README.md
├── index.html (this index.html is a duplicate of the one in the public directory)
├── package-lock.json
├── package.json
├── public
│   ├── favicon.ico
│   ├── index.html
│   ├── logo192.png
│   ├── logo512.png
│   ├── manifest.json
│   └── robots.txt
└── src
    ├── App.css
    ├── App.js
    ├── App.test.js
    ├── index.css
    ├── index.js
    ├── logo.svg
    ├── reportWebVitals.js
    └── setupTests.js

I followed all the steps in the GitHub Pages documentation. When I run npm start , the webpages shows up correctly. However after deployment when I type the url https://propertycashflowapp.github.io , I only see an empty screen.

I tried to move my index.html file to the root directory as was advised online, but it did not work. I also tried to change the line "homepage": "https://propertycashflowapp.github.io/" many times to include the path to the index.html file, but this did not work either. I also added <BrowserRouter basename={process.env.PUBLIC_URL}> to my index.js that wrapped around my <App /> JSX tag, but this did not fix it either. I've looked at several other answers for similar questions but wasn't able to fix my issue.

All help would be greatly appreciated, thank you!

You are trying to deploy the react project with the devlopment code or the source code. This code typically contains unminified and uncompressed JavaScript and CSS files. It is not optimized or suited for production use.

You need to run npm run build command. To know what the command does read

What is "npm run build" in create-react-app?

There are two options to deploy you code.

Option 1: use the build folder instead of the complete react project

Use this option if you want easy deployment and do not intend on changing your code.

  1. In your local development environment, build your React application using the command npm run build. This will create a "build" folder with the optimized production version of your application. The build folder is the folder which will contain index.html file.

  2. Commit and push the build folder to the GitHub repository.(should be the root folder)

cd build
git init
git add .
git commit -m "message"
git branch -M 
git remote add origin https://github.com/propertycashflowapp/propertycashflowapp.github.io.git
git push origin main
  1. Enable GitHub Pages for your repository by going to the repository settings and selecting the "main" branch option under "GitHub Pages" section.

Option 2: using gh-pages

Use this option if you intend to write new code regularly. (needs setup)

Please read this article first: https://www.c-sharpcorner.com/article/how-to-deploy-react-application-on-github-pages/

  1. Install the gh-pages package in your React app by running npm install gh-pages --save-dev in the command line.
  2. Add a homepage field in your package.json file. This field should contain the URL of your GitHub Pages repository, which will be in the format https://propertycashflowapp.github.io .
  3. Create a new script in your package.json file for deploying to GitHub Pages. For example, you can add the following script:
"scripts": {
  "predeploy": "npm run build",
  "deploy": "gh-pages -d build",
  ...
}

Sample package.json file can be found here

  1. Run the deploy script by executing npm run deploy in the command line. (If you change any code run npm run deploy for deploying the changes.)

  2. Wait for the deployment to complete and then visit the URL of your GitHub Pages repository to see your app live.

  3. If you encounter any issue in the process, you can check the logs for troubleshooting.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM