Deploying React Apps
Building for production, static hosting, and deploying to Vercel, Netlify, and Cloudflare.
What you'll learn
- Building an optimized production bundle
- Deploying static builds to hosting platforms
- Environment variables and runtime configuration
- Setting up custom domains and HTTPS
Concept
Building for Production
Before deploying, you create a production build with npm run build (or pnpm build). This command runs the compiler in production mode: it minifies the JavaScript and CSS, tree-shakes unused code, splits the bundle into chunks (if configured), and optimizes assets. The output is a set of static files (HTML, JS, CSS, images) in a dist or build directory. These files are what you deploy — there is no server-side runtime for a client-side React app (unless you are using SSR with Next.js).
Static Hosting Platforms
React apps (without SSR) are just static files, so they can be hosted on any static hosting platform. Vercel is the creator of Next.js and offers zero-config deployment for React apps with a generous free tier. Netlify provides continuous deployment, form handling, and serverless functions. Cloudflare Pages offers fast global CDN delivery with edge functions. GitHub Pages is free for public repositories. All of these connect to your Git repository and deploy automatically on push.
Environment Variables
Environment variables let you configure your app differently for development and production (API URLs, feature flags, analytics keys). In Vite, variables prefixed with VITE_ are available in the client code via import.meta.env.VITE_API_URL. In Create React App, variables prefixed with REACT_APP_ are available via process.env.REACT_APP_API_URL. In Next.js, variables prefixed with NEXT_PUBLIC_ are client-side. Set these in your hosting platform's dashboard or in .env files. Never put secrets in client-side environment variables — they are visible in the bundle.
Custom Domains and HTTPS
All major hosting platforms support custom domains with automatic HTTPS certificates (via Let's Encrypt). You add your domain in the platform dashboard, update your DNS records (usually a CNAME or A record), and the platform handles certificate provisioning and renewal. HTTPS is essential — browsers warn users about non-HTTPS sites, and many web APIs (like service workers and geolocation) require HTTPS.
Example
// package.json — build scripts for production deployment.
{
"name": "my-react-app",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"deploy": "npm run build && npm run deploy:cf"
}
}
// src/config.ts — environment-aware configuration.
const config = {
apiUrl: import.meta.env.VITE_API_URL || "http://localhost:3000/api",
isProduction: import.meta.env.PROD,
analyticsId: import.meta.env.VITE_ANALYTICS_ID,
enableFeatureX: import.meta.env.VITE_FEATURE_X === "true",
};
export default config;
// .env.production — production environment variables.
// VITE_API_URL=https://api.myapp.com/api
// VITE_ANALYTICS_ID=UA-XXXXXXXX
// VITE_FEATURE_X=true
// Deployment: connect your Git repo to Vercel/Netlify/Cloudflare Pages.
// The platform runs 'npm run build' and serves the 'dist' folder on its CDN.
The build script compiles and optimizes for production. Environment variables (prefixed with VITE_) configure the app per environment. Hosting platforms run the build and serve the static output on a global CDN.
Try it
- Environment File Generator
Generate .env files for different deployment environments.
- Code Minifier
See how production minification reduces your bundle size.
Common mistakes
The mistake
Putting API secrets in client-side environment variables
The fix
Environment variables prefixed with VITE_/REACT_APP_/NEXT_PUBLIC_ are embedded in the client bundle and visible to anyone. Keep secrets server-side. Use a backend proxy or serverless function to inject secrets at runtime.
The mistake
Deploying without testing the production build locally
The fix
Run 'npm run build && npm run preview' (or serve the dist folder) to test the production build locally before deploying. Development mode hides issues that only appear in production (code splitting, env vars, base paths).
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.