Vite React Best Practices
A senior-level guide for building production-ready React Single Page Applications (SPAs) with Vite.
When to Apply
Reference these guidelines when:
- Setting up a new Vite + React project
- Configuring build pipelines and CI/CD for SPAs
- Troubleshooting production build or caching issues
- Refactoring React components for performance
Priority Table
| Impact | Rules | | --- | --- | | CRITICAL | Static Rewrites, Caching Strategy | | HIGH | Build Validation, Environment Variables, Route Splitting, Server State | | MEDIUM | Memoization, Image Optimization, Colocation, Anti-Patterns: Import from Dist, Troubleshooting |
Rule Categories
1. Vite SPA Deployment (CRITICAL)
- Static Rewrites - Mandatory for client-side routing.
- Caching Strategy - Immutable assets, no-cache index.html.
- Build Validation - Preview before push.
- Environment Variables -
VITE_prefix and security.
2. React Core Performance
- Route Splitting - Lazy load pages with React Router 8
route.lazy. - Server State - TanStack Query v5 for cached server data.
- Memoization - When to use useMemo/useCallback, and what React Compiler replaces.
- Image Optimization - LCP and CLS prevention.
3. Architecture & Cleanup
- Colocation - Feature-based structure.
- Anti-Patterns: Import from Dist - Avoid bundling twice.
- Troubleshooting - Common Vite 8 fixes.
How to Use
- Pick the rule that matches the task from the categories above, or from the priority table if you need to know what must not be skipped.
- Open the rule file in
rules/. Each file is self-contained: frontmatter withimpactandtags, the rationale, an incorrect example, a correct example, and a References section. - Load only the rules relevant to the task. CRITICAL rules apply to every Vite SPA; HIGH rules apply to most production apps; MEDIUM rules apply when the area is being changed.
- When a rule references another rule, follow the relative link inside
rules/.
| Stack | Version |
| --- | --- |
| React | 19.3.1 |
| Vite | 8.3.5 (Rolldown + Oxc) |
| React Router | 8.5.1 |
| TanStack Query | 5.105.1 |
| React Compiler | babel-plugin-react-compiler 1.x |
| Node (Vite 8) | ^20.19.0 || >=22.12.0 |
Full Compiled Document
For the complete guide with all rules expanded: AGENTS.md (also available as CLAUDE.md).

Scan to join WeChat group