Overview
Deploying a React application involves building an optimized production bundle and hosting it on a static file server or CDN.This guide covers deployment using Firebase Hosting, but the build concepts apply to any hosting platform.
Production Build
Create an optimized production build of your React application:The build command uses Create React App’s build script which:
- Minifies JavaScript and CSS
- Optimizes images and assets
- Creates a production-ready bundle in the
builddirectory - Generates source maps for debugging
Build Configuration
Yourpackage.json contains the build scripts:
Firebase Hosting Setup
Install Firebase CLI
Install the Firebase command-line tools globally:Login to Firebase
Authenticate with your Google account:Initialize Firebase Project
Initialize Firebase in your project directory:Firebase Configuration Files
firebase.json
Configure Firebase Hosting behavior:Configuration Options
Configuration Options
public: Directory to deploy (the build output folder)ignore: Files to exclude from deploymentrewrites: Redirect all requests to index.html for client-side routing
Why Rewrites?
Why Rewrites?
The rewrite rule ensures all routes are handled by your React app, not the server. This is essential for React Router to work correctly in production.
.firebaserc
Store your Firebase project configuration:Deployment Process
Build and Deploy
Environment Variables
Manage environment-specific configuration:Create React App requires environment variables to be prefixed with
REACT_APP_ to be accessible in your code.Using Environment Variables
Custom Domain Setup
Connect a custom domain to your Firebase hosting:Other Hosting Options
Vercel
Netlify
AWS S3 + CloudFront
GitHub Pages
Build Optimization
Code Splitting
Code Splitting
React automatically splits your code using dynamic imports:
Asset Optimization
Asset Optimization
Optimize images before building:
- Use WebP format for images
- Compress images with tools like ImageOptim
- Lazy load images below the fold
Bundle Analysis
Bundle Analysis
Analyze your bundle size:
Performance Best Practices
CI/CD Integration
Automate deployments with GitHub Actions:Monitoring and Analytics
Performance Monitoring
Add Firebase Performance Monitoring:
Error Tracking
Integrate error tracking:
Troubleshooting
404 Errors on Refresh
404 Errors on Refresh
Ensure your hosting platform redirects all routes to
index.html. Check the rewrites configuration in firebase.json.Environment Variables Not Working
Environment Variables Not Working
- Verify variables are prefixed with
REACT_APP_ - Rebuild after changing environment variables
- Check that
.envfiles are in the project root
Large Bundle Size
Large Bundle Size
- Analyze bundle with webpack-bundle-analyzer
- Implement code splitting
- Remove unused dependencies
- Use tree-shaking effectively
Related Topics
Routing
Ensure client-side routing works in production
Authentication
Secure your deployed application