⚡ Bolt: request-level caching for auth and user queries#46
Conversation
Optimized the application by implementing request-level memoization for core authentication and data-fetching functions. - Wrapped `verifyToken` with React's `cache()` to deduplicate CPU-intensive JWT verifications, with a fallback for Edge Runtime/Middleware compatibility. - Memoized `getUser`, `getUserWithTeam`, and `getTeamForUser` to eliminate redundant database queries within a single request lifecycle. - Refactored memoized functions to `const` declarations and ensured correct definition order to avoid hoisting issues. Expected impact: Significant reduction in database load and CPU usage on pages with multiple server components requiring user/team context. 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: This PR implements request-level memoization for core authentication and user/team data fetching using React's
cache()utility.🎯 Why: In Next.js App Router, multiple server components often need access to the current user or team context (e.g., Layout, Page, and various sub-components). Without memoization, each call to
getUser()orverifyToken()triggers a new database query or JWT verification, leading to inefficient resource usage and slower response times.📊 Impact:
🔬 Measurement:
pnpm exec tsc --noEmitpasses.cachewrapper).PR created automatically by Jules for task 18197827694301516260 started by @RAbuseedo