Server Actions
Type-safe server-side functionality with client-side integration using React Query.
Server actions and mutations provide type-safe server-side functionality with client-side integration using React Query. This pattern enables secure server operations with optimized UX through client-side state management.
Project Structure
The application follows a structured approach to organizing server actions and client mutations.
Server Actions
Server actions are server-only functions that handle operations like database
mutations, authentication, and API integrations. They are marked with the
"use server"
directive to ensure they only execute on the server.
Server-Side Implementation
Server actions are located in server/actions/
and handle server-side operations with built-in error handling and rate limiting.
Example: User Action
Server-side implementation of user creation:
Error Handling
Server actions implement standardized error handling using custom error types:
Client Mutations
Client mutations are React hooks that wrap server actions with React Query for state management, providing optimistic updates, loading states, and automatic cache invalidation.
Client-Side Integration
Client mutations are React hooks located in server/db/mutations/
that wrap server actions with React Query for state management.
Example: User Mutation Hook
Client-side mutation hook for user creation:
Standardized Naming Conventions
Client mutations follow a naming convention to clearly distinguish between client-side and server-side functions:
- Hooks are named with
use
prefix (e.g.,useCreateUser
) - Server action functions exposed to components are prefixed with
server_
(e.g.,server_createUser
) - Server-side actions end with
Action
suffix (e.g.,createUserAction
)
Usage in Components
Component implementation uses the client mutation hooks to interact with server actions while providing proper loading states and error handling.
Example: Using Mutations
Using mutation hooks in React components:
Using with React Hook Form
Integration with React Hook Form for form handling:
Key Features
Rate Limiting
⏱️ Built-in rate limiting using Upstash Redis
Error Handling
🛡️ Standardized error handling with custom error types
Type Safety
🔒 Full TypeScript support with Zod validation
State Management
🔄 Automatic cache invalidation with React Query
Common Server Actions
The application includes several common server actions for authentication, user management, and workspace operations.
Authentication Actions
Workspace Actions
Best Practices
Server Actions
- Use 'use server' directive
- Implement proper validation
- Handle all error cases
- Use transactions for related operations
Client Mutations
- Prefix mutation functions with 'server_'
- Handle loading states
- Provide meaningful error messages
- Update UI optimistically when possible
Error Handling
- Use custom error types
- Include specific error messages
- Log errors for debugging
- Return consistent error formats
TypeScript
- Define input and output types
- Use Zod for validation
- Export type definitions
- Use generics for reusable actions
All server actions should be properly rate-limited and validated to prevent abuse. Always handle errors appropriately and provide meaningful feedback to users. Learn more about rate limiting and error handling.
Advanced Usage
Optimistic Updates
Implement optimistic updates for better user experience:
Batch Operations
Handle multiple operations in a single server action:
Additional Resources
React Query Documentation
Learn more about React Query state management on the TanStack Query documentation.
Next.js Server Actions
Read about server actions in the Next.js documentation.
Zod Validation
Explore Zod documentation for type validation.
Rate Limiting
See our Rate Limiting documentation for details on implementation.