Production-ready Next.js 14 app for real-time multi-pass text refinement, results, diffing, batch results, and analytics.
- Next.js 14 (App Router)
- React, TypeScript
- shadcn/ui
NEXT_PUBLIC_REFINER_BACKEND_URL(e.g.,http://localhost:8000)NEXT_PUBLIC_REFINER_BACKEND_WS_URL(optional WebSocket base, e.g.,ws://localhost:8000)NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY(optional; Stripe init is guarded)
pnpm install # or npm install
yarn dev || pnpm dev || npm run devOpen http://localhost:3000.
pnpm build
pnpm start- All refinement is streamed via SSE through the Next.js route:
app/api/refine/run/route.ts. - Required headers set:
Content-Type: text/event-stream,Cache-Control: no-cache, no-transform,Connection: keep-alive,X-Accel-Buffering: no. - Runtime flags:
export const runtime = 'nodejs',export const dynamic = 'force-dynamic'to prevent buffering. - The client (
lib/refiner-client.ts) parses SSE, handles terminal markers, and strips any accidental doubledata:prefix.
- Start refinement: UI in
components/processing-controls.tsx→ callsrefinerClient.startRefinement() - Download:
components/results-viewer.tsx→/api/files/download - Diff:
components/diff-viewer.tsxusesrefinerClient.getDiff()
contexts/ProcessingContext.tsx— global processing state & event logcontexts/SchemaContext.tsx— schema levels across UIcontexts/AnalyticsContext.tsx— analytics fetch and memoized presentation
processing-controls.tsx— start/stop, schema integrationresults-viewer.tsx— real-time results, download, metricsdiff-viewer.tsx— choose file/passes, fetch and show diffsbatch-results.tsx— merges API job data with event streamprogress-tracker.tsx— per-pass stage progression
- All backend calls route through
/api/*Next routes to avoid CORS and preserve headers.
- Verbose console logs removed in production code to avoid leaking sensitive data.
- Only
stream_endarrives: verify proxy headers/runtime; ensureX-Accel-Buffering: noset. - Missing
pass_complete: ensure backend yields per-pass events; confirm client doesn’t early-return on terminal markers. - Double
data:: proxy should not wrap; client strips defensively.
- Use browser DevTools to confirm SSE frames under Network → refine/run → EventStream.
By Ali Ahmed, AI engineer.