From 2863fe0dc17e2bf20dc8cb4137f72480024cc73a Mon Sep 17 00:00:00 2001 From: Taran Vohra Date: Mon, 16 Jun 2025 09:59:55 +0530 Subject: [PATCH] Use `resolvePublishedContentByUrl` instead of the deprecated resolution endpoint (#3310) --- packages/gitbook-v2/src/lib/data/lookup.ts | 97 +++++----------------- packages/gitbook-v2/src/middleware.ts | 20 +---- 2 files changed, 24 insertions(+), 93 deletions(-) diff --git a/packages/gitbook-v2/src/lib/data/lookup.ts b/packages/gitbook-v2/src/lib/data/lookup.ts index 4c999bd7a..be8c21f1d 100644 --- a/packages/gitbook-v2/src/lib/data/lookup.ts +++ b/packages/gitbook-v2/src/lib/data/lookup.ts @@ -1,7 +1,7 @@ import { race, tryCatch } from '@/lib/async'; import { joinPath, joinPathWithBaseURL } from '@/lib/paths'; import { trace } from '@/lib/tracing'; -import type { GitBookAPI, PublishedSiteContentLookup, SiteVisitorPayload } from '@gitbook/api'; +import type { PublishedSiteContentLookup, SiteVisitorPayload } from '@gitbook/api'; import { apiClient } from './api'; import { getExposableError } from './errors'; import type { DataFetcherResponse } from './types'; @@ -18,85 +18,32 @@ interface LookupPublishedContentByUrlInput { * Lookup a content by its URL using the GitBook resolvePublishedContentByUrl API endpoint. * To optimize caching, we try multiple lookup alternatives and return the first one that matches. */ -export async function resolvePublishedContentByUrl(input: LookupPublishedContentByUrlInput) { - return lookupPublishedContentByUrl({ - url: input.url, - fetchLookupAPIResult: ({ url, signal }) => { - const api = apiClient({ apiToken: input.apiToken }); - return trace( - { - operation: 'resolvePublishedContentByUrl', - name: url, - }, - () => - tryCatch( - api.urls.resolvePublishedContentByUrl( - { - url, - ...(input.visitorPayload ? { visitor: input.visitorPayload } : {}), - redirectOnError: input.redirectOnError, - }, - { signal } - ) - ) - ); - }, - }); -} - -/** - * Lookup a content by its URL using the GitBook getPublishedContentByUrl API endpoint. - * To optimize caching, we try multiple lookup alternatives and return the first one that matches. - * - * @deprecated use resolvePublishedContentByUrl. - * - */ -export async function getPublishedContentByURL(input: LookupPublishedContentByUrlInput) { - return lookupPublishedContentByUrl({ - url: input.url, - fetchLookupAPIResult: ({ url, signal }) => { - const api = apiClient({ apiToken: input.apiToken }); - return trace( - { - operation: 'getPublishedContentByURL', - name: url, - }, - () => - tryCatch( - api.urls.getPublishedContentByUrl( - { - url, - visitorAuthToken: input.visitorPayload.jwtToken ?? undefined, - redirectOnError: input.redirectOnError, - // @ts-expect-error - cacheVersion is not a real query param - cacheVersion: 'v2', - }, - { signal } - ) - ) - ); - }, - }); -} - -type TryCatch = ReturnType>; - -async function lookupPublishedContentByUrl(input: { - url: string; - fetchLookupAPIResult: (args: { - url: string; - signal: AbortSignal; - }) => TryCatch>>; -}): Promise> { +export async function lookupPublishedContentByUrl( + input: LookupPublishedContentByUrlInput +): Promise> { const lookupURL = new URL(input.url); const url = stripURLSearch(lookupURL); const lookup = getURLLookupAlternatives(url); const result = await race(lookup.urls, async (alternative, { signal }) => { - const callResult = await input.fetchLookupAPIResult({ - url: alternative.url, - signal, - }); + const api = apiClient({ apiToken: input.apiToken }); + const callResult = await trace( + { + operation: 'resolvePublishedContentByUrl', + name: alternative.url, + }, + () => + tryCatch( + api.urls.resolvePublishedContentByUrl( + { + url: alternative.url, + ...(input.visitorPayload ? { visitor: input.visitorPayload } : {}), + redirectOnError: input.redirectOnError, + }, + { signal } + ) + ) + ); if (callResult.error) { if (alternative.primary) { diff --git a/packages/gitbook-v2/src/middleware.ts b/packages/gitbook-v2/src/middleware.ts index 0d57ce27e..d413d6a43 100644 --- a/packages/gitbook-v2/src/middleware.ts +++ b/packages/gitbook-v2/src/middleware.ts @@ -16,10 +16,9 @@ import { import { serveResizedImage } from '@/routes/image'; import { DataFetcherError, - getPublishedContentByURL, getVisitorAuthBasePath, + lookupPublishedContentByUrl, normalizeURL, - resolvePublishedContentByUrl, throwIfDataError, } from '@v2/lib/data'; import { isGitBookAssetsHostURL, isGitBookHostURL } from '@v2/lib/env'; @@ -34,18 +33,6 @@ export const config = { type URLWithMode = { url: URL; mode: 'url' | 'url-host' }; -/** - * Temporary list of hosts to test adaptive content using the new resolution API. - */ -const ADAPTIVE_CONTENT_HOSTS = [ - 'docs.gitbook.com', - 'paypal.gitbook.com', - 'adaptive-docs.gitbook-staging.com', - 'enriched-content-playground.gitbook-staging.io', - 'docs.testgitbook.com', - 'launchdarkly-site.gitbook.education', -]; - export async function middleware(request: NextRequest) { try { const requestURL = new URL(request.url); @@ -104,11 +91,8 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) { }); const withAPIToken = async (apiToken: string | null) => { - const resolve = ADAPTIVE_CONTENT_HOSTS.includes(siteRequestURL.hostname) - ? resolvePublishedContentByUrl - : getPublishedContentByURL; const siteURLData = await throwIfDataError( - resolve({ + lookupPublishedContentByUrl({ url: siteRequestURL.toString(), visitorPayload: { jwtToken: visitorToken?.token ?? undefined,