Skip to content

⚡ Bolt: Deduplicate database and auth lookups with React cache()#67

Open
RAbuseedo wants to merge 1 commit into
mainfrom
bolt-deduplicate-lookups-3305871368130178304
Open

⚡ Bolt: Deduplicate database and auth lookups with React cache()#67
RAbuseedo wants to merge 1 commit into
mainfrom
bolt-deduplicate-lookups-3305871368130178304

Conversation

@RAbuseedo

Copy link
Copy Markdown
Owner

💡 What

Deduplicated core database lookups and JWT verifications by wrapping foundational functions (getUser, getUserWithTeam, getTeamForUser, and verifyToken) with React's cache(). A conditional memoize helper was used to ensure compatibility with the Edge Runtime (where cache() is unavailable).

🎯 Why

In the current architecture, getUser() and related functions are called redundantly across the request lifecycle—specifically in RootLayout, 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

  1. Run pnpm exec tsc --noEmit to verify type safety.
  2. Sign in to the application and verify that the dashboard loads correctly (confirming getUser still functions).
  3. (In a local environment) Enable database query logging and observe that multiple calls to getUser() result in only a single SQL query per request.

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

- 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>
@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