AI summary by TwinMind
React + Servers = Confusion
Theo - t3․gg
20m 29s
Server Actions Overview
- Server actions rollout with Next.js/React has been confusing
- New pattern for data updates/mutations in React server components
- Multiple different behaviors depending on usage context (similar to useEffect)
- Common misunderstandings in implementation approaches
- Created repo showcasing different patterns with/without tRPC
Demo Setup & Database
- Used pnpm instead of bun due to Drizzle/SQLite compatibility issues
- SQLite database inlined in codebase for easy setup
- Three main examples: vanilla action, tRPC client, RSC tRPC action
- All examples work with/without JavaScript (progressive enhancement)
Vanilla Server Action Pattern
- Simple server components + server actions (no tRPC)
- Database queries using Drizzle ORM with type safety
- Key principle: Define actions where needed data already exists in closure
- Avoids manual form data parsing and type safety issues
- Post ID available in component scope, no need to pass through form
- Form data limitations:
- Data must be included in form to reach server action
- React embeds closure data in hidden encrypted form fields
- No type safety between form inputs and server action parameters
- Create post action:
formData.get('post-name')with manual string casting - Delete post action: Uses post ID from component closure
revalidatePath()regenerates page content in single request- Works without JavaScript via full page reload
tRPC Client-Side Pattern
- Mutations via tRPC (traditional client-side approach)
- Data loading still on server:
await api.post.getPosts() - Server caller pattern:
appRouter.createCaller() server-onlyimport prevents accidental client usage- Guaranteed single entry point for all data access
- Client components required for JavaScript behaviors
- Performance impact: Waterfall requests
- Mutation request → success response →
router.refresh()→ new data - Noticeably slower than vanilla server actions
- Double round trips vs single request with server actions
- Mutation request → success response →
- Manual state management (input clearing, loading states)
- Type safety through tRPC validation
Hybrid RSC + tRPC Pattern
- Combines server actions with tRPC data access layer
- Progressive enhancement benefits maintained
- Server actions call
api.post.delete(postId)through tRPC - Must use tRPC caller (not HTTP proxy) for server actions
- Limitation: Async context doesn't work with sync DB/headers
- Best of both worlds: tRPC validation + server action performance
- Single request with
revalidatePath()vs waterfall in client pattern
Client-Side Server Actions Pattern
- Server actions exported from
'use server'files - Can be imported and called like regular promises in client components
- Compiler transforms imports into fetch calls automatically
- Key difference:
'use server'file vs component-level actions- File exports: Creates RPC-like endpoints for client import
- Component actions: Form binding with progressive enhancement
- Custom input parameters (not just FormData)
- Manual state management but with server-side execution
- Libraries available: next-safe-action (recommended over zod-server-actions)
Implementation Patterns Comparison
- Vanilla actions: Best for progressive enhancement, single requests
- tRPC client: Traditional approach, requires JavaScript, waterfall requests
- Hybrid: Combines tRPC validation with server action performance
- Client server actions: RPC-like pattern, client control with server execution
- Data access layer recommended for security/auditing (react taint video reference)
- Choice depends on specific use case and requirements
