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.

published: reading time: 9 min read author: Geek Workbench updated: November 24, 2026
Quick Summary

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 ci with the committed lockfile.
  • Remove stale output, build once, and preview that exact dist directory.
  • Check the built index.html and 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 dist output, 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

1. What should be uploaded for a Vite static deployment?
Deploy the generated build output, usually dist. It contains the HTML and processed assets the static host serves.
2. Are VITE_ variables secret?
No. Vite exposes them to client code during the build. Use them only for configuration that visitors are allowed to see.
3. Why configure a base path?
If an app is hosted below the domain root, the base path lets Vite generate asset URLs that include that prefix.
4. What does a production build put in dist?
It contains generated static output such as HTML, JavaScript, CSS, and public files.
  • Deploy the build output rather than the source directory.
  • Preview the same output you plan to upload.
5. Why does a nested app need a configured base path?
Generated asset URLs must include the subdirectory where the app will be hosted.
  • Set the Vite base before building.
  • Inspect script and image requests in the built preview.
6. Why can a nested client route fail on refresh?
The host may try to find a physical file at the nested URL.
  • Configure a fallback to the app entry HTML.
  • Open and reload a deep link after deployment.
7. Can a runtime browser config contain a password?
No. If browser code can read the configuration, visitors can inspect it.
  • Use runtime config for public settings only.
  • Keep credentials in protected server code.
8. Is a successful build enough to confirm a deployment?
No. Build output can still use the wrong environment value or deployment path.
  • Preview the built artifact.
  • Check assets and direct routes against the target configuration.
9. Why track a build ID after release?
It helps connect browser failures to the artifact and environment that shipped.
  • Record non-secret deployment metadata.
  • Do not include environment values or private payloads in logs.
10. What should be checked before uploading a static release?
Check the output files, public configuration, base path, and route behavior.
  • Build and preview the complete dist directory.
  • Test a nested route by entering its URL directly.

Further Reading

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.

#vite #react #typescript

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.

#react #typescript #accessibility

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.

#react #typescript #state-management