Deploying React apps
Overview
You’ve built an app; now people need to use it. A client-rendered React app (like a Vite SPA) compiles to plain static files — HTML, JavaScript, CSS and images — that any web server or CDN can host, often for free. This lesson covers the build, configuration, hosting and automation.
Building for production
npm run buildVite (using Rolldown) bundles and optimises your app into dist/:
dist/
├─ index.html
└─ assets/
├─ index-3f9c2e1a.js main bundle
├─ CourseEditor-91b7d4.js lazy-loaded chunk
├─ index-a8c2f0.css
└─ hero-5d2e11.png- JavaScript is minified and tree-shaken (unused exports removed).
- Development-only code — warnings, StrictMode’s double rendering, extra checks — is stripped, so production React is much faster.
- File names include a content hash, so a changed file gets a new name.
Always check the build locally before deploying:
npm run preview # serves dist/ at http://localhost:4173Environment variables
Configuration that differs between environments — API URLs, analytics ids, feature flags — goes in .env files:
# .env loaded in all environments
# .env.development npm run dev
# .env.production npm run build
VITE_API_URL=https://api.academy.example
VITE_SENTRY_DSN=https://…const res = await fetch(`${import.meta.env.VITE_API_URL}/courses`);Two crucial facts:
- Only variables starting with
VITE_are exposed to your client code. This stops server secrets in the same.envfrom leaking by accident. - Values are baked in at build time. They’re written into the JavaScript files, which anyone can read in DevTools. So never put secrets — API keys with write access, database passwords, private tokens — in a client app. Anything secret belongs on a server (your API, or a Server Function).
Add .env.local and .env.*.local to .gitignore for personal overrides.
Hosting a single-page app
Any static host works. Popular choices with generous free tiers:
| Host | Deploy by | SPA fallback |
|---|---|---|
| Netlify | connect a Git repo, or netlify deploy |
public/_redirects: /* /index.html 200 |
| Vercel | connect a Git repo, or vercel CLI |
a rewrite of /(.*) to /index.html in vercel.json |
| Cloudflare Workers / Pages | connect a repo, or wrangler deploy |
"not_found_handling": "single-page-application" in the assets config |
| GitHub Pages | a GitHub Action | copy index.html to 404.html (workaround) |
| AWS S3 + CloudFront | upload dist/ |
error responses → /index.html with status 200 |
For most hosts, connecting the repository is enough: they detect Vite, run npm run build, publish dist/ and give you a URL — and redeploy automatically on every push.
The SPA fallback
This is the most common deployment bug. Your app has routes like /courses/react, but there’s no file called courses/react on the server. Clicking links works (the router handles them in the browser), but refreshing or opening a shared link asks the server for that path and gets a 404.
The fix is a rewrite: serve index.html for any path that doesn’t match a real file. The app loads, React Router reads the URL and shows the right page. Each host configures it differently (see the table); for example, with Cloudflare:
// wrangler.jsonc
{
"name": "course-catalog",
"compatibility_date": "2026-11-01",
"assets": {
"directory": "./dist",
"not_found_handling": "single-page-application"
}
}Make your own 404 page a catch-all route in the router (path: '*'), since the server now always returns index.html.
Hosting under a sub-path
If the app lives at https://example.com/academy/ rather than the domain root, tell Vite and the router:
// vite.config.js
export default defineConfig({ base: '/academy/', plugins: [react()] });createBrowserRouter(routes, { basename: '/academy' });Caching
Hashed file names allow an ideal caching strategy:
/assets/*— cache “forever”:Cache-Control: public, max-age=31536000, immutable. A changed file has a new name, so stale caches are never a problem.index.html— don’t cache (or always revalidate):Cache-Control: no-cache. It’s tiny, and it points to the current hashed assets, so a new deploy takes effect on the next page load.
Most static hosts apply sensible defaults like these automatically.
Continuous integration
Automate the checks so broken code doesn’t reach users:
# .github/workflows/ci.yml
name: CI
on:
push:
branches: [main]
pull_request:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- uses: actions/setup-node@v5
with:
node-version: 24
cache: npm
- run: npm ci
- run: npm run lint
- run: npx vitest run
- run: npm run buildProtect main so pull requests can only merge when CI passes. Most hosts also create a preview deployment for every pull request — a unique URL with that branch’s version, perfect for reviewing changes before merging.
Before you launch
- ✅
npm run buildsucceeds with no warnings;npm run previewworks, including refreshing on deep routes. - ✅ Environment variables are set on the host for production; no secrets in
VITE_variables. - ✅ A top-level error boundary and error reporting (e.g. Sentry) are in place.
- ✅ Page titles and meta descriptions are set (React 19 lets you render
<title>and<meta>in any component and hoists them into<head>). - ✅ Lighthouse shows good performance and accessibility scores on a mid-range mobile profile.
- ✅ HTTPS and a custom domain are configured.
- ✅ Analytics or real-user monitoring are in place, so you know how the app performs for real people.
Beyond static hosting
If you need server rendering for SEO, Server Components or Server Functions, deploy a framework instead — Next.js or React Router’s framework mode run on Node servers, serverless platforms or the edge, and hosts like Vercel, Netlify and Cloudflare support them directly. The Node.js course covers running your own servers and APIs.
Where to go next
You’ve gone from your first component to a tested, accessible, optimised app with routing, data fetching and a deployment pipeline. Natural next steps:
- TypeScript everywhere — convert your project and lean on the compiler.
- A framework — build something with Next.js or React Router framework mode to use Server Components and streaming.
- React Native — reuse your React skills for iOS and Android apps.
- Design systems — build a small component library with variants, accessibility and tests.
Ship early. Deploy the first version of your project today, even if it’s tiny. Every later change is then just a
git push— and real users are the best teachers there are.
Ship the course catalogue
Prepare your Vite app for production: read the API base URL from VITE_API_URL (with .env.development and .env.production files), build it, and preview the build locally. Then write a GitHub Actions workflow that, on every push to main, installs dependencies, runs the linter and tests, and builds the app — and deploy it to a static host of your choice with an SPA fallback so /courses/react works on refresh.
# .env.development
# .env.production
# .github/workflows/ci.yml# .env.development
VITE_API_URL=http://localhost:3000/api
# .env.production
VITE_API_URL=https://api.academy.example/api
# src/api.js
# export const API_URL = import.meta.env.VITE_API_URL;
npm run build # creates dist/
npm run preview # serves dist/ at http://localhost:4173
# .github/workflows/ci.yml
# name: CI
# on:
# push:
# branches: [main]
# pull_request:
# jobs:
# build:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v5
# - uses: actions/setup-node@v5
# with:
# node-version: 24
# cache: npm
# - run: npm ci
# - run: npm run lint
# - run: npx vitest run
# - run: npm run build
# SPA fallback, e.g. Netlify: public/_redirects
# /* /index.html 200Summary
npm run buildproduces an optimiseddist/folder of static files with content-hashed names.- Vite exposes only variables prefixed
VITE_to client code, and bakes them in at build time — never put secrets there. - Single-page apps need an SPA fallback so every path serves
index.htmland the router can take over. - Cache hashed assets for a long time and
index.htmlnot at all, so new deploys take effect immediately. - Automate lint, tests and build in CI, and use preview deployments to review changes before they go live.