Skip to content

fix(plugin-multi-tenant): infinite api calling loop on user switch#16065

Open
r1tsuu wants to merge 1 commit intomainfrom
fix/multi-tenant-use-effect
Open

fix(plugin-multi-tenant): infinite api calling loop on user switch#16065
r1tsuu wants to merge 1 commit intomainfrom
fix/multi-tenant-use-effect

Conversation

@r1tsuu
Copy link
Copy Markdown
Member

@r1tsuu r1tsuu commented Mar 26, 2026

Previously, using the multi-tenant example you were able to reproduce the following:

  • Login as super-admin demo@payloadcms.com demo
  • Logout and login as tenant1 tenant1@payloadcms.com demo
  • Logout and login as tenant2 tenant2@payloadcms.com demo

You'd see an infinite spam of requests to /tenants/populate-tenant-options because of useEffect in TenantSelectionProvider/index.client.tsx

Why?
Login as Tenant 2 → userChanged=true → syncTenants() fetches data
→ setTenantOptions(new array) → tenantOptions reference changes
→ effect re-runs (tenantOptions in deps) → stale initialValue ≠ cookie
→ syncTenants() again → new array reference again → ∞

This PR makes the setTenantOptions call stable (avoid a new reference when not changed) and removes tenantOptions from the dependency array (the tenant options selector still updates correctly when logging out / logging in as a different user)


@github-actions
Copy link
Copy Markdown
Contributor

📦 esbuild Bundle Analysis for payload

This analysis was generated by esbuild-bundle-analyzer. 🤖
This PR introduced no changes to the esbuild bundle! 🙌

@r1tsuu r1tsuu requested a review from JarrodMFlesch March 26, 2026 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant