⚡ Bolt: Deduplicate database and auth lookups with React cache()#67
⚡ Bolt: Deduplicate database and auth lookups with React cache()#67RAbuseedo wants to merge 1 commit into
Conversation
- Wrapped core authentication and data fetching functions with React's `cache()`. - Implemented a conditional `memoize` helper for compatibility with Next.js Edge Runtime (Middleware). - Targeted `getUser`, `getUserWithTeam`, `getTeamForUser`, and `verifyToken` to eliminate redundant operations per request. - Added explanatory `⚡ OPTIMIZATION` comments. Co-authored-by: RAbuseedo <97478862+RAbuseedo@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
💡 What
Deduplicated core database lookups and JWT verifications by wrapping foundational functions (
getUser,getUserWithTeam,getTeamForUser, andverifyToken) with React'scache(). A conditionalmemoizehelper was used to ensure compatibility with the Edge Runtime (wherecache()is unavailable).🎯 Why
In the current architecture,
getUser()and related functions are called redundantly across the request lifecycle—specifically inRootLayout,Middleware, and individual Page components. This leads to multiple database roundtrips and repeated JWT cryptographic verifications for the same session data within a single request.📊 Impact
Eliminates redundant database queries and JWT verifications. For a typical authenticated request, this reduces the number of database calls for user context from ~3 to 1, improving server-side response times and reducing database load.
🔬 Measurement
pnpm exec tsc --noEmitto verify type safety.getUserstill functions).getUser()result in only a single SQL query per request.PR created automatically by Jules for task 3305871368130178304 started by @RAbuseedo