4 Hours

11. Production Deployment: Builds, Environments, and CDN

Deployment

Production deployment involves building your application, configuring environments, and delivering assets via CDN. This module covers the complete production deployment pipeline.

The key insight is that production deployment requires careful planning. Environment variables must be configured. Assets must be optimized. CDN must be configured. Rollbacks must be possible. Understanding production deployment is essential for shipping applications.

This module covers build optimization, environment configuration, CDN setup, and deployment strategies.

Build Optimization for Production

Production builds should be optimized for performance. This includes: minification, compression, tree shaking, code splitting, and asset optimization. Modern bundlers handle most of this automatically in production mode.

Additional optimizations include: gzip compression, brotli compression, and CDN configuration. These optimizations can reduce load times by 50-80%.

Environment Configuration

Environment configuration is essential for production. Environment variables control API URLs, feature flags, and other configuration. Different environments (development, staging, production) have different configuration.

Environment variables should be set at build time for static sites, or at runtime for server-side rendering. Understanding environment configuration is essential for production deployment.

JavaScript
// Build script for production
// package.json
{
  "scripts": {
    "build": "webpack --mode production",
    "build:staging": "webpack --mode production --env NODE_ENV=staging",
    "build:prod": "webpack --mode production --env NODE_ENV=production"
  }
}

// webpack.config.js - Environment variables
module.exports = (env) => {
  const isProduction = env.NODE_ENV === 'production';
  const isStaging = env.NODE_ENV === 'staging';
  
  return {
    mode: isProduction ? 'production' : 'development',
    plugins: [
      new webpack.DefinePlugin({
        'process.env.API_URL': JSON.stringify(
          isProduction ? 'https://api.example.com' :
          isStaging ? 'https://staging-api.example.com' :
          'http://localhost:3000'
        ),
        'process.env.FEATURE_FLAGS': JSON.stringify({
          newUI: isProduction ? false : true,
          analytics: isProduction ? true : false,
        }),
      }),
    ],
  };
};

// CDN configuration
// vite.config.js
export default defineConfig({
  base: process.env.CDN_URL || '/',
  build: {
    assetsDir: 'assets',
    rollupOptions: {
      output: {
        assetFileNames: 'assets/[name].[hash][extname]',
        chunkFileNames: 'js/[name].[hash].js',
        entryFileNames: 'js/[name].[hash].js',
      },
    },
  },
});

// Deployment script
// deploy.sh
#!/bin/bash
npm run build
aws s3 sync dist/ s3://my-bucket/ --delete
aws cloudfront create-invalidation --distribution-id DISTRIBUTION_ID --paths "/*"

Try It Yourself: Production Deployment

Follow these steps to deploy to production.

Step 1: Configure environment variables
Set up environment-specific configuration.

Step 2: Build for production
npm run build:prod

Step 3: Deploy to CDN
Upload to S3 or CDN. Configure cache headers.

Step 4: Test the deployment
Verify the site works in production.

Step 5: Set up monitoring
Monitor performance and errors in production.

Try It Yourself: Deploy a project to production with CDN. Set up monitoring and error tracking.

Knowledge Check

Ready to test your understanding of 11. Production Deployment: Builds, Environments, and CDN?

Back