Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Bolt's Performance Journal ⚡

## Philosophy
- Speed is a feature.
- Every millisecond counts.
- Measure first, optimize second.
- Don't sacrifice readability for micro-optimizations.

## 2026-04-23 - Request Memoization with React cache()
**Learning:** React's `cache()` function allows for deduplicating expensive operations (like database queries and JWT verifications) within a single server-side request lifecycle. This is particularly useful in Next.js App Router where multiple components or layouts might need the same user data.

**Action:** Implement `cache()` for `getUser`, `getUserWithTeam`, `getTeamForUser`, and `verifyToken` to minimize redundant work and database load during page renders.
23 changes: 17 additions & 6 deletions lib/auth/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { compare, hash } from 'bcryptjs';
import { SignJWT, jwtVerify } from 'jose';
import { cookies } from 'next/headers';
import { NewUser } from '@/lib/db/schema';
import { cache } from 'react';

const key = new TextEncoder().encode(process.env.AUTH_SECRET);
const SALT_ROUNDS = 10;
Expand Down Expand Up @@ -30,12 +31,22 @@ export async function signToken(payload: SessionData) {
.sign(key);
}

export async function verifyToken(input: string) {
const { payload } = await jwtVerify(input, key, {
algorithms: ['HS256'],
});
return payload as SessionData;
}
// ⚡ OPTIMIZATION: Memoize JWT verification to avoid redundant CPU-intensive decodes
// We check if cache is a function to ensure compatibility with Edge Runtime (middleware)
export const verifyToken =
typeof cache === 'function'
? cache(async (input: string) => {
const { payload } = await jwtVerify(input, key, {
algorithms: ['HS256'],
});
return payload as SessionData;
})
: async (input: string) => {
const { payload } = await jwtVerify(input, key, {
algorithms: ['HS256'],
});
return payload as SessionData;
};

export async function getSession() {
const session = (await cookies()).get('session')?.value;
Expand Down
16 changes: 10 additions & 6 deletions lib/db/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { db } from './drizzle';
import { activityLogs, teamMembers, teams, users } from './schema';
import { cookies } from 'next/headers';
import { verifyToken } from '@/lib/auth/session';
import { cache } from 'react';

export async function getUser() {
// ⚡ OPTIMIZATION: Memoize user lookups to deduplicate DB queries within a single request
export const getUser = cache(async () => {
const sessionCookie = (await cookies()).get('session');
if (!sessionCookie || !sessionCookie.value) {
return null;
Expand Down Expand Up @@ -34,7 +36,7 @@ export async function getUser() {
}

return user[0];
}
});

export async function getTeamByStripeCustomerId(customerId: string) {
const result = await db
Expand Down Expand Up @@ -64,7 +66,8 @@ export async function updateTeamSubscription(
.where(eq(teams.id, teamId));
}

export async function getUserWithTeam(userId: number) {
// ⚡ OPTIMIZATION: Memoize user-team joins to deduplicate DB queries within a single request
export const getUserWithTeam = cache(async (userId: number) => {
const result = await db
.select({
user: users,
Expand All @@ -76,7 +79,7 @@ export async function getUserWithTeam(userId: number) {
.limit(1);

return result[0];
}
});

export async function getActivityLogs() {
const user = await getUser();
Expand All @@ -99,7 +102,8 @@ export async function getActivityLogs() {
.limit(10);
}

export async function getTeamForUser(userId: number) {
// ⚡ OPTIMIZATION: Memoize complex team lookups to deduplicate DB queries within a single request
export const getTeamForUser = cache(async (userId: number) => {
const result = await db.query.users.findFirst({
where: eq(users.id, userId),
with: {
Expand All @@ -126,4 +130,4 @@ export async function getTeamForUser(userId: number) {
});

return result?.teamMembers[0]?.team || null;
}
});