Skip to main content
This gets a page, a loader, and an API route running.

1. Install

Or add the same packages to an existing Vite app: better-router@alpha, hono, react, react-dom, and vite.

2. Enable the plugin

Replace the default Vite plugin list with betterRouter(). It already includes @vitejs/plugin-react.
vite.config.ts
package.json
src/vite-env.d.ts
tsconfig.json
Delete Vite’s index.html and src/main.tsx. Pages live in src/routes, not a client entry. Recent create-vite templates use tsconfig.app.json project references. Replacing tsconfig.json as shown is enough — you can remove the unused tsconfig.app.json / tsconfig.node.json files if you like. Create src/vite-env.d.ts if the template did not include it. Full compiler options are in TypeScript.

3. Layout and page

Layouts wrap the page tree. They do not render <html> or <body> — that is _document.tsx, or the default document the plugin already provides.
src/routes/_layout.tsx
src/routes/index.tsx
src/routes/index.server.tsx
Loader data is passed as page props and available from useLoaderData(). Use whichever you prefer. The .server.tsx file never ships to the browser.
You do not need src/routes/_document.tsx for this tutorial. The plugin ships a default HTML shell (#app, loader payload, client script). Add a document shell when you want your own <title>, fonts, or global CSS (Tailwind/shadcn). Do not bring back Vite’s index.html or src/main.tsx — the client entry is generated for you.

4. API route

src/routes/api/hello.ts
Objects become JSON. See Routing for redirects, status codes, and validation.

5. Run it

  • App: http://localhost:5173
  • API: http://localhost:5173/api/hello
The first page request is SSR HTML. Clicks on <Link> fetch JSON and swap the page on the client.
vite build emits dist/client (assets) and dist/server (the Worker module). To deploy that to Cloudflare, see Cloudflare Workers.

What you add next