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.

published: reading time: 7 min read author: Geek Workbench
Quick Summary

Vite splits a React project’s workflow into local development, a production build, and a local preview of generated files. Start with the `react-ts` template, then verify `dist/`, asset paths, and client-side route handling before deployment. This guide also explains why `VITE_` environment values are public and which checks help catch release problems early.

Vite Development Workflow for React and TypeScript

Introduction

Vite gives React a fast local server and a production build with little setup. The key is knowing which command belongs to each stage: dev for editing, build for deployable files, and preview to inspect them locally.

This walkthrough follows that workflow from creating a React and TypeScript project through previewing and deployment, including the path and environment settings that can break a release.

To try the workflow locally, scaffold a React and TypeScript project and start its dev server:

npm create vite@latest my-app -- --template react-ts
cd my-app
npm install
npm run dev

Build and preview the production version

When the feature is ready to check, run:

npm run build
npm run preview

Vite writes static output to dist/; preview serves it, typically at http://localhost:4173. This can expose asset path problems hidden during development. Deploy dist/ to your static host.

If the site lives below a domain path, set Vite’s base option to that path. Apps with client-side routes also need the host to serve the entry page for nested URLs.

graph LR
    A[Create project] --> B[Install dependencies]
    B --> C[Run dev server]
    C --> D[Edit and reload]
    D --> E[Build dist]
    E --> F[Preview production output]
    F --> G[Deploy dist]

Trade-Off Table

Choice Useful when Cost or limitation
Vite dev server You want quick startup and responsive edits Development behavior is not identical to the bundled site
vite build You need optimized static assets for deployment Build-time configuration and paths must be correct
vite preview You want to inspect the built output locally It does not reproduce every production host setting

Production Failure Scenarios

  • Assets return 404 after deployment: the configured base may not match the subpath where the app is hosted. Set the base to the deploy path, rebuild, and inspect the network panel for the generated asset URLs.
  • A nested route fails on refresh: configure the host to serve the app entry page for client-side routes. Check the deployed route and response before changing component code.

Common Pitfalls / Anti-Patterns

  • Treating a successful HMR session as proof that the production bundle works. Run build and inspect it with preview before release.
  • Assuming preview reproduces the CDN or every host rewrite. Confirm deployment-specific configuration separately.
  • Changing code before checking build output, the console, and network requests; each points to a different failure source.

Observability Checklist

  • Keep build and type-check output in CI logs; stop deployment when either fails.
  • For a blank page, inspect browser errors, failed asset requests, and the generated dist/ paths.
  • Use browser error reporting for runtime problems and avoid publishing source maps unless intended.

Security and Compliance Notes

Vite replaces client environment variables during the build. Values prefixed with VITE_, such as VITE_API_ORIGIN, are readable in the browser and must be treated as public. Keep passwords and private keys on a server. Ignore local .env files in version control; use safe placeholders in .env.example.

When to Use / When NOT to Use

Use Vite for a client-rendered React app, prototype, or static frontend when the team wants to assemble its own build and routing setup. Choose a server-rendered framework when pages need server-generated output, integrated data loading, or a server runtime. With Vite, your team assembles more of that architecture.

Quick Recap Checklist

  • Create with the react-ts template and install dependencies.
  • Use npm run dev while editing.
  • Use npm run build, then npm run preview, before deployment.
  • Set the correct base path and route fallback for the host.
  • Keep secrets out of VITE_ variables and review build logs.

Interview Questions

1. What is the difference between npm run dev and npm run preview?
dev serves source files for active development and supports hot updates. preview serves the already generated dist/ output so you can inspect the production build locally.
2. Why should a developer avoid putting a private key in a VITE_ variable?
Vite embeds client-exposed variables into the browser bundle. Anyone who can load the app can inspect them, so secrets belong in a protected server environment.
3. What should you check when a deployed app loads a blank page?
Inspect the browser console and network panel, confirm the configured base path matches the deployment URL, and verify that the host serves the expected files from dist/.
4. What does hot module replacement do?
It updates changed modules in the development server without a full page reload when possible.
  • It speeds up the edit-and-check loop.
  • It does not replace a production build check.
5. Where does Vite write production output by default?
The build writes static files to dist/ by default.
  • Deploy the generated files to the static host.
  • Inspect the output when a deployed asset path fails.
6. Why set Vite's base option?
It tells Vite the public path where built assets will be served.
  • Set it when deploying below a domain subpath.
  • Rebuild after changing it and check generated URLs.
7. Does preview reproduce the production hosting environment?
No. It serves the generated output locally, but does not reproduce every CDN or host rewrite.
  • Use it to catch build and asset path issues.
  • Verify host-specific routing and caching after deployment.
8. Which environment variables are safe in browser code?
Only values meant to be public should be exposed through client-prefixed variables.
  • Assume users can inspect the compiled bundle.
  • Keep credentials in a server-side environment.
9. Why configure a route fallback for a single-page app?
A direct visit to a nested client route otherwise asks the host for a file at that path.
  • Configure the host to return the app entry page for client routes.
  • Check the response for a nested route after deployment.
10. What should a release check include for a Vite app?
Build the project and inspect the output before deploying it.
  • Run TypeScript and build checks.
  • Preview the bundle and verify its asset and route paths.

Further Reading

Conclusion

The loop is create, edit, build, and preview. Keeping those stages distinct makes path mistakes and build-only issues easier to spot before a React and TypeScript app ships.

Category

Related Posts

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

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.

#react #typescript #vite

Styling React Components and Importing Assets

Compare plain CSS with CSS modules, then learn how Vite handles imported files and public assets in React projects, with practical code examples.

#react #css #vite