Sub topic 18
When to Use Vite and When to Use a Framework
This chapter assumes you have already scaffolded a Vite React project, run npm run dev, and run npm run build. It also assumes you know the basics of components, props and state. The goal is to help you decide whether a Vite single-page app is the right starting point for a given project, or whether a framework fits better.
What You Get with a Vite React App
A Vite React project is a client-side single-page application (SPA). The server sends one index.html file and a JavaScript bundle. The browser runs the bundle, and React builds the page on the client.
Vite gives you a dev server, hot module replacement, JSX transformation through the React plugin, and an optimized production build. It does not give you the following, because it is only a build tool:
- Routing. There is no built-in way to map URLs to components.
- Data fetching conventions. You choose how to load and cache data.
- Server rendering. The default output is static files that render in the browser.
- A backend. There are no API routes or server functions.
React itself is a library for building UI. So a Vite app is React plus a build tool, and you add every other decision yourself. That is a strength when you want control and a cost when you want defaults.
What a Framework Adds
A React framework is a full toolkit built on top of React, usually with its own build setup. The best-known examples are Next.js, React Router in framework mode (the successor to Remix), and Gatsby for static sites. Frameworks differ in detail, but they generally add the following:
- File-based or config-based routing.
- Server-side rendering (SSR), where the server produces HTML for each request.
- Static site generation (SSG), where HTML is produced at build time.
- Data loading tied to routes, so data can be fetched before a page appears.
- API endpoints or server functions in the same project.
- Code splitting per route, applied automatically.
React's own documentation recommends starting new full-stack apps with a framework. It also says that a build tool such as Vite is a reasonable choice when you want to assemble your own setup. Check the current guidance at react.dev, because this advice has changed over time. Create React App, once the standard starting point, is no longer recommended for new projects.
Why Rendering Location Matters
The biggest practical difference is where the first HTML is created. The diagram shows the two flows.
In the SPA flow, the user sees a blank page until the bundle downloads and runs. In the framework flow, the user sees content earlier, and search engines receive meaningful HTML. This has three consequences.
- Search engine optimization (SEO). Major search engines can run JavaScript, but pre-rendered HTML is more reliable and often indexed faster. Public marketing pages, blogs and product catalogs benefit from this.
- Perceived load speed. Server-rendered pages usually show content sooner on slow devices and networks.
- Link previews. Social platforms read the HTML meta tags and generally do not run your JavaScript. A SPA with one
index.htmlshows the same preview for every URL unless you add extra tooling.
Where a Vite SPA Fits Well
Choose Vite when most of these statements are true:
- The app sits behind a login, so SEO does not matter. Dashboards, admin panels and internal tools are typical.
- You already have a separate backend API, and the front end only consumes it.
- You want to learn React without framework concepts in the way.
- You are building a widget or a prototype, or you are embedding React into an existing site.
- You deploy to static hosting such as an object store with a CDN, with no server to run.
- You want freedom to pick your own router and data library.
Static hosting is a real advantage. The dist folder from npm run build can be served by any file host. There is no Node server to patch, scale or pay for.
Where a Framework Fits Better
Choose a framework when most of these are true:
- Pages are public and need to be found through search.
- First-load performance matters, for example on e-commerce or content sites.
- You want the front end and backend in one codebase.
- You expect many routes and want a convention for organizing them.
- Several developers need shared defaults instead of many local decisions.
The trade-off is more concepts. You must learn the framework's rendering modes, its data-loading rules and often a server runtime. You also depend on the framework's release schedule and its hosting expectations.
Comparison
| Concern | Vite SPA | Full-stack framework |
|---|---|---|
| Setup effort | Minimal | Moderate |
| Routing | Add a library yourself | Built in |
| First HTML | Empty shell | Rendered content |
| SEO for public pages | Weak without extra work | Strong |
| Hosting | Static files only | Static or a server runtime |
| Backend code | Separate project | Can live in the same project |
| Flexibility | High | Follows framework conventions |
| Concepts to learn | React and Vite | React, Vite-like tooling and the framework |
Common Mistakes When Choosing
- Assuming a SPA cannot be fast. A well-split SPA with a small bundle can load quickly. Slow SPAs usually ship too much JavaScript. Use dynamic
import()orReact.lazyto split rarely used screens. - Choosing a framework for a simple internal tool. You take on server concerns you do not need.
- Choosing a SPA for a public content site, then adding hacks for SEO. If you find yourself building pre-rendering by hand, a framework would have been less work.
- Forgetting that environment variables in a SPA are public. Anything with the
VITE_prefix ends up in the bundle that every visitor can read. A framework with a server can keep secrets on the server. In a SPA, secrets must live in a separate backend. - Treating the choice as permanent. Components, props and state work the same way in both setups. Moving a Vite app to a framework mostly means replacing the router and the data loading, not rewriting components.
Adding Routing to a Vite App
If you choose Vite and need several pages, you add a router. React Router is a common choice. This example installs it and defines two routes.
npm install react-router-domimport { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom'
function Home() {
return <h1>Home</h1>
}
function About() {
return <h1>About</h1>
}
createRoot(document.getElementById('root')).render(
<StrictMode>
<BrowserRouter>
<nav>
<Link to="/">Home</Link> | <Link to="/about">About</Link>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</BrowserRouter>
</StrictMode>,
)When this app is deployed as static files, a direct visit to /about asks the host for a file that does not exist. You must configure the host to return index.html for unknown paths, often called a fallback or rewrite rule. The Vite dev server and npm run preview already do this, which is why the problem often appears only after deployment.
A Short Decision Procedure
- Does the app need public pages that search engines index? If yes, lean toward a framework.
- Do you need server code in the same project? If yes, lean toward a framework.
- Is the app behind a login, or served by an existing backend? If yes, Vite is a good fit.
- Are you learning React itself? If yes, start with Vite.
- Still unsure? Start with Vite. Components carry over, so the cost of switching later is limited.
Exercise
You are building an internal inventory dashboard. Users sign in, the data comes from an existing REST API, and the app will be hosted as static files on a CDN. Would you pick a Vite SPA or a framework, and what two facts from the description support your answer?
Show answer
A Vite SPA. The app is behind a login, so search engine indexing does not matter and server-rendered HTML adds little. The data comes from an existing API and the hosting is static files, so there is no need for a server runtime or in-project backend code.
Exercise
Your Vite app uses React Router. It works at http://localhost:5173/about, but after deployment to a static host, visiting /about directly returns a 404. What is the cause and the fix?
Show answer
The static host looks for a file at /about and finds none, because the app has only one index.html and the router runs in the browser. Configure the host with a fallback or rewrite rule that serves index.html for unknown paths. The dev server and npm run preview already do this, which is why the problem only appears after deployment.