Chat Next.js
The @pressw/chat-nextjs
package provides Next.js-specific integrations for the @pressw/chat-core
package. This separate package enables framework-agnostic use of chat-core while providing optimized Next.js functionality.
Features
- Next.js Route Handlers: Pre-built API route handlers for thread management
- Server Components: Optimized server-side rendering support
- App Router: Full support for Next.js 13+ App Router
- Type Safety: Full TypeScript support with Next.js specific types
- SSR/SSG: Server-side and static generation compatibility
- Middleware: Built-in authentication and tenant isolation
- Error Handling: Next.js optimized error responses
Why a Separate Package?
By separating Next.js specific functionality from @pressw/chat-core
, we enable:
- Framework Agnostic Core: Use chat-core with Vite, Create React App, Expo, or any React framework
- Optional Next.js Features: Only include Next.js dependencies when needed
- Smaller Bundle Size: Core package stays lightweight without Next.js dependencies
- Better Tree Shaking: Import only the functionality you need
- Clear Separation: Framework-specific code is isolated and maintainable
Installation
bun add @pressw/chat-nextjs @pressw/chat-core
# or
npm install @pressw/chat-nextjs @pressw/chat-core
note
The @pressw/chat-nextjs
package requires @pressw/chat-core
as a peer dependency.
Quick Start
1. Set up your database adapter
First, configure your database adapter with @pressw/chat-core
:
// lib/adapter.ts
import { createDrizzleAdapter } from '@pressw/chat-core/adapters';
import { db } from './db';
export const adapter = createDrizzleAdapter({ db });
2. Create route handlers
Use the pre-built route handlers for your API:
// app/api/chat/threads/route.ts
import { createThreadRouteHandlers } from '@pressw/chat-nextjs';
import { adapter } from '@/lib/adapter';
import { getUserContext } from '@/lib/auth';
const handlers = createThreadRouteHandlers({
adapter,
getUserContext,
});
export const GET = handlers.GET;
export const POST = handlers.POST;
// app/api/chat/threads/[id]/route.ts
import { createThreadDetailRouteHandlers } from '@pressw/chat-nextjs';
import { adapter } from '@/lib/adapter';
import { getUserContext } from '@/lib/auth';
const handlers = createThreadDetailRouteHandlers({
adapter,
getUserContext,
});
export const GET = handlers.GET;
export const PUT = handlers.PUT;
export const DELETE = handlers.DELETE;
3. Use React hooks in your components
// components/ThreadList.tsx
import { useThreads } from '@pressw/chat-core/react';
export function ThreadList() {
const { data, isLoading, error } = useThreads({
listOptions: { limit: 20 }
});
if (isLoading) return <div>Loading...</div>;
if (error) return <div>Error: {error.message}</div>;
return (
<div>
{data?.threads.map((thread) => (
<div key={thread.id}>
<h3>{thread.title}</h3>
<p>{thread.createdAt.toLocaleDateString()}</p>
</div>
))}
</div>
);
}
API Routes
Thread Management Routes
The @pressw/chat-nextjs
package provides three main types of route handlers:
Individual Route Handlers
For granular control over each endpoint:
// app/api/chat/threads/route.ts - List and create threads
export const { GET, POST } = createThreadRouteHandlers(config);
// app/api/chat/threads/[id]/route.ts - Single thread operations
export const { GET, PUT, DELETE } = createThreadDetailRouteHandlers(config);
Catch-All Route Handler
For simplified setup with a single route file:
// app/api/chat/[...route]/route.ts
import { createCatchAllThreadRouteHandler } from '@pressw/chat-nextjs';
const handler = createCatchAllThreadRouteHandler({
adapter,
getUserContext,
});
export const GET = handler;
export const POST = handler;
export const PUT = handler;
export const DELETE = handler;