Skip to content

⚡ Bolt: request-level caching for auth and user queries#46

Open
RAbuseedo wants to merge 1 commit into
mainfrom
bolt-request-memoization-18197827694301516260
Open

⚡ Bolt: request-level caching for auth and user queries#46
RAbuseedo wants to merge 1 commit into
mainfrom
bolt-request-memoization-18197827694301516260

Conversation

@RAbuseedo
Copy link
Copy Markdown
Owner

💡 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() or verifyToken() triggers a new database query or JWT verification, leading to inefficient resource usage and slower response times.

📊 Impact:

  • Eliminates redundant database queries for user/team data within a single request.
  • Reduces CPU overhead by deduplicating JWT verification.
  • Ensures data consistency across the entire request lifecycle.

🔬 Measurement:

  • Verify that pnpm exec tsc --noEmit passes.
  • Inspect server logs to confirm that database queries for the same user/team are no longer repeated within a single page load.
  • Ensure Middleware still functions correctly (handled by the conditional cache wrapper).

PR created automatically by Jules for task 18197827694301516260 started by @RAbuseedo

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>
@google-labs-jules
Copy link
Copy Markdown

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant