Leaving Next.js for Vite, React Router and Hono: one codebase on Node, Cloudflare Workers and a desktop app
Our app was 175 'use client' files behind thin server wrappers, and it had to run in three places. Moving it to a Vite SPA plus a Hono server took the build from about 60 s to about 10 s, without changing a single API handler's signature. Here's how, and the two race conditions it exposed.
TablePad is a database client: a spreadsheet-style table grid, a SQL editor and a canvas. Its API routes talk to databases through Node drivers (pg, mysql2, better-sqlite3). The same code has to run in three places:
- The desktop app. A small Tauri launcher starts a bundled Node server on
127.0.0.1and points a window at it. - Self-hosted, with
pnpm start. - The hosted version, a Cloudflare Worker with D1 bindings.
For its first releases it was a Next.js app. At the end of October we moved it to a Vite single-page app with React Router, plus a Hono server. This post covers why, how we kept the diff small, what got measurably better, and the two bugs that only showed up afterwards.
This is not a "Next.js bad" post. The marketing site you're reading this on is still Next.js, and it should be.
The app was already a client app
When we counted, 175 files started with 'use client'. The 12 page and layout files that didn't were mostly one-line wrappers:
import DatabaseTablePageClient from './page-client';
export default function DatabaseTablePage() {
return <DatabaseTablePageClient />;
}That's not a mistake. A database client renders from IndexedDB caches and MobX stores, and its data is behind connections that live on the user's machine. There's nothing useful to server-render. We were paying for a framework whose main features we didn't use, and its runtime model was what made the three deployment targets hard:
- The desktop build needed Next's standalone server output, which has to be copied into the bundle.
- The Worker build went through an adapter,
@opennextjs/cloudflare, which is another moving part to keep in step with each Next release. - A
middleware.tsissued an anonymous account cookie on the hosted build, and server code read it withcookies()fromnext/headers, which only works inside Next's request scope.
Keep the folder structure, swap what's underneath
The goal was a small diff. People and tools (coding agents included) know where things live in src/app/, so that tree stayed exactly as it was. The commit touched 232 files outside the lockfile, for +1,194 and −707 lines. Most of them are a removed 'use client' line and a changed import.
Pages: a router that mirrors the folders
src/router.tsx maps the same tree to React Router by hand. Each page.tsx and layout.tsx is lazy-loaded and prefetched once the app is idle:
function page(load: () => Promise<PageModule>): Pick<RouteObject, 'lazy'> {
loaders.push(load);
return { lazy: async () => ({ Component: (await load()).default }) };
}One Next behavior had to be copied on purpose. When a dynamic segment changes, say from one workspace to another, everything below it should mount fresh, while the layouts above it stay mounted. A keyed fragment does that:
function DynamicSegment({ param, children }: { param: string; children: ReactNode }) {
const value = useParams()[param];
return <Fragment key={value}>{children}</Fragment>;
}Navigation hooks: one import line per file
Components imported useRouter, usePathname, useSearchParams and useParams from next/navigation. We wrote @/lib/navigation with the same names and the same semantics. That includes one easy-to-miss detail: Next returns params URL-encoded (my%20table) while React Router decodes them. Code throughout the app expected the encoded form, so the shim re-encodes them. After that, most component diffs were exactly one line:
- import { usePathname, useRouter, useSearchParams } from 'next/navigation';
+ import { usePathname, useRouter, useSearchParams } from '@/lib/navigation';API routes: they were already plain Fetch handlers
This is the part that made the whole move cheap. A Next route handler is a function from a Request to a Response, plus a context with params. That's just the Fetch API, and Hono speaks it natively. Each route.ts stayed where it was, with the same exports, and a table registers them:
export const API_ROUTES: ApiRoute[] = [
{ path: '/api/health', handlers: health },
{ path: '/api/workspaces/:workspaceId', handlers: workspacesWorkspaceId },
{ path: '/api/workspaces/:workspaceId/canvas/query', handlers: workspacesWorkspaceIdCanvasQuery },
// …
];
for (const route of API_ROUTES) {
for (const method of HTTP_METHODS) {
const handler = route.handlers[method];
if (!handler) continue;
app.on(method, route.path, c =>
handler(c.req.raw, { params: Promise.resolve(c.req.param()) })
);
}
}params stays a Promise because that's what Next 15+ handlers expect, so not a single handler had to change. Of 29 route files, 3 changed, and each change was deleting export const dynamic = 'force-dynamic'.
A hand-written table can drift from the folder tree, so a test walks src/app/api/**/route.ts and fails if any file isn't registered at the path its directory implies.
Middleware and cookies: AsyncLocalStorage
middleware.ts became a Hono middleware. cookies() from next/headers became a small request context on AsyncLocalStorage, which also carries the Worker's bindings to code deep inside services that never sees the Request:
const storage = new AsyncLocalStorage<RequestContext>();
app.use('*', async (c, next) => {
const context = { env: c.env ?? {}, cookies: parseCookieHeader(c.req.header('cookie')) };
const setCookie = issueAccountCookie(context);
await runWithRequestContext(context, next);
if (setCookie) {
c.res = new Response(c.res.body, c.res); // asset responses have immutable headers
c.res.headers.append('Set-Cookie', setCookie);
}
});On Workers, this needs the nodejs_compat compatibility flag.
One app, three entry points
createApp() builds the Hono app. Each runtime adds only how it serves the client:
| Runtime | Entry | Serves the UI with |
|---|---|---|
| Desktop and self-hosted | server/node.ts → esbuild → dist/server.js |
Files from dist/client/, with immutable caching for hashed /assets/ and index.html for any other path |
| Hosted | server/worker.ts |
The Worker's ASSETS binding, which falls back to index.html |
| Development | Vite plugin | Vite itself. /api/* goes to the same Hono app, loaded with ssrLoadModule, so a server edit applies on the next request with one port for both |
The Node server is bundled with esbuild into one CommonJS file. The database drivers stay external because better-sqlite3 is a native module, and SQLite canvas queries run in a child process that requires it by path.
What changed, measured
I built the commit before the migration and the current one on the same 4-core machine, twice each, from a clean output directory:
| Next.js 15.5 (before) | Vite 7 + esbuild (after) | |
|---|---|---|
| Production build | 56.8 s, 60.6 s | 10.2 s, 10.0 s |
node_modules on disk |
1.3 GB | 643 MB |
| Build output | .next/ (mixed with caches) |
9.7 MB: 4.2 MB client, 1.4 MB server.js, plus source maps |
The build time matters most day to day. Every over-the-air desktop release rebuilds the full app and smoke-tests it, so 50 seconds saved per build adds up.
The two bugs that instant navigation exposed
Both lived in code that was correct as long as navigation was a little slow. After the move, client navigation is instant, and they surfaced right away.
1. A route guard that decided too early. The table page redirects away when the table in the URL doesn't exist. After a workspace switch, the guard ran before the table list had loaded from either the cache or the network. It saw an empty list, concluded the table didn't exist, and redirected. The fix was to treat "attached, but nobody has answered yet" as loading:
// Attached but neither the cache nor the network has answered yet.
if (tablesStore.tables.length === 0 && tablesStore.lastFetchedAt === 0 && !tablesStore.loadError) {
return true; // still loading, don't redirect
}2. Reading the URL instead of the route. The app remembers the last table you opened, and it read the address from window.location. A client router writes the URL of a pending navigation before that route renders, so a quick switch sometimes recorded the next table under the previous page. The fix is to use the router's view of the rendered route:
// The rendered route, not window.location: the router writes the URL of a pending
// navigation before that route renders.
const search = searchParams.toString();
const href = search ? `${pathname}?${search}` : pathname;If you migrate, search your code for window.location and for any "if the list is empty, redirect" logic. Those are where timing assumptions hide.
What we gave up
- Server rendering and React Server Components. We weren't using them. If your pages need SEO or a fast first paint from server data, this trade is different for you.
- File-based routing for free.
router.tsxis about 240 lines that mirror the folders by hand. The route test covers the API side; the page side is checked by using it. next/imagein four components, which are now plain<img>tags with static files.- One framework to upgrade. Now there are three smaller ones: Vite, React Router and Hono. That's a trade-off, not a free win.
Should you do this?
If your Next app is mostly 'use client', your API routes are plain Request → Response handlers, and you deploy the same server to more than one runtime, then yes, probably. The handlers move almost untouched, and the rest is a router file and a navigation shim. If your app depends on server components, server actions or ISR, it's a rewrite, and the numbers above won't pay for it.