A React production build is an optimized version of your React app designed for deployment. It includes minified code, optimized assets, and efficient execution to improve performance and reduce loading times. Here’s everything you need to know to create, understand, and deploy a production build.
1. How to Create a React Production Build
If you’re using Create React App (CRA):
- Navigate to your project directory in the terminal:
cd your-react-app
- Run the build command:
npm run build
Or, if using Yarn:
yarn build
- This will generate a
build/
folder in your project directory containing the optimized production build.
2. Contents of the build/
Folder
The build/
folder contains:
- HTML Files:
index.html
and other entry points. - Static Files: Minified and hashed JavaScript, CSS, and media files for caching purposes.
- Source Maps (optional): Helps in debugging minified code in production (can be disabled).
Example structure:
build/ โโโ static/ โ โโโ css/ โ โโโ js/ โ โโโ media/ โโโ index.html โโโ manifest.json
3. Key Features of a Production Build
- Minification: Removes unnecessary characters (spaces, comments) from code.
- Code Splitting: Automatically splits large bundles into smaller chunks to load only the required parts.
- Tree Shaking: Eliminates unused code.
- Environment Variables: Automatically sets
process.env.NODE_ENV
toproduction
. - Caching: File names include a hash for cache busting.
4. Serving a Production Build
You need a server to serve the production build files. Here are some popular ways to do it:
A. Using serve
(Quick Test)
- Install the
serve
package globally:npm install -g serve
- Serve the production build:
serve -s build
- Open your app in the browser at
http://localhost:3000
.
B. Deploying to Web Servers
- Node.js Server: Copy your
build/
folder to your server and serve it using Express or any Node.js server.Example:
const express = require('express'); const path = require('path'); const app = express(); app.use(express.static(path.join(__dirname, 'build'))); app.get('*', (req, res) => { res.sendFile(path.join(__dirname, 'build', 'index.html')); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
- Static Hosting Services:
- Netlify
- Vercel
- Firebase Hosting
- GitHub Pages
- AWS S3
5. Environment Variables in Production
You can configure your app’s behavior using environment variables. React uses .env
files for this:
- Set Variables: Add to your
.env
file:REACT_APP_API_URL=https://api.example.com
- Access them in your app:
console.log(process.env.REACT_APP_API_URL);
- Build the app:
npm run build
Important: Only variables prefixed with REACT_APP_
are available in React apps.
6. Optimizing the Build for Production
For advanced optimization, you can customize the production build using tools like:
- Webpack: Customize your
webpack.config.js
for bundling. - Terser: Optimize JavaScript code minification.
- PurifyCSS: Remove unused CSS.
7. Testing the Production Build
- Test Locally: Serve it with a static server (e.g.,
serve
) before deploying. - Performance Testing: Use tools like:
- Lighthouse (built into Chrome DevTools).
- WebPageTest.
- PageSpeed Insights.
8. Common Issues
- 404 on Refresh: React apps using React Router need to handle routing properly. Use a fallback to
index.html
:- For Apache: Add an
.htaccess
file. - For Nginx: Configure the server to serve
index.html
for unknown routes.
- For Apache: Add an
- Environment Variables Not Working: Ensure they’re prefixed with
REACT_APP_
and set before building.
9. Deployment Example (Netlify)
- Push your React project to GitHub.
- Go to Netlify and log in.
- Create a new site, connect your GitHub repository, and choose the branch.
- Set the build command as
npm run build
and publish directory asbuild/
. - Deploy and get your production-ready site live!
10. Best Practices
- Keep Source Maps Secure: Avoid exposing source maps in production unless needed for debugging.
- Analyze Bundle Size: Use
source-map-explorer
or Webpack Bundle Analyzer. - Use HTTPS: Secure your app using HTTPS for all production deployments.
- Optimize Images: Use tools like
ImageOptim
orwebpack-image-loader
.