Skip to content

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

Merged
JarrodMFlesch merged 1 commit intomainfrom
fix/multi-tenant-use-effect
Apr 1, 2026
Merged

fix(plugin-multi-tenant): infinite api calling loop on user switch#16065
JarrodMFlesch merged 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
@JarrodMFlesch JarrodMFlesch enabled auto-merge (squash) March 31, 2026 19:41
@JarrodMFlesch JarrodMFlesch added the plugin: multi-tenant @payloadcms/plugin-multi-tenant label Mar 31, 2026
@JarrodMFlesch JarrodMFlesch merged commit 3da805f into main Apr 1, 2026
308 of 311 checks passed
@JarrodMFlesch JarrodMFlesch deleted the fix/multi-tenant-use-effect branch April 1, 2026 01:18
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 8, 2026

🚀 This is included in version v3.82.0

milamer pushed a commit to milamer/payload that referenced this pull request Apr 20, 2026
…ayloadcms#16065)

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)


---
- To see the specific tasks where the Asana app for GitHub is being
used, see below:
  - https://app.asana.com/0/0/1213822464852864
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants