diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..1c1bfca --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-07-03 - Bypass cache wrapper for pre-fetched entities +**Learning:** In React's `cache()` wrappers (like `getWorkspaceExperienceBySlug`), adding optional pre-fetched object arguments fragments the cache key and degrades performance. Bypassing the cached database fetch entirely and calling the internal synchronous compiler directly when we already have the data is much faster. +**Action:** When an entity is already fetched (like `organization` during tenant access resolution), bypass `getWorkspaceExperienceBySlug` and call `compileWorkspaceExperience` directly to avoid redundant database queries. diff --git a/lib/application/auth/tenant-access.ts b/lib/application/auth/tenant-access.ts index 09f0d27..d43b18e 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,6 +111,24 @@ export async function resolveTenantAccess( }); if (organization) { + // organization here is of type MembershipSelectionOrganization, which is a pick of id, name, slug. + // But we have the full organization record in organizationsById + const fullOrganization = organizationsById.get(organization.id); + if (fullOrganization) { + // Optimization: we already fetched the full organization, bypass the database call + const workspace = compileWorkspaceExperience({ + slug: fullOrganization.slug, + organization: fullOrganization, + }); + return existingWorkspaceAccess({ + slug: fullOrganization.slug, + name: fullOrganization.name, + workspace: workspace.workspace, + accessMethod: "membership", + }); + } + + // Fallback const workspace = await getWorkspaceExperienceBySlug(organization.slug); return existingWorkspaceAccess({ slug: organization.slug, @@ -124,7 +143,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); + // Optimization: we already fetched the full organization, bypass the database call + const workspace = compileWorkspaceExperience({ + slug: workspaceOrganization.slug, + organization: workspaceOrganization, + }); return existingWorkspaceAccess({ slug: workspaceOrganization.slug, name: workspaceOrganization.name, @@ -143,7 +166,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); + // Optimization: we already fetched the full organization, bypass the database call + const workspace = compileWorkspaceExperience({ + slug: existingOrganization.slug, + organization: existingOrganization, + }); return existingWorkspaceAccess({ slug: existingOrganization.slug, name: existingOrganization.name,