Skip to content

⚡ Bolt: implement request-level memoization using React cache#41

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

⚡ Bolt: implement request-level memoization using React cache#41
RAbuseedo wants to merge 1 commit into
mainfrom
bolt-request-memoization-9608255757870531926

Conversation

@RAbuseedo
Copy link
Copy Markdown
Owner

💡 What:

Implemented React cache() for core authentication and database fetching functions:

  • verifyToken in lib/auth/session.ts
  • getUser, getUserWithTeam, and getTeamForUser in lib/db/queries.ts

🎯 Why:

In Next.js App Router, multiple Server Components and the Layout often need access to the current user or team data. Without memoization, each call to these functions would trigger a new database query or JWT verification, even within the same request.

📊 Impact:

  • Reduces database load: Ensures that a user or team lookup only happens once per request, even if called by multiple components.
  • CPU optimization: Memoizes jwtVerify, which is an expensive cryptographic operation.
  • Improved developer experience: Components can directly call getUser() without needing to pass user data down as props (avoiding prop drilling) while still being performance-efficient.

🔬 Measurement:

Deduplication can be verified by adding logs to the cached functions and observing that they only fire once during a page render that accesses them multiple times (e.g., the dashboard page which calls getUser and getTeamForUser). Verified type safety with pnpm exec tsc --noEmit.


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

Deduplicate database queries and CPU-intensive JWT verification within a single
request lifecycle using React's `cache` function. This prevents redundant
operations when multiple Server Components access the same session or entity data.

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