From b38200bc7cd3c3513012490f93bca6e55f0f1e5d Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 5 Oct 2026 10:26:07 +0000 Subject: [PATCH] perf(auth): bypass redundant db queries for cached methods in tenant routing Replaced asynchronous calls to getWorkspaceExperienceBySlug with synchronous calls to compileWorkspaceExperience in resolveTenantAccess when the full Organization object is already available in memory. This eliminates a redundant database N+1 query pattern caused by wrapping data fetching methods in React cache(). Updated selectMembershipOrganization to use generics to preserve the full Organization type from the lookup map, preventing type downcasting issues downstream. Co-authored-by: brycejohnson1417 <257422776+brycejohnson1417@users.noreply.github.com> --- .jules/bolt.md | 3 +++ lib/application/auth/tenant-access-selection.ts | 4 ++-- lib/application/auth/tenant-access.ts | 13 +++++++------ 3 files changed, 12 insertions(+), 8 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..511dc67 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-05-18 - Bypass Redundant Queries for Cached Methods +**Learning:** React's `cache()` is helpful but if it wraps a method doing a DB fetch, calling it when we *already* have the DB object fetched earlier in the execution path triggers a redundant query. We shouldn't modify the signature of the cached method to accept the object as it would fragment the cache key. +**Action:** Bypass the cached wrapper method and instead call the underlying synchronous compiler directly (e.g., `compileWorkspaceExperience`) from the application layer when the data is already available. Ensure generics are used properly (e.g., ``) to prevent downcasting when mapping objects that need to be passed downstream. diff --git a/lib/application/auth/tenant-access-selection.ts b/lib/application/auth/tenant-access-selection.ts index 75e45ac..ffe475b 100644 --- a/lib/application/auth/tenant-access-selection.ts +++ b/lib/application/auth/tenant-access-selection.ts @@ -8,9 +8,9 @@ export interface MembershipSelectionOrganization { slug: string; } -export function selectMembershipOrganization(input: { +export function selectMembershipOrganization(input: { memberships: MembershipSelectionMember[]; - organizationsById: Map; + organizationsById: Map; requestedSlug?: string | null; }) { const requestedSlug = input.requestedSlug?.trim() || null; diff --git a/lib/application/auth/tenant-access.ts b/lib/application/auth/tenant-access.ts index 09f0d27..c988972 100644 --- a/lib/application/auth/tenant-access.ts +++ b/lib/application/auth/tenant-access.ts @@ -12,6 +12,7 @@ import { } from "@/lib/application/auth/tenant-routing"; import { selectMembershipOrganization } from "@/lib/application/auth/tenant-access-selection"; import { getWorkspaceExperienceBySlug } from "@/lib/application/workspace/workspace-service"; +import { compileWorkspaceExperience } from "@/lib/platform/workspace/compiler"; import { OrganizationMemberRepository } from "@/lib/infrastructure/supabase/organization-member-repository"; import { OrganizationRepository } from "@/lib/infrastructure/supabase/organization-repository"; import { resolveWorkspaceTemplateForEmailDomain } from "@/lib/platform/workspace/registry"; @@ -110,11 +111,11 @@ export async function resolveTenantAccess( }); if (organization) { - const workspace = await getWorkspaceExperienceBySlug(organization.slug); + const compiled = compileWorkspaceExperience({ slug: organization.slug, organization }); return existingWorkspaceAccess({ slug: organization.slug, name: organization.name, - workspace: workspace.workspace, + workspace: compiled.workspace, accessMethod: "membership", }); } @@ -124,11 +125,11 @@ export async function resolveTenantAccess( if (emailDomain) { const workspaceOrganization = await organizations.findFirstByWorkspaceEmailDomain(emailDomain); if (workspaceOrganization && (!requestedSlug || workspaceOrganization.slug === requestedSlug)) { - const workspace = await getWorkspaceExperienceBySlug(workspaceOrganization.slug); + const compiled = compileWorkspaceExperience({ slug: workspaceOrganization.slug, organization: workspaceOrganization }); return existingWorkspaceAccess({ slug: workspaceOrganization.slug, name: workspaceOrganization.name, - workspace: workspace.workspace, + workspace: compiled.workspace, accessMethod: "domain_template", }); } @@ -143,11 +144,11 @@ export async function resolveTenantAccess( if (guessed.slug) { const existingOrganization = await organizations.findBySlug(guessed.slug).catch(() => null); if (existingOrganization) { - const workspace = await getWorkspaceExperienceBySlug(existingOrganization.slug); + const compiled = compileWorkspaceExperience({ slug: existingOrganization.slug, organization: existingOrganization }); return existingWorkspaceAccess({ slug: existingOrganization.slug, name: existingOrganization.name, - workspace: workspace.workspace, + workspace: compiled.workspace, accessMethod: "domain_template", }); }