Repository navigation
Tenant lookup refactor - #897
Conversation
Create single source of truth for all 30 US avalanche center slugs with their custom domains. This is the foundation for eliminating Edge Config and API-based tenant lookups. Changes: - Add src/utilities/tenancy/avalancheCenters.ts with AVALANCHE_CENTERS constant, ValidTenantSlug type, and helper functions - Update tenants.ts to use type guards and add isProductionTenant() helper - Update all usages of PRODUCTION_TENANTS.includes() to use isProductionTenant() - Update tests to use valid tenant slugs from the new hardcoded list Co-Authored-By: Claude Opus 4.5 <[email protected]>
- Change slug field from text to select with valid avalanche center options - Remove Edge Config sync hooks (updateEdgeConfigAfterChange/AfterDelete) - Remove cached-public endpoint for tenant lookups - Keep revalidation hooks for ISR cache invalidation Co-Authored-By: Claude Opus 4.5 <[email protected]>
Major simplification of the middleware and tenant lookup system: - Middleware is now synchronous - no Edge Config or API lookups - Cookie stores tenant slug instead of numeric ID - Remove Edge Config and cached API fallback from middleware - Make getTenantSubdomainUrls and getProductionTenantUrls synchronous - Delete getTenants.ts (no longer needed) - Delete vercel.ts service (Edge Config no longer used) - Update payload.config.ts CORS/CSRF to use synchronous functions This eliminates external service dependencies and reduces middleware latency. Co-Authored-By: Claude Opus 4.5 <[email protected]>
…ase 4) - Replace getTenantIdFromCookie with getTenantSlugFromCookie - Update getTenantFromCookie to read slug strings from cookie - Modify tenantField hook to look up tenant ID from slug - Update filterByTenant to query by tenant.slug - Update byTenantRole access control to compare slugs - Update collectionFilters with proper Where type annotation - Update getGlobalViewRedirect to resolve tenant ID from slug - Remove unused getCollectionIDType utility Co-Authored-By: Claude Opus 4.5 <[email protected]>
…ies (Phase 5) - Update TenantSelectionProvider to use slug as option value - Update TenantSelectionProviderClient with selectedTenantSlug context - Update TenantSelectorClient to use slug-based selection - Update TenantFieldComponent to look up tenant ID from slug - Update DuplicatePageForDrawer to use tenant slug - Update duplicatePageToTenant endpoint to accept slug parameter - Update getTenantOptions to include slug in select Co-Authored-By: Claude Opus 4.5 <[email protected]>
- Remove deprecated getTenantFromCookie function (use getTenantSlugFromCookie) - Remove Edge Config environment variables from .env.example - Create ADR 013-hardcoded-tenant-lookup documenting the new approach - Mark ADR 008-edge-config-tenant-lookup as superseded Co-Authored-By: Claude Opus 4.5 <[email protected]>
…nant and id filter too
… currently selected tenant cookie
… for tenant lookups
rchlfryn
left a comment
There was a problem hiding this comment.
PR looks good - minor comments
One major difference I was thinking before reviewing this PR is we were going to migrate away from using ids and use the slug as the primary key/id. I dug into it and we should not do that since Payload expects id and Payload's core functionality relies on the id field being the primary key. Maybe we add this to the decision doc?
A few questions:
What will happen for current logged in users - will the tenant cookie just be updated? or are they forced to re-login
Can we update the onboarding docs where adding the slug to the edge config in production? Are there any other docs we need to update?
|
|
||
| We're essentially migrating back to the approach we used before [007-dynamic-tenants-middleware.md](./007-dynamic-tenants-middleware.md) which is to have a hardcoded tenant list. We moved to a dynamic tenant lookup because we were using the multi-tenant plugin from Payload at that point which needed to use the tenants collection's id for the tenant cookie. | ||
|
|
||
| Since we [moved away from the multi-tenant plugin](./010-remove-multi-tenant-plugin.md), we can use a hardcoded list of all possible avalanche centers and use the slug for the `payload-tenant` cookie because we control the code. The biggest issue that [007-dynamic-tenants-middleware.md](./007-dynamic-tenants-middleware.md) accomplished was to avoid issues where ids were different between environments due to order in which they were created / the tenants that were actually created in a given environment. This isn't an issue when using the slug. |
|
|
||
| --- | ||
|
|
||
| ### Tenant-Required Collection (e.g., Pages, Posts) |
There was a problem hiding this comment.
⛏️ ⛏️ ⛏️
| ### Tenant-Required Collection (e.g., Pages, Posts) | |
| ### Tenant-Required Collection (e.g. Pages, Posts) |
| - [ ] The tenant selector should be hidden (only 1 option available) | ||
| - [ ] The tenant cookie should automatically be set to their single tenant's value | ||
| - [ ] All tenant-scoped operations should use their single tenant | ||
|
|
There was a problem hiding this comment.
Do we want to add provider manage here or make note of it?
There was a problem hiding this comment.
Yea we def should - although I guess maybe it makes more sense to expand this into RBAC docs not just tenant-selector specific. I'll think about this for my PR for #866
| { | ||
| name: 'slug', | ||
| type: 'text', | ||
| type: 'select', |
|
|
||
| if (!tenant) { | ||
| if (tenantSlug) { | ||
| const { docs } = await payload.find({ |
There was a problem hiding this comment.
This seems to be repeated a few time through out the code. Might be a good chance to make it a utility function
export const getTenantIdFromSlug = async (slug: string) => {
const result = await payload.find({
collection: 'tenants',
where: { slug: { equals: slug } },
limit: 1,
})
return result.docs[0]
}There was a problem hiding this comment.
Good call, I think that would be nice. I'll see about a follow up PR.
| } | ||
|
|
||
| try { | ||
| const response = await fetch( |
There was a problem hiding this comment.
Any reason we are making this as a client component and using fetch?
There was a problem hiding this comment.
Yea good thought - this is used in the TenantFieldComponent which is a client component so we need to use fetch/the REST API.
| } | ||
|
|
||
| try { | ||
| const response = await fetch(`/api/tenants/${id}?depth=0`, { credentials: 'include' }) |
There was a problem hiding this comment.
Same question about fetch
Description
See decision doc
docs/decisions/013-hardcoded-tenant-lookup.mdfor full details.Refactors tenant lookup to use a hardcoded list and use slug in the
payload-tenantcookie instead of id.Related Issues
#579
Key Changes
See decision doc
docs/decisions/013-hardcoded-tenant-lookup.md.How to test
See
docs/tenant-selector.mdfor manual test cases.Screenshots / Demo video
Demo: https://www.loom.com/share/4c083b90aedf4df983eab130db9b1783
Not going to lie: the demo is a little boring. I'm just running through the manual test cases. Might be more helpful for you to run through on your own but def feel free to just watch the loom on 2x speed instead.
Future enhancements / Questions
I'm starting to think that we shouldn't allow visiting the admin panel on subdomains and custom domains at all. We already direct users to use the root domain in production. Let's just add a redirect to the root domain for
/adminroutes.