Skip to content

Commit b692c26

Browse files
authored
chore: clean up preview logic (#3288)
1 parent 9085a01 commit b692c26

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

servers/fdr/src/__test__/local/services/docs.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,9 @@ it("preview domain truncation - valid preview link", async () => {
294294
// check link is valid
295295
expect(startDocsPreviewResponse.previewUrl).toBeDefined();
296296
const previewUrl = new URL(startDocsPreviewResponse.previewUrl);
297-
expect(previewUrl.hostname.length).toBeLessThanOrEqual(63);
297+
const subdomains = previewUrl.hostname.split(".");
298+
const mainSubdomain = subdomains[0];
299+
expect(mainSubdomain.length).toBeLessThanOrEqual(63);
298300
expect(previewUrl.hostname).toMatch(
299301
/^short-preview-[a-f0-9-]+\.docs\.buildwithfern\.com$/
300302
);
@@ -312,7 +314,7 @@ it("preview domain truncation - requires truncation", async () => {
312314

313315
const startDocsPreviewResponse =
314316
await fdr.docs.v2.write.startDocsPreviewRegister({
315-
orgId: FdrAPI.OrgId("medium-org-name"),
317+
orgId: FdrAPI.OrgId("medium-org-name-for-truncation"),
316318
filepaths: [
317319
DocsV1Write.FilePath("logo.png"),
318320
DocsV1Write.FilePath("guides/guide.mdx"),
@@ -329,9 +331,11 @@ it("preview domain truncation - requires truncation", async () => {
329331
expect(startDocsPreviewResponse.ok).toBe(true);
330332
if (startDocsPreviewResponse.ok) {
331333
const previewUrl = new URL(startDocsPreviewResponse.body.previewUrl);
332-
expect(previewUrl.hostname.length).toBeLessThanOrEqual(63);
334+
const subdomains = previewUrl.hostname.split(".");
335+
const mainSubdomain = subdomains[0];
336+
expect(mainSubdomain.length).toBeLessThanOrEqual(63);
333337
expect(previewUrl.hostname).toMatch(
334-
/^medium-org-name-preview-[a-f0-9-]{8,}\.docs\.buildwithfern\.com$/
338+
/^medium-org-name-for-truncation-preview-[a-f0-9-]{23,}\.docs\.buildwithfern\.com$/
335339
);
336340
}
337341
});

servers/fdr/src/controllers/docs/v2/getDocsWriteV2Service.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function truncateDomainName({
5252
docsRegistrationId: string;
5353
domainSuffix: string;
5454
}): string {
55-
const subdomainLimit = 63;
55+
const subdomainLimit = 62;
5656
const fullDomain = `${orgId}-preview-${docsRegistrationId}.${domainSuffix}`;
5757

5858
if (fullDomain.length <= subdomainLimit) {
@@ -61,7 +61,7 @@ function truncateDomainName({
6161

6262
const prefix = `${orgId}-preview-`;
6363
const suffix = `.${domainSuffix}`;
64-
const availableSpace = subdomainLimit - prefix.length - suffix.length;
64+
const availableSpace = subdomainLimit - prefix.length;
6565

6666
// keep 8 characters of obscurity for security
6767
const minRegistrationIdLength = 8;
@@ -72,7 +72,8 @@ function truncateDomainName({
7272
}
7373

7474
const truncatedRegistrationId = docsRegistrationId.slice(0, availableSpace);
75-
return `${prefix}${truncatedRegistrationId}${suffix}`;
75+
const cleanRegistrationId = truncatedRegistrationId.replace(/-+$/, "");
76+
return `${prefix}${cleanRegistrationId}${suffix}`;
7677
}
7778

7879
function validateAndParseFernDomainUrl({

0 commit comments

Comments
 (0)