1. Install
better-router@alpha, hono, react, react-dom, and vite.
2. Enable the plugin
Replace the default Vite plugin list withbetterRouter(). It already includes @vitejs/plugin-react.
vite.config.ts
package.json
src/vite-env.d.ts
tsconfig.json
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
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
5. Run it
- App:
http://localhost:5173 - API:
http://localhost:5173/api/hello
<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.