Skip to content

Commit

Permalink
fix: custom domains fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
richiemcilroy committed Feb 24, 2025
1 parent 48a82eb commit 99796be
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 21 deletions.
62 changes: 60 additions & 2 deletions apps/web/app/api/settings/workspace/domain/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,43 @@ const addDomain = async (domain: string) => {

const getRequiredConfig = async (domain: string) => {
console.log(`[Required Config] Fetching required config for domain: ${domain}`);

// First try to get the records directly
try {
const recordsResponse = await fetch(
`https://api.vercel.com/v4/domains/${domain.toLowerCase()}/records?limit=10&teamId=${process.env.VERCEL_TEAM_ID}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${process.env.VERCEL_AUTH_TOKEN}`,
"Content-Type": "application/json",
},
cache: "no-store"
},
).then((res) => res.json());

console.log(`[Required Config] Records Response:`, recordsResponse);

if (recordsResponse.records) {
const aRecord = recordsResponse.records.find((record: any) =>
record.type === 'A' && record.name === ''
);

if (aRecord) {
return {
configuredBy: 'vercel',
aValues: [aRecord.value],
serviceType: 'vercel',
};
}
}
} catch (error) {
console.error('[Required Config] Error fetching records:', error);
}

// Fallback to the old config endpoint
const response = await fetch(
`https://vercel.com/api/v6/domains/${domain.toLowerCase()}/config?strict=true&teamId=${process.env.VERCEL_TEAM_ID}`,
`https://api.vercel.com/v6/domains/${domain.toLowerCase()}/config?teamId=${process.env.VERCEL_TEAM_ID}`,
{
method: "GET",
headers: {
Expand All @@ -89,7 +124,30 @@ const getRequiredConfig = async (domain: string) => {
cache: "no-store"
},
).then((res) => res.json());
console.log(`[Required Config] Response:`, response);
console.log(`[Required Config] Fallback Response:`, response);

// If we still don't have the A record, try the project domains endpoint
if (!response.aValues || response.aValues.length === 0) {
const projectResponse = await fetch(
`https://api.vercel.com/v9/projects/${process.env.VERCEL_PROJECT_ID}/domains?teamId=${process.env.VERCEL_TEAM_ID}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${process.env.VERCEL_AUTH_TOKEN}`,
"Content-Type": "application/json",
},
cache: "no-store"
},
).then((res) => res.json());

if (projectResponse.domains) {
const projectDomain = projectResponse.domains.find((d: any) => d.name === domain);
if (projectDomain?.apexValue) {
response.aValues = [projectDomain.apexValue];
}
}
}

return response;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,11 +483,26 @@ export function CustomDomain() {
(value, index) => (
<div
key={index}
className="flex items-center gap-2"
className={
value === domainConfig.requiredAValue
? "flex items-center gap-2 text-green-600"
: "flex items-center gap-2 text-red-600"
}
>
<code className="bg-gray-50 px-2 py-1 rounded">
<code
className={
value === domainConfig.requiredAValue
? "px-2 py-1 rounded bg-green-50"
: "px-2 py-1 rounded bg-red-50"
}
>
{value}
</code>
{value === domainConfig.requiredAValue && (
<span className="text-xs text-green-600">
(Correct)
</span>
)}
</div>
)
)}
Expand All @@ -500,24 +515,29 @@ export function CustomDomain() {
</dt>
<dd className="flex items-center gap-2">
<div className="flex items-center gap-1.5 bg-gray-50 px-2 py-1 rounded flex-1 min-w-0">
<code className="text-sm text-green-600">
{domainConfig.requiredAValue}
<code className="text-sm text-gray-900">
{domainConfig.requiredAValue || "Loading..."}
</code>
<button
type="button"
onClick={() =>
domainConfig.requiredAValue &&
handleCopy(domainConfig.requiredAValue, "value")
}
className="p-1 hover:bg-gray-100 rounded-md transition-colors shrink-0"
title="Copy to clipboard"
>
{copiedField === "value" ? (
<Check className="h-4 w-4 text-green-500" />
) : (
<Copy className="h-4 w-4 text-gray-500" />
)}
</button>
{domainConfig.requiredAValue && (
<button
type="button"
onClick={() =>
domainConfig.requiredAValue &&
handleCopy(
domainConfig.requiredAValue,
"value"
)
}
className="p-1 hover:bg-gray-100 rounded-md transition-colors shrink-0"
title="Copy to clipboard"
>
{copiedField === "value" ? (
<Check className="h-4 w-4 text-green-500" />
) : (
<Copy className="h-4 w-4 text-gray-500" />
)}
</button>
)}
</div>
</dd>
</div>
Expand Down

1 comment on commit 99796be

@vercel
Copy link

@vercel vercel bot commented on 99796be Feb 24, 2025

Choose a reason for hiding this comment

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

Please sign in to comment.