Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

code improvements

Pedro Ladeira9888d701f053
diff --git a/apps/web/app/api/domains/[domain]/domain-connect/apply/route.ts b/apps/web/app/api/domains/[domain]/domain-connect/apply/route.ts
index 55c0a82ffa..2b973f48f7 100644
--- a/apps/web/app/api/domains/[domain]/domain-connect/apply/route.ts
+++ b/apps/web/app/api/domains/[domain]/domain-connect/apply/route.ts
@@ -9,6 +9,7 @@ import {
DEFAULT_DC_SERVICE_APEX,
DEFAULT_DC_SERVICE_SUBDOMAIN,
DOMAIN_CONNECT_KEY_HOST,
+ DOMAIN_CONNECT_PROVIDER_ID,
} from "@/lib/domain-connect/constants";
import { discoverDomainConnect } from "@/lib/domain-connect/discover";
import { buildSignedApplyUrl } from "@/lib/domain-connect/sign-apply-url";
@@ -97,39 +98,62 @@ export const POST = withWorkspace(
const queryParams: Record<string, string> = {
domain: apex,
- groupId: "subdomain",
+ groupId: isApex ? "apex" : "subdomain",
redirect_uri: redirectUri,
};
- if (isApex) {
- queryParams.groupId = "apex";
- } else {
- queryParams.groupId = "subdomain";
- queryParams.cnameHost = (subdomain ?? "www").toLowerCase();
+ // links-subdomain v1 scopes all records under `host`, so its TXT lands at
+ // `_vercel.<subdomain>`. v2 takes `cnameHost` and writes the TXT at the apex.
+ // Providers deploy template versions independently.
+ let subdomainTemplateVersion = 1;
+ if (!isApex && discovery.urlAPI) {
+ try {
+ const res = await fetch(
+ `${discovery.urlAPI}/v2/domainTemplates/providers/${DOMAIN_CONNECT_PROVIDER_ID}/services/${serviceId}`,
+ {
+ headers: { accept: "application/json" },
+ redirect: "manual",
+ signal: AbortSignal.timeout(3000),
+ },
+ );
+ if (res.ok) {
+ const { version } = (await res.json()) as { version?: unknown };
+ if (typeof version === "number") subdomainTemplateVersion = version;
+ }
+ } catch {}
+ }
+
+ if (subdomain && subdomainTemplateVersion >= 2) {
+ queryParams.cnameHost = subdomain;
+ } else if (subdomain) {
+ queryParams.host = subdomain;
}
const txtVerification = domainJson.verification?.find(
(x: { type: string }) => x.type === "TXT",
);
- if (txtVerification) {
- const txtValue = txtVerification.value?.trim() ?? "";
- if (txtValue && !isApex) {
+ const txtValue = txtVerification?.value?.trim();
+ if (txtValue && isApex) {
+ const txtHostFqdn: string = txtVerification.domain?.toLowerCase() ?? "";
+ const apexSuffix = `.${apex}`;
+ const txtHost = txtHostFqdn.endsWith(apexSuffix)
+ ? txtHostFqdn.slice(0, -apexSuffix.length)
+ : txtHostFqdn === apex
+ ? "@"
+ : txtHostFqdn;
+ if (txtHost) {
queryParams.groupId = queryParams.groupId + ",verification";
+ queryParams.txtHost = txtHost;
queryParams.txtValue = txtValue;
- } else if (txtValue) {
- const txtHostFqdn: string = txtVerification.domain?.toLowerCase() ?? "";
- const apexSuffix = `.${apex}`;
- const txtHost = txtHostFqdn.endsWith(apexSuffix)
- ? txtHostFqdn.slice(0, -apexSuffix.length)
- : txtHostFqdn === apex
- ? "@"
- : txtHostFqdn;
- if (txtHost) {
- queryParams.groupId = queryParams.groupId + ",verification";
- queryParams.txtHost = txtHost;
- queryParams.txtValue = txtValue;
- }
}
+ } else if (
+ txtValue &&
+ subdomainTemplateVersion >= 2 &&
+ txtVerification.domain?.toLowerCase().replace(/\.$/, "") ===
+ `_vercel.${apex}`
+ ) {
+ queryParams.groupId = queryParams.groupId + ",verification";
+ queryParams.txtValue = txtValue;
}
const applyUrl = buildSignedApplyUrl({
diff --git a/apps/web/lib/domain-connect/discover.ts b/apps/web/lib/domain-connect/discover.ts
index dee52ca797..1b74dcd821 100644
--- a/apps/web/lib/domain-connect/discover.ts
+++ b/apps/web/lib/domain-connect/discover.ts
@@ -8,6 +8,7 @@ import type {
type DomainConnectSettings = {
providerId?: string;
urlSyncUX?: string;
+ urlAPI?: string;
};
function providerKindFromId(
@@ -27,6 +28,13 @@ const ALLOWED_SETTINGS_HOST_SUFFIXES = [
".cloudflare.com",
];
+function isAllowedSettingsHost(host: string): boolean {
+ return ALLOWED_SETTINGS_HOST_SUFFIXES.some((suffix) => {
+ const hostnameSuffix = suffix.replace(/^\./, "");
+ return host === hostnameSuffix || host.endsWith(`.${hostnameSuffix}`);
+ });
+}
+
function settingsBaseFromTxtRecords(records: string[][]): string | null {
const candidates = records
.map((chunks) =>
@@ -59,10 +67,7 @@ function settingsBaseFromTxtRecords(records: string[][]): string | null {
) {
return false;
}
- return ALLOWED_SETTINGS_HOST_SUFFIXES.some((suffix) => {
- const hostnameSuffix = suffix.replace(/^\./, "");
- return host === hostnameSuffix || host.endsWith(`.${hostnameSuffix}`);
- });
+ return isAllowedSettingsHost(host);
});
return candidates[0] ?? null;
@@ -124,7 +129,18 @@ export async function discoverDomainConnect(
const providerKind = providerKindFromId(dnsProviderId);
if (!providerKind) return null;
- return { providerKind, dnsProviderId, urlSyncUX };
+ let urlAPI: string | undefined;
+ try {
+ const parsedAPI = new URL(json.urlAPI ?? "");
+ if (
+ parsedAPI.protocol === "https:" &&
+ isAllowedSettingsHost(parsedAPI.hostname)
+ ) {
+ urlAPI = `${parsedAPI.origin}${parsedAPI.pathname}`.replace(/\/$/, "");
+ }
+ } catch {}
+
+ return { providerKind, dnsProviderId, urlSyncUX, urlAPI };
}
/**
diff --git a/apps/web/lib/domain-connect/types.ts b/apps/web/lib/domain-connect/types.ts
index 1a6ed2ff7f..97b39539e2 100644
--- a/apps/web/lib/domain-connect/types.ts
+++ b/apps/web/lib/domain-connect/types.ts
@@ -4,4 +4,5 @@ export type DomainConnectDiscovery = {
providerKind: DomainConnectProviderKind;
dnsProviderId: string;
urlSyncUX: string;
+ urlAPI?: string;
};