Skip to content

Commit 3da805f

Browse files
authored
fix(plugin-multi-tenant): infinite api calling loop on user switch (#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
1 parent d7c07b3 commit 3da805f

1 file changed

Lines changed: 2 additions & 4 deletions

File tree

  • packages/plugin-multi-tenant/src/providers/TenantSelectionProvider

packages/plugin-multi-tenant/src/providers/TenantSelectionProvider/index.client.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,12 @@ export const TenantSelectionProviderClient = ({
251251
// user logging out
252252
setSelectedTenantID(undefined)
253253
deleteTenantCookie()
254-
if (tenantOptions.length > 0) {
255-
setTenantOptions([])
256-
}
254+
setTenantOptions((prev) => (prev.length > 0 ? [] : prev))
257255
router.refresh()
258256
}
259257
prevUserID.current = userID
260258
}
261-
}, [userID, userChanged, syncTenants, tenantOptions, initialValue, router])
259+
}, [userID, userChanged, syncTenants, initialValue, router])
262260

263261
/**
264262
* If there is no initial value, clear the tenant and refresh the router.

0 commit comments

Comments
 (0)