Skip to content

Deploying React Apps

构建 for production, 静态 hosting, and deploying 到 Vercel, Netlify, and Cloudflare.

你将学到

  • 构建 optimized production bundle
  • Deploying 静态构建到 hosting platforms
  • 环境变量 and 运行时配置
  • 设置自定义领域 and HTTPS

概念讲解

Building for Production

Before deploying, you create a production build with npm 运行 构建 (or pnpm 构建). 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 构建 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 导入.meta.环境.VITE_API_URL. In Create React App, variables prefixed with REACT_APP_ are available via 处理.环境.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 .环境 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.

示例代码

              // 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.
            

构建 script 编译 and optimizes for production. 环境变量 (prefixed 使用 VITE_) 配置 app 每环境. Hosting platforms 运行构建 and serve 静态输出 on 全局 CDN.

动手试一试

常见错误

错误写法

Putting API secrets in 客户端-边环境变量

正确做法

环境变量 prefixed 使用 VITE_/REACT_APP_/NEXT_PUBLIC_ 是 embedded in 客户端 bundle and 可见到 anyone. Keep secrets 服务器-边. Use backend proxy or serverless 函数到 inject secrets at 运行时.

错误写法

Deploying 不使用测试 production 构建 locally

正确做法

运行 'npm 运行构建 && npm 运行 preview' (or serve dist folder) 到测试 production 构建 locally 之前 deploying. Development 模式 hides issues 那个仅 appear in production (code 拆分, 环境 vars, 基础 paths).

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误