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. 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.

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