Getting Started with Deployment
Deploying a React app means taking the optimised files produced by your production build and putting them on a server that anyone can reach through a URL. A React single page app built with Vite or Create React App compiles down to static files: one HTML page, JavaScript bundles, CSS and assets. Because nothing needs to run on the server, you can host it on any static file host or CDN.
What actually gets uploaded
In the previous lesson you ran the build. Vite writes its output to a dist folder, while Create React App writes to build. That folder is the only thing the host needs. Your src folder, node_modules and config files stay on your machine or in your repository.
Inside the output you will find index.html plus an assets folder (or static in Create React App) containing files with hashes in their names, like index-3f9a1c.js. The hash changes whenever the content changes, which lets hosts cache these files for a long time while still delivering updates.
Preview the production build locally first
The development server hides problems that only show up in the real build, such as wrong asset paths or missing environment variables. Always test the built output before uploading it.
# Vite project
npm run build
npm run preview # serves dist/ at http://localhost:4173
# Create React App project
npm run build
npx serve -s build # -s sends unknown paths to index.html
Click through every page, refresh a few of them, and open the browser console to check for errors before going live.
Choosing where to host
Static hosting services generally work the same way. You either drag and drop the build folder, use a command-line tool, or connect a Git repository so every push to the main branch triggers a fresh build and deploy. The last option is the most convenient, because the host runs npm run build for you.
When connecting a repository, you usually set three things:
- Build command: typically
npm run build. - Output directory:
distorbuild. - Environment variables: values like your API base URL.
Many hosts, such as Netlify, Vercel, Cloudflare Pages, GitHub Pages and Firebase Hosting, offer free tiers for static sites. Pick one and learn its dashboard; the concepts carry over to the rest.
The environment variable trap
Environment variables in a React build are not secret. Vite exposes only variables starting with VITE_, Create React App only those starting with REACT_APP_, and both copy their values into the JavaScript bundle at build time. Anyone can read them by opening your site's source. Never put database passwords, private API keys or payment secrets there; keep those on a backend server.
A second consequence is that changing a variable on the hosting dashboard does nothing until you rebuild. The old value is baked into the files already deployed.
Deploying under a subfolder
If your app will live at a path such as example.com/shop/ instead of the domain root, asset URLs must include that path. In Vite set base: '/shop/' in vite.config.js; in Create React App set "homepage" in package.json. Forgetting this gives a blank page with 404 errors for every script.
Before you move on
- Upload only the build output, never the source folder.
- Preview the production build locally and refresh pages to check routes.
- Keep secrets out of frontend environment variables and rebuild after changing them.
Refreshing a page like /products/3 on a live host may still return a 404, because the server looks for a file at that path. Fixing that is the subject of the next lesson on routes.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.