Files
Nathan Rajlich 7653e6bfdb Migrate @workflow/web from Next.js to React Router v7 (#1005)
## Summary

- Replace Next.js App Router with React Router v7.13.0 framework mode (Vite-based), eliminating the large `next` dependency from the web, CLI, and workflow metapackages
- Serve the web UI in-process from the CLI via Express instead of spawning `next start` as a child process
- Switch RPC transport from JSON to CBOR to preserve binary data types across the wire
- Replace `nuqs` URL state management with React Router's `useSearchParams`
- Replace Next.js server actions with an RPC resource route (`/api/rpc`) and a thin CBOR-based client

## Motivation

The `next` package is ~300MB installed and was the single largest dependency in the monorepo. It also required spawning a separate child process from the CLI to run the o11y web server, adding complexity around process lifecycle management, port readiness polling, and environment variable forwarding.

With React Router framework mode, the web package builds to a standard Express-compatible server bundle that the CLI can import and serve directly in its own process.

## What changed

**Framework swap (`@workflow/web`):**
- `next.config.ts` / `postcss.config.mjs` → `react-router.config.ts` / `vite.config.ts`
- `src/` directory → `app/` directory (React Router convention)
- `src/app/layout.tsx` + `layout-client.tsx` → `app/root.tsx`
- `src/app/page.tsx` → `app/routes/home.tsx`
- `src/app/run/[runId]/page.tsx` → `app/routes/run-detail.tsx`
- Path alias `@/` → `~/`
- Removed all `'use client'` / `'use server'` directives

**Data transport:**
- Server actions → RPC resource route at `/api/rpc` with CBOR encoding
- CBOR preserves `Uint8Array` and other binary types natively (no base64 overhead)
- Stream reading → dedicated `/api/stream/:streamId` resource route

**URL state:**
- `nuqs` (`useQueryState`) → `useSearchParams` from `react-router`

**Fonts:**
- `next/font/google` → Geist `.woff2` files referenced directly from `node_modules/geist` via `@font-face` in CSS

**CLI integration (`@workflow/cli`):**
- `import('@workflow/web/server').then(m => m.startServer(port))`
- No child process, no readiness polling, no cleanup handlers

**Radix UI compatibility:**
- `onSubmit` preventDefault on `AlertDialogContent` and `SheetContent` to prevent Radix's internal `<form method="dialog">` from triggering React Router route actions
- Catch-all action on root route for any stray POSTs

## Dependencies removed
- `next`, `swr`, `nuqs`, `@tailwindcss/postcss`

## Dependencies added
- `react-router` / `@react-router/dev` / `@react-router/node` / `@react-router/express` (all `7.13.0`)
- `express`, `vite`, `@tailwindcss/vite`, `cbor-x`, `isbot`, `cross-env`
- `geist` (devDep)
2026-02-12 16:30:51 -08:00

153 lines
4.7 KiB
TypeScript

import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from '~/components/ui/tooltip';
import { useServerConfig } from '~/lib/world-config-context';
type ServerConfigValue = ReturnType<typeof useServerConfig>['serverConfig'];
function getLocalShortName(displayInfo: ServerConfigValue['displayInfo']) {
return displayInfo?.['local.shortName'];
}
function _getLocalDataDirPath(displayInfo: ServerConfigValue['displayInfo']) {
return displayInfo?.['local.dataDirPath'];
}
function getShowLocalMisconfigWarning(
backendId: string,
displayInfo: ServerConfigValue['displayInfo']
): boolean {
return (
(backendId === 'local' || backendId === '@workflow/world-local') &&
getLocalShortName(displayInfo) === 'packages/web'
);
}
function getVercelDisplayString(
publicEnv: ServerConfigValue['publicEnv']
): string {
const env = publicEnv.WORKFLOW_VERCEL_ENV || 'production';
const team = publicEnv.WORKFLOW_VERCEL_TEAM;
const project = publicEnv.WORKFLOW_VERCEL_PROJECT;
const vercelInfo =
team && project ? `${team}/${project}` : project || team || 'Unknown';
return `Connected to Vercel ${env} (${vercelInfo})`;
}
function getPostgresDisplayString(
displayInfo: ServerConfigValue['displayInfo']
): string {
const host = displayInfo?.['derived.WORKFLOW_POSTGRES_URL.hostname'];
const db = displayInfo?.['derived.WORKFLOW_POSTGRES_URL.database'];
if (!host) return 'Connected to Postgres';
return `Connected to Postgres (${host}${db ? `/${db}` : ''})`;
}
function getDisplayString(config: ServerConfigValue): string {
const { backendDisplayName, backendId, displayInfo, publicEnv } = config;
switch (backendId) {
case 'local':
case '@workflow/world-local':
return `Local Dev: ${getLocalShortName(displayInfo) || 'Unknown'}`;
case 'vercel':
case '@workflow/world-vercel':
return getVercelDisplayString(publicEnv);
case 'postgres':
case '@workflow/world-postgres':
return getPostgresDisplayString(displayInfo);
default:
return `Connected to: ${backendDisplayName}`;
}
}
function renderKeyValueTable(rows: Array<{ key: string; value: string }>) {
return (
<div className="grid grid-cols-[auto,1fr] gap-x-4 gap-y-1">
{rows.map(({ key, value }) => (
<div key={key} className="contents">
<div className="font-mono text-xs text-muted-foreground">{key}</div>
<div className="font-mono text-xs text-foreground break-all">
{value}
</div>
</div>
))}
</div>
);
}
/**
* Displays the current world connection status.
*
* This component shows information from the public server configuration.
* Env-derived values are strictly allowlisted per world backend.
*/
export function ConnectionStatus() {
const { serverConfig } = useServerConfig();
const displayString = getDisplayString(serverConfig);
const { backendId } = serverConfig;
const showLocalMisconfigWarning = getShowLocalMisconfigWarning(
backendId,
serverConfig.displayInfo
);
const publicEnvEntries = Object.entries(serverConfig.publicEnv).sort(
([a], [b]) => a.localeCompare(b)
);
const sensitiveKeys = [...serverConfig.sensitiveEnvKeys].sort();
const hasTooltip =
publicEnvEntries.length > 0 ||
sensitiveKeys.length > 0 ||
showLocalMisconfigWarning;
const content = (
<div className="text-md whitespace-nowrap">
<span className="font-medium">{displayString}</span>
</div>
);
// TODO: Based on queue or HTTP health check, show a live status icon.
if (!hasTooltip) {
return content;
}
return (
<Tooltip>
<TooltipTrigger asChild>{content}</TooltipTrigger>
<TooltipContent>
<div className="flex flex-col gap-2 max-w-[640px]">
{showLocalMisconfigWarning && (
<div className="mb-2">
<div className="font-medium text-foreground">
Local data directory looks misconfigured
</div>
<div className="text-muted-foreground">
This UI appears to be pointing at <code>packages/web</code>.
Configure the local data directory / working directory and
restart the web UI. See{' '}
<a
className="underline"
href="https://useworkflow.dev/docs/observability"
target="_blank"
rel="noopener noreferrer"
>
docs
</a>
.
</div>
</div>
)}
{renderKeyValueTable([
...publicEnvEntries.map(([key, value]) => ({ key, value })),
...sensitiveKeys.map((key) => ({ key, value: '*****' })),
])}
</div>
</TooltipContent>
</Tooltip>
);
}