Deployment Steps
Deploying a React app means turning your development code into a set of optimised static files and putting them on a server that delivers them to visitors. The development server you use while coding is not meant for real traffic. A production build is smaller, faster and free of development warnings. The steps below apply whether you host on a static host, a cloud bucket, or your own server running nginx.
Step 1: prepare environment values
Settings such as the API base URL usually differ between your laptop and the live site. Build tools read them from environment variables at build time. In a Vite project, only variables prefixed with VITE_ are exposed to your code, read through import.meta.env.VITE_API_URL. Put production values in the host's settings or in a .env.production file. Anything bundled into the frontend can be read by visitors, so never place secret keys there.
Step 2: create the production build
# install exact dependency versions from the lockfile
npm ci
# create the optimised bundle (output goes to dist/ in Vite, build/ in Create React App)
npm run build
# serve the built files locally to check them before uploading
npm run preview
The build step minifies JavaScript, removes development-only code, and adds content hashes to file names, such as index-4f2a9c.js. Those hashes let browsers cache files for a long time, because any change produces a new name.
Step 3: test the build, not just the dev server
Run the preview and click through the app. Problems that only appear in production include wrong environment values, case-sensitive import paths that worked on Windows but fail on a Linux server, and assets referenced with absolute paths when the app is hosted in a subfolder. In Vite, the base option in vite.config.js sets that subfolder.
Step 4: upload and configure the server
Copy the contents of the output folder to your host. Most static hosts let you connect a Git repository so every push runs npm run build and publishes the result automatically. Make sure HTTPS is enabled.
The refresh 404 problem
The most common deployment bug in React apps that use client-side routing: the home page works, but refreshing on /products/42 shows a 404. The server looks for a file at that path, finds none, and gives up, because only index.html exists and React Router handles the URL in the browser. The fix is a fallback rule that serves index.html for unknown paths. In nginx that is a single directive:
location / {
try_files $uri $uri/ /index.html;
}
Static hosts usually offer the same thing as a "rewrite" or "single-page app" setting.
Step 5: check after going live
Open the live site in a private window, check the browser console for errors, confirm that API calls hit the production backend, and verify that the backend allows requests from the new domain through its CORS settings.
Checklist
- Production environment values set, no secrets in the bundle.
npm run buildsucceeds and the preview works.- Fallback to
index.htmlconfigured for client-side routes. - HTTPS on, console clean, API reachable.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.