Production Deployment Strategies
Learn various deployment strategies including static hosting, containerization, and cloud deployment options. This is a foundational concept in component-based UI development that professional developers rely on daily. The explanations below are written to be beginner-friendly while covering the depth and nuance that comes from real-world React experience. Take your time with each section and practice the examples
Static Hosting
Deploy React applications to static hosting services like Netlify, Vercel, and AWS S3 with proper configuration.. This is an essential concept that every React developer must understand thoroughly. In professional development environments, getting this right can mean the difference between code that works reliably and code that breaks in production. The following sections break this down into clear, digestible pieces with practical examples you can try immediately
Netlify Deployment
# netlify.toml
[build]
publish = "build"
command = "npm run build"
[build.environment]
NODE_VERSION = "18"
[[redirects]]
from = "/*"
to = "/index.html"
status = 200
[headers]
for = "/static/*"
[headers.values]
Cache-Control = "public, max-age=31536000, immutable"Docker Deployment
# Dockerfile for React app
FROM node:18-alpine as build
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=build /app/build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]CI/CD Pipeline
# .github/workflows/deploy.yml
name: Deploy to Production
on:
push:
branches: [main]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test -- --coverage --watchAll=false
- name: Build application
run: npm run build
env:
REACT_APP_API_URL: ${{ secrets.REACT_APP_API_URL }}
- name: Deploy to AWS S3
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Upload to S3
run: aws s3 sync build/ s3://my-react-app --deleteBlue-Green Deployment
// Blue-green deployment strategy
const deploymentConfig = {
blue: {
version: '1.0.0',
url: 'https://blue.myapp.com',
traffic: 0
},
green: {
version: '1.1.0',
url: 'https://green.myapp.com',
traffic: 100
}
};
// Gradual traffic shifting
const shiftTraffic = (from, to, percentage) => {
// Implementation for traffic shifting
console.log(`Shifting ${percentage}% traffic from ${from} to ${to}`);
};Mini-Project: Complete Deployment Pipeline
// Complete deployment configuration
# docker-compose.yml
version: '3.8'
services:
react-app:
build: .
ports:
- "3000:80"
environment:
- NODE_ENV=production
- REACT_APP_API_URL=http://api:3001
depends_on:
- nginx
nginx:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./build:/usr/share/nginx/html
# nginx.conf
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
gzip on;
gzip_types text/plain text/css application/json application/javascript;
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
location /static/ {
expires 1y;
add_header Cache-Control "public, immutable";
}
}
}
# package.json scripts
{
"scripts": {
"build": "react-scripts build",
"build:docker": "docker build -t react-app .",
"deploy:staging": "npm run build && aws s3 sync build/ s3://staging-bucket",
"deploy:production": "npm run build && aws s3 sync build/ s3://production-bucket"
}
}