Files
supabase/apps/studio/data/config/project-endpoint-query.ts
supabase-vercel-tedd[bot] d15ec8c93d fix(studio): use the custom domain only after activation completes (#54566)
## Problem

Studio switched project URLs to the custom domain when the Cloudflare
hostname reported `active`, which already happens at
`4_origin_setup_completed`. At that point the platform services don't
accept requests on the custom domain yet. So URLs Studio handed out,
such as the Storage explorer's **Get URL** links, failed until
activation finished.

## Solution

`useProjectApiUrl` now uses the custom domain only when the
custom-hostname status is `5_services_reconfigured`. Before that it
returns the project's own host. Every Studio surface that builds project
URLs reads this hook, so Storage **Get URL**, the Connect sheet, API
settings, and Edge Functions all follow the same rule.

## How to verify

1. Open a project with the custom domain add-on whose domain is in
`4_origin_setup_completed` (verified, not yet activated). In Storage,
click **Get URL** on a file. The copied URL uses the project's
`supabase.co` host.
2. On a project whose custom domain is in `5_services_reconfigured`
(activated), **Get URL** copies a URL on the custom domain.
3. On a project with no custom domain, **Get URL** copies a URL on the
project's `supabase.co` host, as before.
4. `pnpm exec vitest run
data/config/project-endpoint-query.platform.test.tsx` from `apps/studio`
passes. The status-4 case fails without this change.

## Checks

From `apps/studio`: `tsc --noEmit` (passed); `eslint` and the type-aware
`eslint.type-checks.config.cjs` on the changed files (passed); `vitest
run` on the new test plus `ProjectConnectionPopover.platform.test.tsx`
and `TerminalInstructions.platform.test.tsx` (passed). `oxfmt --check`
on the changed files (passed). Checked the status values against
`packages/api-types` (`/v1/projects/{ref}/custom-hostname`) and against
`CustomDomainConfig.tsx`, which treats status 4 as "ready to activate".

## Checklist

- [x] I have read the [Contributing
Guidelines](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [x] I have added tests where applicable
- [ ] I have checked that the change works in the preview deployment

## Feedback for Tedd

To ask Tedd for a change to this PR, reply in Tedd's session on
[FE-5306](https://linear.app/supabase/issue/FE-5306/studio-uses-custom-domain-before-domain-is-fully-activated).
Tedd doesn't read comments on this PR.

---------

Co-authored-by: supabase-vercel-tedd[bot] <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com>
2026-12-04 12:23:44 +00:00

55 lines
2.2 KiB
TypeScript

import { IS_PLATFORM } from 'common'
import { useProjectSettingsV2Query } from './project-settings-v2-query'
import { useCustomDomainsQuery } from '@/data/custom-domains/custom-domains-query'
import { useProjectAddonsQuery } from '@/data/subscriptions/project-addons-query'
export const useProjectApiUrl = (
{ projectRef }: { projectRef?: string },
{ enabled = true }: { enabled?: boolean } = {}
) => {
const { data } = useProjectAddonsQuery({ projectRef }, { enabled })
const hasCustomDomainsAddon = !!data?.selected_addons.find((x) => x.type === 'custom_domain')
const {
data: customDomainData,
error: customDomainsError,
isPending: isLoadingCustomDomains,
isSuccess: isSuccessCustomDomains,
isError: isErrorCustomDomains,
} = useCustomDomainsQuery({ projectRef }, { enabled })
const isCustomDomainsActive = customDomainData?.status === '5_services_reconfigured'
const customEndpoint =
hasCustomDomainsAddon && isCustomDomainsActive
? `https://${customDomainData?.customDomain?.hostname}`
: undefined
const {
data: settings,
error: projectSettingsError,
isPending: isLoadingProjectSettings,
isSuccess: isSuccessProjectSettings,
isError: isErrorProjectSettings,
} = useProjectSettingsV2Query({ projectRef }, { enabled })
const protocol = settings?.app_config?.protocol ?? 'https'
const endpoint = settings?.app_config?.endpoint
const hostEndpoint =
isSuccessProjectSettings && endpoint ? `${protocol}://${endpoint}` : undefined
const resolvedEndpoint = isCustomDomainsActive ? customEndpoint : hostEndpoint
const storageEndpoint = settings?.app_config?.storage_endpoint
? `${IS_PLATFORM ? 'https' : protocol}://${settings?.app_config?.storage_endpoint}`
: undefined
return {
data: resolvedEndpoint,
customEndpoint,
hostEndpoint,
storageEndpoint,
error: projectSettingsError || (hasCustomDomainsAddon ? customDomainsError : undefined),
isPending: isLoadingProjectSettings || (hasCustomDomainsAddon && isLoadingCustomDomains),
isSuccess: isSuccessProjectSettings && (!hasCustomDomainsAddon || isSuccessCustomDomains),
isError: isErrorProjectSettings || (hasCustomDomainsAddon && isErrorCustomDomains),
}
}