Performance Optimization and Vercel Best Practices
Applied Vercel and Next.js best practices across every API route, service, hook, and provider. Parallelized async work, deferred non-critical tasks with after(), added mutex-based token refresh, and eliminated redundant re-renders.
Improved
- Parallelized dbConnect() and request.json() with Promise.all across all API routes, reducing cold-start latency.
- Deferred non-blocking work (verification emails, welcome emails, auth event logging) using Next.js after() to speed up response times.
- Parallelized GitHub OAuth user and emails API calls with Promise.all, and user profile + auth data queries in user-details endpoint.
- Added mutex pattern to token refresh interceptor preventing concurrent 401 responses from triggering duplicate refresh requests.
- Converted avatar upload hook from manual useCallback to useMutation with automatic cache invalidation via TanStack Query.
- Replaced manual username availability check (useState + useEffect) with useQuery for built-in caching, deduplication, and stale-time control.
- Simplified useLogout by replacing identical onSuccess and onError handlers with a single onSettled callback.
- Replaced useEffect-based provider initialization with lazy useState for synchronous cookie reads in useLastUsedProvider.
- Hardened AuthProvider with stored user validation, reactive hasToken state, and cleanup on corrupt localStorage data.
- Narrowed useEffect dependencies in FontProvider and profile form to primitives, preventing unnecessary re-renders from object references.
- Added typed return values (ApiResponse<T>) to all service functions for end-to-end type safety.
- Parallelized user data deletion (UserModel, UserSessionModel, UserAuthModel) with Promise.all in delete-user endpoint.
- Removed dead barrel export files (lib/auth/auth.ts, protected/components/index.tsx) that added bundle weight with zero consumers.
Fixed
- Fixed OAuth welcome email bug where welcome messages were sent on every login instead of only on first signup.
- Fixed cache race condition in useUpdateUserDetails and useChangeUsername by awaiting invalidateQueries before reading fresh cache.
- Fixed useDeleteUser hook that was logging users out even when account deletion failed.
- Fixed Google OAuth token exchange using wrong client_secret value (was sending client ID instead of secret).
- Fixed UploadThing avatar upload 500 error by passing JWT authorization header via useUploadThing headers callback.






