Production Builds and Deployment Basics for React
Learn what a React production build contains, how Vite handles client environment values and base paths, and how to release a static app safely.
A Vite production build turns React source into static files that a host can serve. This guide explains what belongs in the build output, why browser environment values are public, and how base paths and route fallbacks affect subpath deployments. Use the release checks to preview the exact artifact, then verify assets and direct routes on the deployed host.
Production Builds and Deployment Basics for React
Introduction
During development, a React app runs from source through a development server. A production build transforms those files into static assets for a web host. Knowing what gets built helps prevent missing scripts, images, or routes after release.
This guide follows a Vite build from source to deployable files and covers public configuration, base paths, and release checks.
Build source into deployable files
The examples here target Vite 8.3.8, the stable release checked on November 24, 2026. Pin the exact version in devDependencies and keep the lockfile in source control; the Vite release page records the version used by this guide.
To pin an existing npm project once, run this command and commit both package.json and package-lock.json:
npm install --save-dev --save-exact vite@8.3.8
In a Vite project, package scripts often expose the build and local preview commands:
npm run build
npm run preview
Check package.json for the actual script names. The build script typically runs Vite’s production build and writes dist/; preview serves those built files locally so you can catch missing assets before upload. Preview is for checking the build, not a production server.
For a clean, repeatable release, install from the committed lockfile, remove any old output, build, and preview that same directory:
npm ci
npm exec vite -- --version
rm -rf dist
npm run build -- --emptyOutDir
npm run preview -- --host 127.0.0.1
The version command should report vite/8.3.8. Leave preview running and inspect the site at http://127.0.0.1:4173, unless the project sets another port. Stop it with Ctrl+C when finished. These commands assume the standard build and preview scripts and the default dist output directory. If your project uses a different outDir, use that directory consistently instead.
Security and Compliance Notes
Client apps cannot keep secrets. In Vite, variables prefixed with VITE_ are exposed to browser code and replaced during the build. Use them only for public configuration, such as an API base URL:
VITE_API_BASE_URL=https://api.example.test
const apiBaseUrl = import.meta.env.VITE_API_BASE_URL;
That value is embedded in the client bundle. Never put passwords, private keys, or server credentials in a VITE_ variable. Anything needed by browser code cannot stay secret.
Before release, scan the built output for likely secret names or key markers. This command prints matching filenames only, not their contents:
rg -l --hidden -i 'secret|password|token|api[_-]?key|BEGIN .*PRIVATE KEY' dist
Treat every match as a lead to review, not as a finding: public names and sample text can match, and a text search cannot prove the bundle is clean. Use your organization’s secret scanner as well. Do not print matching lines into CI logs, where an accidental value could be copied into build output or logs.
Because values are baked in during the build, a staging and production deployment may need separate builds. If a deployment platform provides runtime configuration to browser code, it must still be treated as public.
When to Use / When NOT to Use
Use a static build when the React app can serve its screens and data from browser code plus static files. Choose a server-backed deployment when requests need protected server code or request-time rendering. Do not put server credentials in the client bundle to avoid that server requirement.
Set paths and handle direct routes
Apps hosted at the domain root use the default base path. If a site lives under a path such as /demo/, configure Vite’s base for that prefix before building:
// vite.config.ts
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
export default defineConfig({
plugins: [react()],
base: "/demo/",
});
Vite then rewrites built asset URLs to use that path. For a client-side router, also configure the host to return the app entry HTML for routes like /demo/settings; otherwise reloading a route may return a host-level 404 before React runs.
For /demo/settings, the host should serve /demo/index.html when the request is for a client-side route with no matching file. It should still serve real files such as /demo/assets/app-<hash>.js directly, and return a real 404 for missing assets or API paths. A catch-all rewrite that returns the app HTML for every URL can hide broken asset paths. Set Vite’s base to /demo/ and configure the router to use the matching /demo prefix.
flowchart LR
A[React source and public files] --> B[npm run build]
B --> C[dist assets]
C --> D[Preview and inspect]
D --> E[Upload to static host]
E --> F[Check root and direct routes]
Trade-Off Table
| Approach | Use it when | Trade-off |
|---|---|---|
Build-time VITE_ value |
The public API URL is known when building | Change it by rebuilding the app |
| Runtime public config | One artifact must use different public settings after deploy | Host must supply and cache the config correctly |
| Static hosting | The app can run in the browser and serve files | Router fallback and asset base path need configuration |
| Server rendering | Pages need request-time server work | Requires a server runtime and more deployment setup |
Static hosting suits small React apps. Use server rendering when request-time work justifies a server.
Production Failure Scenarios
A build can succeed with the wrong API URL. Shipping a staging VITE_API_BASE_URL sends production visitors to the wrong service. Check build mode and values, preview the artifact, and test a nested route directly.
If a nested deployment uses the root base path, generated scripts or images request the wrong URLs and the app may render blank. Set the deployment prefix in Vite, rebuild, and inspect network requests in the preview before uploading.
Before release:
- Use the pinned Vite version and
npm ciwith the committed lockfile. - Remove stale output, build once, and preview that exact
distdirectory. - Check the built
index.htmland inspect the output for possible secret leaks. - Confirm public environment values and base path match the target host.
- Test the root page, a nested route entered directly, and a missing asset on the deployed host.
- Upload the complete output as one release and keep the previous release available for rollback.
Rollback and asset caching
Builds often contain fingerprinted asset names. Those assets can use a long cache lifetime with immutable; the HTML entry file should revalidate or have a short cache lifetime so visitors discover the current asset names. Keep each release’s assets available while cached HTML may still reference them. For rollback, point the host back to the prior release, then purge or revalidate the HTML at the CDN. Do not delete the previous release’s hashed assets before that switch is complete.
Observability Checklist
- Track browser asset failures and API errors after release.
- Record build ID and deployment environment with reports.
- Never log secret values or full user payloads.
Common Pitfalls and Anti-Patterns
Uploading src instead of dist, forgetting the nested base path, and assuming client environment values are private are common mistakes. TypeScript and build success cannot prove deployment configuration is correct.
Quick Recap Checklist
- Deploy the generated
distoutput, not the source tree. - Treat every
VITE_value as public and build-specific. - Set Vite’s base path and the host’s route fallback for nested deployments.
- Preview the built files and open nested routes directly before release.
Interview Questions
dist. It contains the HTML and processed assets the static host serves.
VITE_ variables secret?- Deploy the build output rather than the source directory.
- Preview the same output you plan to upload.
- Set the Vite base before building.
- Inspect script and image requests in the built preview.
- Configure a fallback to the app entry HTML.
- Open and reload a deep link after deployment.
- Use runtime config for public settings only.
- Keep credentials in protected server code.
- Preview the built artifact.
- Check assets and direct routes against the target configuration.
- Record non-secret deployment metadata.
- Do not include environment values or private payloads in logs.
- Build and preview the complete
distdirectory. - Test a nested route by entering its URL directly.
Further Reading
- React Router fundamentals explains client routes that need a host fallback on direct visits.
- Styling React components and importing assets covers local assets that become part of the build output.
- Linting, formatting, and type checking shows how package scripts keep local and CI checks consistent.
- See Vite’s official static deployment guide and environment and mode guide.
Conclusion
Upload the complete dist/ directory built for the target host. Preview it locally, then open the root and a nested URL on the deployed site; the host fallback and asset paths need to work there too.
Category
Related Posts
Vite Development Workflow for React and TypeScript
Learn a practical Vite workflow for React and TypeScript: create a project, use hot reload, build, preview, and protect environment values.
Accessible React UI: Semantics, Labels, and Keyboard Use
Build React interfaces with semantic controls, connected labels, visible keyboard focus, and status announcements through practical TypeScript examples.
Advanced React State: When Local State Stops Fitting
Learn when React state needs a reducer and context, when Redux Toolkit helps, and why server data often belongs in a query cache instead.