Skip to content

Commit

Permalink
fix: hit s3 in loadWithUrl to make everything faster
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi committed Jan 23, 2025
1 parent 9d7809f commit 294adc1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
35 changes: 6 additions & 29 deletions packages/fern-docs/bundle/src/server/DocsLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type { AuthEdgeConfig } from "@fern-docs/auth";
import { ApiDefinitionLoader } from "@fern-docs/cache";
import { getAuthEdgeConfig } from "@fern-docs/edge-config";
import { getAuthState, type AuthState } from "./auth/getAuthState";
import { loadDocsDefinitionFromS3 } from "./loadDocsDefinitionFromS3";
import { loadWithUrl } from "./loadWithUrl";
import { pruneWithAuthState } from "./withRbac";

Expand Down Expand Up @@ -119,38 +118,16 @@ export class DocsLoader {
.withEnvironment(process.env.NEXT_PUBLIC_FDR_ORIGIN)
.load();
}

private getDocsDefinitionUrl() {
return (
process.env.NEXT_PUBLIC_DOCS_DEFINITION_S3_URL ??
"https://docs-definitions.buildwithfern.com"
);
}


private async loadDocs(): Promise<
DocsV2Read.LoadDocsForUrlResponse | undefined
> {
if (!this.#loadForDocsUrlResponse) {
try {
const response = await loadDocsDefinitionFromS3({
domain: this.domain,
docsDefinitionUrl: this.getDocsDefinitionUrl(),
});
if (response == null) {
throw new Error(
`Failed to load docs definition for domain: ${this.domain}`
);
}
this.#loadForDocsUrlResponse = response;
} catch (error) {
// Not served by cloudfront, fetch from Redis and then RDS
console.error("Failed to load docs definition from S3:", error);
const response = await loadWithUrl(this.domain);
if (response.ok) {
this.#loadForDocsUrlResponse = response.body;
} else {
this.#error = response.error;
}
const response = await loadWithUrl(this.domain);
if (response.ok) {
this.#loadForDocsUrlResponse = response.body;
} else {
this.#error = response.error;
}
}
return this.#loadForDocsUrlResponse;
Expand Down
30 changes: 28 additions & 2 deletions packages/fern-docs/bundle/src/server/loadWithUrl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { APIResponse, FdrAPI } from "@fern-api/fdr-sdk/client/types";
import { provideRegistryService } from "@fern-docs/ui";
import { withoutStaging } from "@fern-docs/utils";
import { loadDocsDefinitionFromS3 } from "./loadDocsDefinitionFromS3";

export type LoadWithUrlResponse = APIResponse<
FdrAPI.docs.v2.read.LoadDocsForUrlResponse,
Expand All @@ -11,8 +12,33 @@ export type LoadWithUrlResponse = APIResponse<
* - If the token is a WorkOS token, we need to use the getPrivateDocsForUrl endpoint.
* - Otherwise, we can use the getDocsForUrl endpoint (including custom auth).
*/
export function loadWithUrl(url: string): Promise<LoadWithUrlResponse> {
export async function loadWithUrl(
domain: string
): Promise<LoadWithUrlResponse> {
const domainWithoutStaging = withoutStaging(domain);

let response;
try {
response = await loadDocsDefinitionFromS3({
domain: domainWithoutStaging,
docsDefinitionUrl: getDocsDefinitionUrl(),
});
if (response != null) {
return response;
}
} catch (error) {
console.error("Failed to load docs definition:", error);
response = null;
}

return provideRegistryService().docs.v2.read.getDocsForUrl({
url: FdrAPI.Url(withoutStaging(url)),
url: FdrAPI.Url(domainWithoutStaging),
});
}

function getDocsDefinitionUrl() {
return (
process.env.NEXT_PUBLIC_DOCS_DEFINITION_S3_URL ??
"https://docs-definitions.buildwithfern.com"
);
}

0 comments on commit 294adc1

Please sign in to comment.