πŸš€ React Deployment Demystified: Solving Common Issues Across Platforms

CodeReacherCodeReacher
3 min read

You’ve built a shiny React app… now comes the real challenge β€” deployment.

Whether it’s deploying to Netlify, Vercel, GitHub Pages, Firebase Hosting, or custom servers, developers often face frustrating issues like:

  • ⚠️ White screens after deployment

  • πŸ”„ Routes not working (404 errors)

  • πŸ“¦ Incorrect asset paths or broken images

  • πŸ” Environment variable misconfigurations

  • πŸ”„ CI/CD pipeline problems

Let’s walk through why these issues happen, how to solve them, and which platform fits your needs best.

🌐 1. React Deployment Basics: Understanding npm run build

Before deploying, always generate a production build:

npm run build

This creates an optimized build/ folder with minified JS, preprocessed HTML, and static assets β€” ready for the web!

🧭 2. Platform-Specific Challenges & Solutions

πŸ”΅ Netlify

Issue: App works locally but gives a blank screen or broken links after deploy.

βœ… Solution:

  • Add a _redirects file in public/ for SPA routing:
/*    /index.html   200

Set build command: npm run build

Set publish directory: build/

πŸ“Œ Bonus: Use netlify.toml for advanced configs.

⚫ Vercel

Issue: Dynamic routes (React Router) result in 404s.

βœ… Solution:

  • Use vercel.json to define rewrites for SPA:
{
  "rewrites": [
    { "source": "/(.*)", "destination": "/" }
  ]
}
  • Vercel automatically detects React builds β€” minimal setup needed!

πŸ”₯ Firebase Hosting

Issue: Refreshing a routed page results in 404.

βœ… Solution:

  • Add firebase.json with rewrite rule:
{
  "hosting": {
    "public": "build",
    "rewrites": [
      { "source": "**", "destination": "/index.html" }
    ]
  }
}
  • Use Firebase CLI:
firebase init
firebase deploy

🟣 GitHub Pages

Issue: Routes break, and page reloads show 404.

βœ… Solution:

  • Set "homepage" field in package.json:
"homepage": "https://yourusername.github.io/repo-name"
  • Use gh-pages package:
npm install --save gh-pages

Add scripts:

"scripts": {
  "predeploy": "npm run build",
  "deploy": "gh-pages -d build"
}

🧱 Custom Servers (Node, Nginx)

Issue: Routing and asset handling issues.

βœ… Solution:

  • On Nginx, add this config for SPA routing:
location / {
  try_files $uri /index.html;
}
  • On Node/Express, serve index.html for all unmatched routes:
app.use(express.static(path.join(__dirname, 'build')));
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

πŸ” 3. Environment Variables in Production

Issue: Variables like API keys don’t work after deploy.

βœ… Solution:

  • React only exposes env vars prefixed with REACT_APP_

  • Define in .env.production and rebuild:

REACT_APP_API_URL=https://api.example.com

πŸ”„ 4. CI/CD Pipeline Tips

For teams using GitHub Actions, GitLab CI, or other pipelines:

βœ… Checklist:

  • Run npm install and npm run build

  • Cache dependencies for faster builds

  • Set correct environment and secrets

  • Use artifacts to upload the build/ folder

πŸš€ Bonus: Vite Deployment

Using Vite instead of Create React App?

Set base in vite.config.js:

export default defineConfig({
  base: '/your-repo-name/',
});

Then build and deploy as usual.

πŸ“š References


🧠 Final Thoughts

Deployment is more than just uploading files β€” each platform has quirks that can break your app if you're not careful. But once you understand the rules of each system, deployment becomes a breeze.

πŸ‘‰ Follow Code Reacher for more in-depth React engineering guides, tool breakdowns, and real-world solutions.

0
Subscribe to my newsletter

Read articles from CodeReacher directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

CodeReacher
CodeReacher

Hi, I'm CodeReacher, a budding technical content writer with a passion for simplifying complex tech concepts. I come from a Computer Science background and love creating clear, engaging content around topics like Web development, DevOps, or cloud. I recently started Code Reacher, a blog dedicated to sharing practical, reference-rich tech articles for developers and learners. I'm eager to grow in the field and contribute meaningful content to the tech community.