← Back to React Course | Chapter 14: Performance & Production Deployment | Lesson 10 of 13

Deploying to Netlify

Deploying to Netlify is like handing your finished, packed dish to a delivery service that gets it in front of customers anywhere in the world.

Basic Netlify Deployment Setup

Connecting a Git repository to Netlify and specifying a build command (like 'npm run build') plus a publish directory (like dist) is usually all that's needed — Netlify automatically builds and deploys your app on every push, without manual server setup.

Note: Netlify's free tier is generous and commonly used for personal projects, prototypes, and even many production sites.

Warning: This is a real hosting service requiring an actual account and deployment — it can't be demonstrated running inside this code preview.

Example: Deploying to Netlify from the CLI

bash
$ npm install -g netlify-cli
$ netlify login
$ netlify init
? Build command: npm run build
? Directory to deploy: dist

$ netlify deploy --prod

⚠️ Run this command in your terminal.

Fixing Client-Side Routing with Redirects

A React Router app only has ONE real HTML file (index.html) — all other pages are rendered client-side by JavaScript. Without telling Netlify this, directly loading or refreshing a page like /about returns a real 404, since no actual /about file exists on the server. A redirect rule fixes this by always serving index.html and letting React Router take over.

Note: This same 'catch-all redirect to index.html' pattern is needed on almost every static host for a client-side-routed single-page app.

Warning: Without this redirect rule, your app works fine when navigated to via Links, but breaks (404s) on a hard refresh or a directly-typed/shared URL to any non-home page.

Example: The _redirects File

bash
$ cat public/_redirects
/*    /index.html   200

⚠️ Run this command in your terminal.

Setting Environment Variables on Netlify

Local .env files never get uploaded to Netlify (they're typically gitignored) — production environment variables need to be configured separately in Netlify's own site settings dashboard, so the production build has the correct values baked in during the build process.

Note: Set these once in Netlify's dashboard under Site settings > Environment variables — they persist across future deployments automatically.

Warning: Forgetting to set required environment variables in Netlify's dashboard means the deployed build runs with those values undefined, potentially breaking API calls or other config-dependent features.

Example: Setting a Netlify Environment Variable

bash
$ netlify env:set VITE_API_URL https://api.example.com
Set environment variable VITE_API_URL with value https://api.example.com

⚠️ Run this command in your terminal.

Common Mistakes
  1. Setting the wrong build command or publish directory, causing Netlify to serve an empty or broken site.
  2. Forgetting to configure a redirect rule for client-side routing (React Router), causing a 404 on any direct page load besides the homepage.
  3. Not setting production environment variables in Netlify's dashboard, leaving the deployed app missing config it needs.
Chapter Summary
  • Netlify is a popular static-hosting platform, well-suited to deploying a React app's production build.
  • Deployment typically just needs a build command (npm run build) and the output folder (dist or build).
  • A redirect rule (via netlify.toml or a _redirects file) is needed for client-side routing to work on direct page loads/refreshes.
  • Environment variables must be configured in Netlify's own dashboard, separate from your local .env files.
Browser Support

No React-version restriction — a hosting/deployment workflow, not a code feature.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.