Skip to content

Tenant lookup refactor - #897

Merged
stevekuznetsov merged 21 commits into
mainfrom
tenant-lookup-refactor
Jan 28, 2026
Merged

stevekuznetsov merged 21 commits into
mainfrom
tenant-lookup-refactor

Conversation

@busbyk

@busbyk busbyk commented Jan 27, 2026 •

Copy link
Copy Markdown
Collaborator

Description

See decision doc docs/decisions/013-hardcoded-tenant-lookup.md for full details.

Refactors tenant lookup to use a hardcoded list and use slug in the payload-tenant cookie 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.md for 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 /admin routes.

busbyk and others added 15 commits January 22, 2026 15:23
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]>
@github-actions

Copy link
Copy Markdown
Contributor

Comment thread src/utilities/useTenantLookup.ts Resolved
@busbyk
busbyk marked this pull request as ready for review January 27, 2026 23:58
@busbyk busbyk self-assigned this Jan 28, 2026
@stevekuznetsov
stevekuznetsov added this pull request to the merge queue Jan 28, 2026
Merged via the queue into main with commit 0f74777 Jan 28, 2026
8 checks passed
@stevekuznetsov
stevekuznetsov deleted the tenant-lookup-refactor branch January 28, 2026 00:52
rchlfryn added a commit that referenced this pull request Jan 28, 2026

@rchlfryn rchlfryn left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice win!

Comment thread docs/tenant-selector.md

---

### Tenant-Required Collection (e.g., Pages, Posts)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⛏️ ⛏️ ⛏️

Suggested change
### Tenant-Required Collection (e.g., Pages, Posts)
### Tenant-Required Collection (e.g. Pages, Posts)

Comment thread docs/tenant-selector.md
- [ ] 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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to add provider manage here or make note of it?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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',

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this change!


if (!tenant) {
if (tenantSlug) {
const { docs } = await payload.find({

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, I think that would be nice. I'll see about a follow up PR.

}

try {
const response = await fetch(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason we are making this as a client component and using fetch?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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' })

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question about fetch

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥳

Comment thread docs/tenant-selector.md

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏🏼

rchlfryn added a commit that referenced this pull request Feb 7, 2026
This reverts commit 0f74777, reversing
changes made to 777ef9a.
rchlfryn added a commit that referenced this pull request Feb 7, 2026
This reverts commit 0f74777, reversing
changes made to 777ef9a.
github-merge-queue Bot pushed a commit that referenced this pull request Feb 7, 2026
busbyk added a commit that referenced this pull request Feb 16, 2026

This branch was previously deployed

1 inactive deployment
Preview — 97063934 Deployed Jan 28, 2026 by busbyk via deploy #1367
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants