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 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
basemay 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
buildand inspect it withpreviewbefore release. - Assuming
previewreproduces 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-tstemplate and install dependencies. - Use
npm run devwhile editing. - Use
npm run build, thennpm 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
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.
VITE_ variable?base path matches the deployment URL, and verify that the host serves the expected files from dist/.
- It speeds up the edit-and-check loop.
- It does not replace a production build check.
dist/ by default.
- Deploy the generated files to the static host.
- Inspect the output when a deployed asset path fails.
- Set it when deploying below a domain subpath.
- Rebuild after changing it and check generated URLs.
- Use it to catch build and asset path issues.
- Verify host-specific routing and caching after deployment.
- Assume users can inspect the compiled bundle.
- Keep credentials in a server-side environment.
- Configure the host to return the app entry page for client routes.
- Check the response for a nested route after deployment.
- Run TypeScript and build checks.
- Preview the bundle and verify its asset and route paths.
Further Reading
- Review npm and package scripts to understand the commands the Vite template adds to
package.json. - Modern JavaScript Modules explains how imports connect files in a React app.
- After the toolchain setup, read Typed Function Components for a practical React and TypeScript example.
- For scaffolding options and supported templates, see the official Vite guide.
- For base paths and hosting, use Vite’s official static deployment guide.
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.
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.
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.