使用Travis CI部署CRA到GitHub Pages遇两类问题求助
Fixing GitHub Pages Deployment Issues with create-react-app & Travis CI
Hey there, let's break down and solve the two issues you're hitting when deploying your create-react-app to GitHub Pages (with a custom domain) via Travis CI:
Issue 1: Page only displays README.md
This usually happens because GitHub Pages is prioritizing the README file over your app's built assets. Here's how to fix it:
- First, head to your GitHub repo's Settings > Pages section. Double-check that the "Source" is set to the
gh-pagesbranch (this is where Travis should be pushing your build files) and that the directory is set to /root (not /docs or another subfolder). - Verify your Travis CI deployment script is correctly pushing the
buildfolder to thegh-pagesbranch. Your script should include a command likegh-pages -d build(make sure you've installed thegh-pagespackage as a dev dependency in your project). - If you're using a custom domain, ensure you've added a
CNAMEfile in yourpublicfolder (create-react-app will copy this to the build folder during compilation) with your domain name—this ensures GitHub Pages recognizes your custom domain correctly.
Issue 2: Blank page due to react-router misconfiguration
The blank page happens because react-router doesn't account for the GitHub Pages project prefix (/ollidia-front-end) in your URLs. The solution you referenced addresses this perfectly—here's the core steps to implement it:
- Set the homepage in package.json: Add this line to your
package.jsonfile:
This tells create-react-app to prefix all asset paths with your project's GitHub Pages URL, so resources load correctly."homepage": "https://jaanhio.github.io/ollidia-front-end" - Configure react-router for the project prefix:
- If you're using
BrowserRouter, update it to include thebasenameprop, which adds your project path to all routes:
With this setup, you can keep your route paths as normal (e.g.,import { BrowserRouter } from 'react-router-dom'; <BrowserRouter basename="/ollidia-front-end"> {/* Your routes go here */} </BrowserRouter><Route path="/about" component={About} />—the basename will automatically prepend/ollidia-front-endto the URL). - Alternatively, if you want to avoid dealing with server-side routing limitations, switch to
HashRouterinstead. It uses URL hashes (likehttps://jaanhio.github.io/ollidia-front-end/#/about) which GitHub Pages handles seamlessly without extra configuration.
- If you're using
- Handle 404s (if using BrowserRouter): GitHub Pages doesn't natively support client-side routing, so if someone refreshes a page like
/ollidia-front-end/about, it'll throw a 404. To fix this, create a404.htmlfile in yourpublicfolder that's identical to yourindex.html—GitHub Pages will redirect all 404s to this file, and react-router will take over from there.
Give these steps a try—they should resolve both your issues. Let me know if you run into any hiccups along the way!
内容的提问来源于stack exchange,提问作者jaanhio
相关产品推荐
相关产品推荐

