You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Parcel打包工具替代react-scripts部署至GitHub Pages后页面空白问题求助

Fixing Blank GitHub Pages for Parcel-Built React Apps

Hey there, let's troubleshoot why your Parcel-built React app is showing up blank on GitHub Pages. I’ve run into this exact issue before, so here are the most likely fixes to get your app working:

1. Set the Correct Public Path & Homepage

GitHub Pages hosts your app under a subpath like https://<your-username>.github.io/<your-repo-name>/, but Parcel defaults to building for the root directory. This mismatch causes the app to fail loading assets.

Fix Steps:

  • Add a homepage field to your package.json pointing to your GitHub Pages URL:
    "homepage": "https://<your-github-username>.github.io/<your-repo-name>"
    
  • Update your build-prod script to include the --public-url flag matching your repo name:
    "build-prod": "parcel build src/index.html --public-url /<your-repo-name>/"
    

This tells Parcel to prefix all asset paths with your repo name, so GitHub Pages can find them correctly.

2. Fix React Router Configuration (If You’re Using It)

Since you’re using react-router-dom, the default BrowserRouter won’t work with GitHub Pages—GitHub’s server doesn’t handle client-side routing, leading to blank pages or 404s.

Fix Steps:

Switch from BrowserRouter to HashRouter in your root component:

// Before
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';

// After
import { HashRouter as Router, Routes, Route } from 'react-router-dom';

function App() {
  return (
    <Router>
      <Routes>
        {/* Your existing routes go here */}
      </Routes>
    </Router>
  );
}

HashRouter uses URL hashes (like #/about) which GitHub Pages can handle without server-side configuration.

3. Verify Your dist Folder Contents

Run npm run build-prod and check the dist folder to make sure all assets are present:

  • Confirm index.html exists and has correct src attributes for your JS/CSS files (no broken paths)
  • Ensure static assets (images, fonts) are included and their paths match what’s in your code

If you see missing files or incorrect paths, try deleting the dist folder first, then re-run build-prod to force a clean build.

4. Double-Check GitHub Pages Settings

Head to your repo’s Settings > Pages and confirm:

  • The deployment source is set to the gh-pages branch (this is where gh-pages pushes your dist content)
  • The root directory (/root) is selected (not /docs or another subfolder)

Final Test

After making these changes, run:

npm run deploy

Wait a few minutes for GitHub Pages to update, then visit your app URL. It should load correctly now!

内容的提问来源于stack exchange,提问作者Tobi_009

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 20:02:35