From bee011635ca7f492ecd85f391bf1674ce08cb369 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 8 Jul 2026 10:40:36 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Bypass=20redundant=20databa?= =?UTF-8?q?se=20queries=20in=20tenant=20access?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 💡 What: Bypass `getWorkspaceExperienceBySlug` and use `compileWorkspaceExperience` directly when the `Organization` object is already fetched. 🎯 Why: To avoid redundant database queries and prevent cache fragmentation in React's `cache()`. 📊 Impact: Eliminates 1 unnecessary DB query per tenant resolution. 🔬 Measurement: Check network/DB traces to verify `organization` table is not queried twice during login. 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..1586fe4 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-07-08 - Bypass Cached Database Queries with Synchronous Compilation +**Learning:** When React's `cache()` wraps a data-fetching method (e.g., `getWorkspaceExperienceBySlug`), modifying its signature to accept optional pre-fetched objects fragments the cache. Furthermore, calling the cached method when the data (e.g., `Organization`) is already available results in redundant database queries. +**Action:** Instead of modifying the cached function, bypass it and directly call the internal synchronous compiler (e.g., `compileWorkspaceExperience`) from the application layer when the necessary data is already available, taking care not to reference properties (like `organization`) that are omitted from the compiler's return type. 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", }); }