Compare commits

...

3 Commits

Author SHA1 Message Date
Peter White 5f2ef86594 Add browser tests for searching the API directly with a search pass 2026-11-15 16:06:59 +00:00
Peter White 90d18d793d changeset 2026-11-15 15:22:36 +00:00
Peter White 3bc18f1d32 Search the site search API from the browser with a pass from its own site route 2026-11-15 15:22:36 +00:00
17 changed files with 1264 additions and 178 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"gitbook": patch
---
Send site searches straight to the search API when the site hands out a search pass, and use the site's search route when it does not or when the API refuses a search.
+250 -1
View File
@@ -1,4 +1,4 @@
import { type Page, expect } from '@playwright/test';
import { type Page, type Request, expect } from '@playwright/test';
import jwt from 'jsonwebtoken';
import {
@@ -387,6 +387,250 @@ const searchTestCases: Test[] = [
},
];
const SEARCH_API_URL = /\/v1\/orgs\/[^/]+\/sites\/[^/]+\/search$/;
const SEARCH_PASS_RECORD = {
type: 'record',
id: 'search-pass-record',
title: 'Answered by the search API',
description: 'A search made with a search pass',
url: 'https://example.com/search-pass',
score: 1,
};
/**
* Answer the page's search pass requests as the API does when passes are switched on.
*/
async function giveSearchPasses(page: Page, lifetimesMs: number[] = []) {
let passes = 0;
await page.route('**/~gitbook/search-pass', async (route) => {
passes += 1;
await route.fulfill({
json: {
token: `test-search-pass-${passes}`,
expiresIn: lifetimesMs.shift() ?? 300_000,
},
});
});
}
/**
* Answer each search API call with the next status, and with a record when it succeeds.
*/
async function answerSearchAPI(page: Page, statuses: number[] = []) {
const calls: Request[] = [];
await page.route(SEARCH_API_URL, async (route) => {
calls.push(route.request());
const status = statuses.shift() ?? 200;
await route.fulfill({
status,
headers: { 'access-control-allow-origin': '*' },
json: status === 200 ? { items: [SEARCH_PASS_RECORD] } : { error: { code: status } },
});
});
return calls;
}
/**
* Collect the searches the page sends to the site's search route.
*/
function trackRouteSearches(page: Page) {
const searches: Request[] = [];
page.on('request', (request) => {
if (request.method() === 'POST' && request.url().endsWith('/~gitbook/search')) {
searches.push(request);
}
});
return searches;
}
/**
* Open search and wait for the page to receive its search pass.
*/
async function openSearchWithPass(page: Page) {
const passReply = page.waitForResponse('**/~gitbook/search-pass');
await page.getByTestId('search-input').focus();
await passReply;
}
async function typeSearch(page: Page, text: string) {
const searchInput = page.getByTestId('search-input');
await searchInput.focus();
await searchInput.pressSequentially(text);
}
const searchPassTestCases: Test[] = [
{
name: 'No pass: searches go through the site route',
url: '',
screenshot: false,
run: async (page) => {
const passReply = page.waitForResponse('**/~gitbook/search-pass');
const apiCalls: Request[] = [];
page.on('request', (request) => {
if (SEARCH_API_URL.test(request.url())) {
apiCalls.push(request);
}
});
const routeSearches = trackRouteSearches(page);
await waitForCookiesDialog(page);
await typeSearch(page, 'gitbook');
expect((await passReply).status()).toBe(204);
await expect
.poll(() => routeSearches.length, { timeout: 30_000 })
.toBeGreaterThanOrEqual(1);
expect((await routeSearches[0]?.response())?.status()).toBe(200);
expect(apiCalls).toHaveLength(0);
},
},
{
name: 'With a pass: searches go straight to the API',
url: '',
screenshot: false,
run: async (page) => {
await giveSearchPasses(page);
const apiCalls = await answerSearchAPI(page);
const routeSearches = trackRouteSearches(page);
await waitForCookiesDialog(page);
await openSearchWithPass(page);
await typeSearch(page, 'gitbook');
await expect(
page
.getByTestId('search-record-result')
.filter({ hasText: SEARCH_PASS_RECORD.title })
).toBeVisible({ timeout: 30_000 });
expect((await apiCalls[0]?.allHeaders())?.authorization).toBe(
'Bearer test-search-pass-1'
);
expect(routeSearches).toHaveLength(0);
},
},
{
name: 'Expired pass: the search uses the route, then the next pass searches the API',
url: '',
screenshot: false,
run: async (page) => {
await giveSearchPasses(page, [1_000]);
const apiCalls = await answerSearchAPI(page);
const routeSearches = trackRouteSearches(page);
await waitForCookiesDialog(page);
await openSearchWithPass(page);
await typeSearch(page, 'gitbook');
await expect.poll(() => routeSearches.length, { timeout: 30_000 }).toBe(1);
expect(routeSearches[0]?.headers()['x-gitbook-search-fallback']).toBe('expired');
expect(apiCalls).toHaveLength(0);
await typeSearch(page, ' editor');
await expect(
page
.getByTestId('search-record-result')
.filter({ hasText: SEARCH_PASS_RECORD.title })
).toBeVisible({ timeout: 30_000 });
expect((await apiCalls[0]?.allHeaders())?.authorization).toBe(
'Bearer test-search-pass-2'
);
},
},
{
name: 'Refused pass: the page stays on the route',
url: '',
screenshot: false,
run: async (page) => {
await giveSearchPasses(page);
const apiCalls = await answerSearchAPI(page, [403]);
const routeSearches = trackRouteSearches(page);
await waitForCookiesDialog(page);
await openSearchWithPass(page);
await typeSearch(page, 'gitbook');
await expect.poll(() => routeSearches.length, { timeout: 30_000 }).toBe(1);
await typeSearch(page, ' editor');
await expect.poll(() => routeSearches.length, { timeout: 30_000 }).toBe(2);
expect(
routeSearches.map((request) => request.headers()['x-gitbook-search-fallback'])
).toEqual(['403', '403']);
expect(apiCalls).toHaveLength(1);
expect((await routeSearches.at(-1)?.response())?.status()).toBe(200);
},
},
{
name: 'Cancelled search: a late API answer never replaces the newer query',
url: '',
screenshot: false,
run: async (page) => {
await giveSearchPasses(page);
const staleTitle = 'Late answer for an older query';
await page.route(SEARCH_API_URL, async (route) => {
const { query } = route.request().postDataJSON() as { query: string };
const stale = query === 'gitbook';
if (stale) {
await page.waitForTimeout(3_000);
}
await route
.fulfill({
headers: { 'access-control-allow-origin': '*' },
json: {
items: [
stale
? { ...SEARCH_PASS_RECORD, title: staleTitle }
: SEARCH_PASS_RECORD,
],
},
})
.catch(() => undefined);
});
const routeSearches = trackRouteSearches(page);
await waitForCookiesDialog(page);
const firstSearch = page.waitForRequest(
(request) =>
SEARCH_API_URL.test(request.url()) &&
(request.postDataJSON() as { query: string }).query === 'gitbook'
);
await openSearchWithPass(page);
await typeSearch(page, 'gitbook');
await firstSearch;
await typeSearch(page, ' editor');
await expect(
page
.getByTestId('search-record-result')
.filter({ hasText: SEARCH_PASS_RECORD.title })
).toBeVisible({ timeout: 30_000 });
await page.waitForTimeout(4_000);
await expect(page.getByText(staleTitle)).toHaveCount(0);
expect(routeSearches).toHaveLength(0);
},
},
{
name: 'Embedded search goes straight to the API',
url: '~gitbook/embed/search',
screenshot: false,
run: async (page) => {
await giveSearchPasses(page);
await answerSearchAPI(page);
const routeSearches = trackRouteSearches(page);
// The embedded page keeps <html> hidden, so wait for hydration without visibility.
await page.locator('html.hydrated').waitFor({ state: 'attached' });
// The embedded search opens on the first key; a one-letter query is never sent.
const passReply = page.waitForResponse('**/~gitbook/search-pass');
await typeSearch(page, 'g');
await passReply;
await typeSearch(page, 'itbook');
await expect(
page
.getByTestId('search-record-result')
.filter({ hasText: SEARCH_PASS_RECORD.title })
).toBeVisible({ timeout: 30_000 });
expect(routeSearches).toHaveLength(0);
},
},
];
const testCases: TestsCase[] = [
{
name: 'GitBook Site (Single Variant)',
@@ -1177,6 +1421,11 @@ const testCases: TestsCase[] = [
contentBaseURL: 'https://gitbook.gitbook.io/test-gitbook-open/',
tests: searchTestCases,
},
{
name: 'Search pass',
contentBaseURL: 'https://gitbook.gitbook.io/test-gitbook-open/',
tests: searchPassTestCases,
},
{
name: 'Content tests',
contentBaseURL: 'https://gitbook.gitbook.io/test-gitbook-open/',
@@ -1,16 +1,17 @@
import { type NextRequest, NextResponse } from 'next/server';
import { orderSearchResultGroups } from './orderSearchResults';
import type {
OrderedComputedResult,
SearchSiteContentRequest,
} from '@/components/Search/search-types';
import { SEARCH_FALLBACK_HEADER } from '@/components/Search/search-pass-store';
import type { SearchSiteContentRequest } from '@/components/Search/search-types';
import { throwIfDataError } from '@/lib/data';
import { getLogger } from '@/lib/logger';
import { getSiteURLDataFromMiddleware } from '@/lib/middleware';
import { transformSitePageResult } from '@/lib/search';
import { computeSearchResults } from '@/lib/search-results';
import { getServerActionBaseContext } from '@/lib/server-actions';
import { findSiteSpaceBy, getLinkerForSiteSpace } from '@/lib/sites';
const SEARCH_FALLBACK_REASON = /^(\d{3}|expired|pass_request|network|invalid|unknown_space)$/;
export async function POST(request: NextRequest) {
const { asEmbeddable, query, scope } = (await request.json()) as SearchSiteContentRequest;
const [context, siteURLData] = await Promise.all([
@@ -18,6 +19,15 @@ export async function POST(request: NextRequest) {
getSiteURLDataFromMiddleware(),
]);
const fallback = request.headers.get(SEARCH_FALLBACK_HEADER);
if (fallback && SEARCH_FALLBACK_REASON.test(fallback)) {
getLogger()
.subLogger('search')
.info(
`Search pass fallback to the route (reason=${fallback}, organization=${siteURLData.organization}, site=${siteURLData.site})`
);
}
if (query.length <= 1) {
return NextResponse.json([]);
}
@@ -56,49 +66,24 @@ export async function POST(request: NextRequest) {
? getLinkerForSiteSpace(context.linker, currentSiteSpace.siteSpace, revision.pages)
: context.linker;
const results = orderSearchResultGroups<OrderedComputedResult>(
searchResults.map((resultItem) => {
if (resultItem.type === 'record') {
const result: OrderedComputedResult = {
type: 'record',
id: resultItem.id,
title: resultItem.title,
description: resultItem.description,
href: resultItem.url,
score: resultItem.score,
};
const results = computeSearchResults(searchResults, (spaceItem, pageItem) => {
const isCurrentRevisionSpace = Boolean(revision && spaceItem.id === siteURLData.space);
const found =
isCurrentRevisionSpace && currentSiteSpace
? currentSiteSpace
: findSiteSpaceBy(structure, (siteSpace) => siteSpace.space.id === spaceItem.id);
return { type: 'context' as const, results: [result] };
}
const isCurrentRevisionSpace = Boolean(revision && resultItem.id === siteURLData.space);
const found =
isCurrentRevisionSpace && currentSiteSpace
? currentSiteSpace
: findSiteSpaceBy(
structure,
(siteSpace) => siteSpace.space.id === resultItem.id
);
return {
type: 'pages' as const,
results: resultItem.pages.flatMap((pageItem) => {
const result = transformSitePageResult({
asEmbeddable: Boolean(asEmbeddable),
linker: isCurrentRevisionSpace ? revisionLinker : context.linker,
pageItem,
spaceItem: resultItem,
siteSpace: found?.siteSpace,
siteSection: found?.siteSection ?? undefined,
siteSectionGroup: found?.siteSectionGroup ?? undefined,
revisionPages: isCurrentRevisionSpace ? revision?.pages : undefined,
});
return result ? [{ rank: pageItem.rank, result }] : [];
}),
};
})
);
return transformSitePageResult({
asEmbeddable: Boolean(asEmbeddable),
linker: isCurrentRevisionSpace ? revisionLinker : context.linker,
pageItem,
spaceItem,
siteSpace: found?.siteSpace,
siteSection: found?.siteSection ?? undefined,
siteSectionGroup: found?.siteSectionGroup ?? undefined,
revisionPages: isCurrentRevisionSpace ? revision?.pages : undefined,
});
});
return NextResponse.json(results);
}
@@ -12,6 +12,7 @@ import { EmbeddableIframeAPI } from './EmbeddableIframeAPI';
import { EmbeddableThemeSync } from './EmbeddableThemeSync';
import { IfEmbeddableTrademark } from './EmbeddableTrademark';
import { CustomizationRootLayout } from '@/components/RootLayout';
import { getDirectSearchProps } from '@/components/Search/search-props';
import {
SiteLayoutClientContexts,
generateSiteLayoutMetadata,
@@ -60,6 +61,7 @@ export async function EmbeddableRootLayout({
contextId={context.contextId}
proxyOrigin={context.site.proxy?.origin}
searchPrewarmURL={context.linker.toPathInSite('~gitbook/search-prewarm')}
directSearch={getDirectSearchProps(context)}
>
{/* Persist an explicit ?theme= override so it survives tab navigation. RND-11571 */}
<EmbeddableThemeSync forcedTheme={rememberedTheme} />
@@ -0,0 +1,105 @@
import { afterEach, describe, expect, it, spyOn } from 'bun:test';
import { createSearchPassStore } from './search-pass-store';
import type { DirectSearchProps } from '@/lib/search-results';
const endpoint = 'https://api.gitbook.com/v1/orgs/org_1/sites/site_1/search';
const directSearch: DirectSearchProps = {
apiURL: 'https://api.gitbook.com',
organizationId: 'org_1',
siteId: 'site_1',
linker: {
urlMode: 'url-host',
siteHost: 'docs.example.com',
siteBasePath: '/',
spaceBasePath: '/',
gitbookURL: undefined,
},
spaces: {},
passURL: '/~gitbook/search-pass',
};
/** Answer each pass request with a new pass, and each API search with the next status. */
function mockFetch(apiStatuses: number[], passLifetimesMs: number[] = []) {
const authorizations: (string | null)[] = [];
let passes = 0;
const fetchSpy = spyOn(globalThis, 'fetch').mockImplementation((async (
input: RequestInfo | URL,
init?: RequestInit
) => {
if (init?.signal?.aborted) {
throw new DOMException('Aborted', 'AbortError');
}
if (String(input) === '/~gitbook/search-pass') {
passes += 1;
return Response.json({
token: `pass-${passes}`,
expiresIn: passLifetimesMs.shift() ?? 300_000,
});
}
expect(String(input)).toBe(endpoint);
authorizations.push(new Headers(init?.headers).get('authorization'));
const status = apiStatuses.shift() ?? 200;
return status === 200 ? Response.json({ items: [] }) : new Response(null, { status });
}) as typeof fetch);
return { authorizations, fetchSpy };
}
function search(store: ReturnType<typeof createSearchPassStore>, signal?: AbortSignal) {
return store.search({
query: 'tokens',
scope: { mode: 'all' },
signal: signal ?? new AbortController().signal,
asEmbeddable: false,
});
}
describe('createSearchPassStore', () => {
let restore: (() => void) | undefined;
afterEach(() => restore?.());
it('stays on the route for the rest of the page after a 403', async () => {
const { authorizations, fetchSpy } = mockFetch([403]);
restore = () => fetchSpy.mockRestore();
const store = createSearchPassStore(directSearch);
await store.requestPass();
expect(await search(store)).toEqual({ type: 'route', reason: '403' });
await store.requestPass();
expect(await search(store)).toEqual({ type: 'route', reason: '403' });
expect(authorizations).toHaveLength(1);
});
it.each([
['an expired pass', [], [1_000], 'expired', ['Bearer pass-2']],
['a 401', [401], [], '401', ['Bearer pass-1', 'Bearer pass-2']],
])(
'sends the search to the route on %s and searches directly again with a new pass',
async (_name, apiStatuses, passLifetimesMs, reason, sent) => {
const { authorizations, fetchSpy } = mockFetch(apiStatuses, passLifetimesMs);
restore = () => fetchSpy.mockRestore();
const store = createSearchPassStore(directSearch);
await store.requestPass();
expect(await search(store)).toEqual({ type: 'route', reason });
await new Promise((resolve) => setTimeout(resolve, 0));
expect(await search(store)).toEqual({ type: 'results', results: [] });
expect(authorizations).toEqual(sent);
}
);
it('neither retries nor falls back when the search is aborted', async () => {
const { authorizations, fetchSpy } = mockFetch([]);
restore = () => fetchSpy.mockRestore();
const store = createSearchPassStore(directSearch);
await store.requestPass();
const controller = new AbortController();
controller.abort();
await expect(search(store, controller.signal)).rejects.toThrow();
expect(await search(store)).toEqual({ type: 'results', results: [] });
expect(authorizations).toHaveLength(1);
});
});
@@ -0,0 +1,172 @@
import ReactDOM from 'react-dom';
import type { SearchRecordResult, SearchSpaceResult } from '@gitbook/api';
import type { OrderedComputedResult, SearchSiteContentScope } from './search-types';
import { getEmbeddableLinker } from '@/lib/embeddable-linker';
import { createSiteLinker } from '@/lib/links';
import {
type DirectSearchProps,
computePublishedSearchResults,
toSearchSiteContentBody,
} from '@/lib/search-results';
/** Request header on a search route call naming why the search did not go to the API. */
export const SEARCH_FALLBACK_HEADER = 'x-gitbook-search-fallback';
const PASS_REQUEST_TIMEOUT_MS = 3_000;
const EXPIRY_MARGIN_MS = 5_000;
const RATE_LIMIT_WAIT_MS = 2_000;
type SearchPassState =
| { status: 'none'; reason?: string }
| { status: 'ready'; token: string; expiresAt: number }
| { status: 'route'; reason: string };
type DirectSearchOutcome =
| { type: 'results'; results: OrderedComputedResult[] }
| { type: 'route'; reason?: string };
export type SearchPassStore = ReturnType<typeof createSearchPassStore>;
/**
* Hold a search pass from the site's search pass route in memory, and search the API with it.
*/
export function createSearchPassStore(directSearch: DirectSearchProps | undefined) {
let state: SearchPassState = { status: 'none' };
let pendingRequest: Promise<void> | null = null;
const sendPassRequest = async () => {
if (!directSearch) {
return;
}
try {
const response = await fetch(directSearch.passURL, {
method: 'POST',
signal: AbortSignal.timeout(PASS_REQUEST_TIMEOUT_MS),
});
if (!response.ok) {
throw new Error(`Search pass request failed: ${response.status}`);
}
if (response.status !== 200) {
return;
}
const { token, expiresIn } = (await response.json()) as {
token?: unknown;
expiresIn?: unknown;
};
if (
state.status !== 'route' &&
typeof token === 'string' &&
typeof expiresIn === 'number'
) {
state = { status: 'ready', token, expiresAt: Date.now() + expiresIn };
ReactDOM.preconnect(directSearch.apiURL, { crossOrigin: 'anonymous' });
}
} catch (error) {
if (state.status === 'none') {
state = { status: 'none', reason: 'pass_request' };
}
throw error;
}
};
const requestPass = () => {
pendingRequest ??= sendPassRequest().finally(() => {
pendingRequest = null;
});
return pendingRequest;
};
const dropPass = (token: string, reason: string): DirectSearchOutcome => {
if (state.status === 'ready' && state.token === token) {
state = { status: 'none', reason };
void requestPass().catch(() => undefined);
}
return { type: 'route', reason };
};
const fallBack = (reason: string): DirectSearchOutcome => {
state = { status: 'route', reason };
return { type: 'route', reason };
};
const search = async (input: {
query: string;
scope: SearchSiteContentScope;
signal: AbortSignal;
asEmbeddable: boolean;
}): Promise<DirectSearchOutcome> => {
const { query, scope, signal, asEmbeddable } = input;
if (!directSearch) {
return { type: 'route' };
}
if (state.status === 'ready' && state.expiresAt - Date.now() <= EXPIRY_MARGIN_MS) {
return dropPass(state.token, 'expired');
}
if (state.status !== 'ready') {
return { type: 'route', reason: state.reason };
}
if (query.length <= 1) {
return { type: 'results', results: [] };
}
const { token } = state;
const { apiURL, organizationId, siteId } = directSearch;
let response: Response;
try {
response = await fetch(`${apiURL}/v1/orgs/${organizationId}/sites/${siteId}/search`, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(toSearchSiteContentBody(query, scope)),
credentials: 'omit',
signal,
});
} catch (error) {
if (signal.aborted) {
throw error;
}
return fallBack('network');
}
if (response.status === 401) {
return dropPass(token, '401');
}
if (response.status === 429) {
fallBack('429');
await new Promise((resolve) => setTimeout(resolve, RATE_LIMIT_WAIT_MS));
signal.throwIfAborted();
return { type: 'route', reason: '429' };
}
if (!response.ok) {
return fallBack(String(response.status));
}
let results: OrderedComputedResult[] | null;
try {
const { items } = (await response.json()) as {
items: (SearchSpaceResult | SearchRecordResult)[];
};
const linker = createSiteLinker(directSearch.linker);
results = computePublishedSearchResults({
asEmbeddable,
linker: asEmbeddable ? getEmbeddableLinker(linker) : linker,
searchResults: items,
spaces: directSearch.spaces,
});
} catch (error) {
if (signal.aborted) {
throw error;
}
return fallBack('invalid');
}
return results ? { type: 'results', results } : fallBack('unknown_space');
};
return { requestPass, search };
}
@@ -2,6 +2,8 @@ import type { SiteExternalLink, SiteSection, SiteSectionGroup, SiteSpace } from
import { hasMultipleSiteSections } from '../SiteSections';
import type { GitBookSiteContext } from '@/lib/context';
import { getSearchSpaceLookup } from '@/lib/search';
import type { DirectSearchProps } from '@/lib/search-results';
import { getLocalizedTitle } from '@/lib/sites';
export type SearchSection = Pick<SiteSection, 'id' | 'icon' | 'object'> & {
@@ -123,3 +125,18 @@ export function findSearchSection(
return undefined;
}
/**
* What the browser needs to search the API directly, built from the site spaces visitors can see.
*/
export function getDirectSearchProps(context: GitBookSiteContext): DirectSearchProps | undefined {
if (!context.directSearch) {
return undefined;
}
return {
...context.directSearch,
spaces: getSearchSpaceLookup(context.structure),
passURL: context.linker.toPathInSite('~gitbook/search-pass'),
};
}
@@ -10,7 +10,10 @@ import {
import React from 'react';
import type { LinkProps } from '../primitives';
import { type SearchPassStore, createSearchPassStore } from './search-pass-store';
import { createSearchPrewarmer } from './search-prewarm';
import { useSearchPrewarm } from './useSearchPrewarm';
import type { DirectSearchProps } from '@/lib/search-results';
export type SearchScope =
/** Search all content on the site */
@@ -49,6 +52,7 @@ export type UpdateSearchState = (
const SetSearchStateContext = React.createContext<UpdateSearchState | undefined>(undefined);
const SearchStateContext = React.createContext<SearchState | null | undefined>(undefined);
const SearchPassContext = React.createContext<SearchPassStore | null>(null);
/**
* Normalize the raw state to handle legacy formats.
@@ -73,9 +77,9 @@ export function shouldKeepSearchState(
}
export function SearchContextProvider(
props: React.PropsWithChildren<{ prewarmURL: string }>
props: React.PropsWithChildren<{ prewarmURL: string; directSearch?: DirectSearchProps }>
): React.ReactElement {
const { children, prewarmURL } = props;
const { children, prewarmURL, directSearch } = props;
// URL-backed state
const [rawState, setRawState] = useQueryStates(keyMap, {
@@ -101,6 +105,12 @@ export function SearchContextProvider(
useSearchPrewarm(Boolean(state?.open), prewarmURL);
const searchPass = React.useMemo(() => createSearchPassStore(directSearch), [directSearch]);
const passRequester = React.useMemo(() => createSearchPrewarmer(false), []);
React.useEffect(() => {
passRequester.update(Boolean(state?.open), searchPass.requestPass);
}, [state?.open, searchPass, passRequester]);
const stateRef = React.useRef(state);
React.useLayoutEffect(() => {
stateRef.current = state;
@@ -136,12 +146,18 @@ export function SearchContextProvider(
return (
<SearchStateContext.Provider value={state}>
<SetSearchStateContext.Provider value={setState}>
{children}
<SearchPassContext.Provider value={searchPass}>
{children}
</SearchPassContext.Provider>
</SetSearchStateContext.Provider>
</SearchStateContext.Provider>
);
}
export function useSearchPass() {
return React.useContext(SearchPassContext);
}
export function useSetSearchState() {
const setState = React.useContext(SetSearchStateContext);
if (setState === undefined) {
@@ -12,10 +12,11 @@ import { computeFilterSiteSpaceIds } from './filter';
import { useRecentSearchQueries } from './recent-queries';
import { type MergedPageResult, fuseSearchResults } from './reciprocalRankFusion';
import { computeRemoteSearchScope } from './remote-scope';
import { SEARCH_FALLBACK_HEADER, type SearchPassStore } from './search-pass-store';
import type { OrderedComputedResult, SearchSiteContentScope } from './search-types';
import { streamRecommendedQuestions } from './server-actions';
import { type LocalPageResult, useLocalSearchResults } from './useLocalSearchResults';
import type { SearchScope } from './useSearch';
import { type SearchScope, useSearchPass } from './useSearch';
import { useAI } from '@/components/AI';
export type ResultType =
@@ -74,6 +75,7 @@ export function useSearchResults(props: {
} = props;
const trackEvent = useTrackEvent();
const searchPass = useSearchPass();
const filterSiteSpaceIds = React.useMemo(
() =>
@@ -211,7 +213,14 @@ export function useSearchResults(props: {
const fetchSearch = (
scope: Parameters<typeof fetchSearchResults>[1]
): Promise<OrderedComputedResult[]> =>
fetchSearchResults(searchURL, scope, query, abortController.signal, asEmbeddable);
fetchSearchResults(
searchURL,
scope,
query,
abortController.signal,
asEmbeddable,
searchPass
);
try {
const resultsPromise = fetchSearch(
@@ -287,6 +296,7 @@ export function useSearchResults(props: {
suggestions,
searchURL,
asEmbeddable,
searchPass,
]);
const abort = React.useCallback(() => {
@@ -339,18 +349,32 @@ export function useSearchResults(props: {
}
/**
* Fetch search results from the search API route.
* Fetch search results from the search API with a search pass, or else from the search API route.
*/
async function fetchSearchResults(
searchURL: string,
scope: SearchSiteContentScope,
query: string,
signal?: AbortSignal,
asEmbeddable?: boolean
signal: AbortSignal,
asEmbeddable: boolean | undefined,
searchPass: SearchPassStore | null
): Promise<OrderedComputedResult[]> {
const direct = await searchPass?.search({
query,
scope,
signal,
asEmbeddable: Boolean(asEmbeddable),
});
if (direct?.type === 'results') {
return direct.results;
}
const response = await fetch(searchURL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
headers: {
'Content-Type': 'application/json',
...(direct?.reason ? { [SEARCH_FALLBACK_HEADER]: direct.reason } : {}),
},
body: JSON.stringify({
asEmbeddable,
query,
@@ -11,6 +11,7 @@ import { SiteLayoutClientContexts } from './SiteLayoutClientContexts';
import { AdminToolbar } from '@/components/AdminToolbar';
import { CookiesToast } from '@/components/Cookies';
import { LoadIntegrations } from '@/components/Integrations';
import { getDirectSearchProps } from '@/components/Search/search-props';
import { SpaceLayout } from '@/components/SpaceLayout';
import { WebMCP } from '@/components/WebMCP/WebMCP';
import type { VisitorAuthClaims } from '@/lib/adaptive';
@@ -108,6 +109,7 @@ export async function SiteLayout(props: {
context.linker.toAbsoluteURL(context.linker.toPathInSite(''))
)}
searchPrewarmURL={context.linker.toPathInSite('~gitbook/search-prewarm')}
directSearch={getDirectSearchProps(context)}
>
<AIContextProvider
aiMode={customization.ai?.mode}
@@ -13,6 +13,7 @@ import { LinkContext, type LinkContextType } from '../primitives';
import { SearchContextProvider } from '../Search';
import { SelectProvider } from '../Select';
import { isExternalLink } from '../utils/link';
import type { DirectSearchProps } from '@/lib/search-results';
/**
* Client component context providers for the site layout.
@@ -27,6 +28,7 @@ export function SiteLayoutClientContexts(props: {
/** Absolute URL of the site, so same-site absolute links render as internal on the server. */
siteURL?: string;
searchPrewarmURL: string;
directSearch: DirectSearchProps | undefined;
children: React.ReactNode;
}) {
const {
@@ -39,6 +41,7 @@ export function SiteLayoutClientContexts(props: {
proxyOrigin,
siteURL,
searchPrewarmURL,
directSearch,
} = props;
useClearRouterCache(contextId);
@@ -72,7 +75,10 @@ export function SiteLayoutClientContexts(props: {
<NuqsAdapter>
<SelectProvider>
<LinkContext.Provider value={linkContext}>
<SearchContextProvider prewarmURL={searchPrewarmURL}>
<SearchContextProvider
prewarmURL={searchPrewarmURL}
directSearch={directSearch}
>
<ReducedMotionProvider>{children}</ReducedMotionProvider>
</SearchContextProvider>
</LinkContext.Provider>
+32 -24
View File
@@ -20,9 +20,14 @@ import type {
TranslationLanguage,
} from '@gitbook/api';
import { GITBOOK_URL } from './env';
import { GITBOOK_API_PUBLIC_URL, GITBOOK_URL } from './env';
import { type ImageResizer, createImageResizer } from './images';
import { type GitBookLinker, createLinker, linkerForPublishedURL } from './links';
import {
type GitBookLinker,
type SiteLinkerInput,
createSiteLinker,
linkerForPublishedURL,
} from './links';
import type { AskVisitorAgent } from '@/lib/ask-visitor';
import {
type GitBookDataFetcher,
@@ -31,6 +36,7 @@ import {
throwIfDataError,
} from '@/lib/data';
import { getLogger } from '@/lib/logger';
import type { DirectSearchProps } from '@/lib/search-results';
import {
findSiteSpaceBy,
getFallbackSiteSpacePath,
@@ -133,6 +139,11 @@ export type GitBookBaseContext = {
* Translation language of the space.
*/
locale?: TranslationLanguage;
/**
* Target for the browser to query the site search API directly with a search pass.
*/
directSearch?: Omit<DirectSearchProps, 'spaces' | 'passURL'>;
};
/**
@@ -240,28 +251,14 @@ export function getBaseContext(input: {
apiToken: siteURLData.apiToken ?? null,
});
const gitbookURL = GITBOOK_URL ? new URL(GITBOOK_URL) : undefined;
const linker =
urlMode === 'url-host'
? createLinker({
host: siteURL.host,
siteBasePath: siteURLData.siteBasePath,
spaceBasePath: siteURLData.basePath,
})
: createLinker({
protocol: gitbookURL?.protocol,
host: gitbookURL?.host,
siteBasePath: `/url/${siteURL.host}${siteURLData.siteBasePath}`,
spaceBasePath: `/url/${siteURL.host}${siteURLData.basePath}`,
});
if (urlMode === 'url') {
// Create link in the same format for links to other sites/sections.
linker.toLinkForContent = (rawURL: string) => {
const urlObject = new URL(rawURL);
return `/url/${urlObject.host}${urlObject.pathname}${urlObject.search}${urlObject.hash}`;
};
}
const linkerInput: SiteLinkerInput = {
urlMode,
siteHost: siteURL.host,
siteBasePath: siteURLData.siteBasePath,
spaceBasePath: siteURLData.basePath,
gitbookURL: GITBOOK_URL || undefined,
};
const linker = createSiteLinker(linkerInput);
const imageResizer = createImageResizer({
imagesContextId: siteURLData.imagesContextId,
@@ -270,10 +267,21 @@ export function getBaseContext(input: {
linker: linker,
});
const directSearch: GitBookBaseContext['directSearch'] =
!siteURLData.revision && !siteURLData.changeRequest && !siteURLData.preview
? {
apiURL: GITBOOK_API_PUBLIC_URL,
organizationId: siteURLData.organization,
siteId: siteURLData.site,
linker: linkerInput,
}
: undefined;
return {
dataFetcher,
linker,
imageResizer,
directSearch,
};
}
+44
View File
@@ -197,6 +197,50 @@ export function createLinker(
return linker;
}
/**
* Serializable inputs of the linker of a site request.
*/
export type SiteLinkerInput = {
urlMode: 'url' | 'url-host';
siteHost: string;
siteBasePath: string;
spaceBasePath: string;
/** Main host GitBook runs on, used in `url` mode. */
gitbookURL: string | undefined;
};
/**
* Create the linker of a site request, for the URL mode it is served in.
*/
export function createSiteLinker(input: SiteLinkerInput): GitBookLinker {
const { urlMode, siteHost, siteBasePath, spaceBasePath } = input;
const gitbookURL = input.gitbookURL ? new URL(input.gitbookURL) : undefined;
const linker =
urlMode === 'url-host'
? createLinker({
host: siteHost,
siteBasePath,
spaceBasePath,
})
: createLinker({
protocol: gitbookURL?.protocol,
host: gitbookURL?.host,
siteBasePath: `/url/${siteHost}${siteBasePath}`,
spaceBasePath: `/url/${siteHost}${spaceBasePath}`,
});
if (urlMode === 'url') {
// Create link in the same format for links to other sites/sections.
linker.toLinkForContent = (rawURL: string) => {
const urlObject = new URL(rawURL);
return `/url/${urlObject.host}${urlObject.pathname}${urlObject.search}${urlObject.hash}`;
};
}
return linker;
}
/**
* Create a new linker that intercepts links that belongs to the published site and rewrite them
* relative to the URL being served.
@@ -0,0 +1,188 @@
import { describe, expect, it } from 'bun:test';
import type {
SearchPageResult,
SearchRecordResult,
SearchSpaceResult,
SiteSpace,
SiteStructure,
} from '@gitbook/api';
import { getBaseContext } from './context';
import { getEmbeddableLinker } from './embeddable-linker';
import { createSiteLinker } from './links';
import { getSearchSpaceLookup, transformSitePageResult } from './search';
import { computePublishedSearchResults, computeSearchResults } from './search-results';
import { findSiteSpaceBy } from './sites';
function siteSpace(id: string, title: string, published: string, extra: Partial<SiteSpace> = {}) {
return {
object: 'site-space',
id: `ss_${id}`,
title,
path: '',
default: false,
draft: false,
space: { id: `space_${id}`, title, language: 'en' },
urls: { published },
...extra,
} as SiteSpace;
}
const structure = {
type: 'sections',
structure: [
{
object: 'site-section',
id: 'section_guides',
title: 'Guides',
default: true,
siteSpaces: [siteSpace('guides', 'Guides', 'https://docs.example.com/')],
},
{
object: 'site-section-group',
id: 'group_products',
title: 'Products',
icon: 'box',
children: [
{
object: 'site-section',
id: 'section_api',
title: 'API',
icon: 'code',
siteSpaces: [
siteSpace('v2', 'v2', 'https://docs.example.com/api/', { default: true }),
siteSpace('v1', 'v1', 'https://docs.example.com/api/v1/'),
siteSpace('v0', 'v0', 'https://docs.example.com/api/v0/', { hidden: true }),
],
},
],
},
],
} as unknown as SiteStructure;
function space(id: string, score: number, pages: Partial<SearchPageResult>[]): SearchSpaceResult {
return {
type: 'space',
id: `space_${id}`,
title: id,
score,
pages: pages.map((page) => ({
title: `Page ${page.id}`,
description: 'Synthetic result',
score: 10 - (page.rank ?? 0),
ancestors: [],
urls: { app: 'https://app.gitbook.com/o/org/s/space' },
...page,
})) as SearchPageResult[],
};
}
const items: (SearchSpaceResult | SearchRecordResult)[] = [
space('v2', 3, [
{
id: 'auth',
path: 'authentication',
rank: 2,
ancestors: [{ title: 'Getting started' }],
sections: [
{
id: 'tokens',
title: 'Tokens',
body: 'Create a token',
path: 'authentication#tokens',
score: 4,
urls: { app: 'https://app.gitbook.com/o/org/s/space' },
},
],
},
]),
space('guides', 2, [
{ id: 'intro', path: '', rank: 1 },
{ id: 'status', path: 'https://status.example.com/', rank: 3 },
]),
space('v1', 1, [{ id: 'legacy', path: 'legacy/authentication', rank: 4 }]),
{
type: 'record',
id: 'record_1',
title: 'Support ticket',
description: 'Reset a token',
url: 'https://support.example.com/1',
score: 2,
} as SearchRecordResult,
];
describe('computePublishedSearchResults', () => {
it.each([
['url-host', false],
['url-host', true],
['url', false],
['url', true],
] as const)('gives the route results (%s, embedded: %p)', (urlMode, asEmbeddable) => {
const context = getBaseContext({
siteURL: 'https://docs.example.com/api/',
siteURLData: {
organization: 'org_1',
site: 'site_1',
siteSection: 'section_api',
siteSpace: 'ss_v2',
space: 'space_v2',
siteBasePath: '/',
basePath: '/api/',
apiToken: 'page-token',
imagesContextId: 'images',
} as Parameters<typeof getBaseContext>[0]['siteURLData'],
urlMode,
});
const directSearch = context.directSearch!;
const embed = (linker: typeof context.linker) =>
asEmbeddable ? getEmbeddableLinker(linker) : linker;
const route = computeSearchResults(items, (spaceItem, pageItem) => {
const found = findSiteSpaceBy(structure, (s) => s.space.id === spaceItem.id);
return transformSitePageResult({
asEmbeddable,
linker: embed(context.linker),
pageItem,
spaceItem,
siteSpace: found?.siteSpace,
siteSection: found?.siteSection ?? undefined,
siteSectionGroup: found?.siteSectionGroup ?? undefined,
});
});
const browser = computePublishedSearchResults({
asEmbeddable,
linker: embed(createSiteLinker(directSearch.linker)),
searchResults: items,
spaces: getSearchSpaceLookup(structure),
});
expect(JSON.stringify(directSearch)).not.toContain('page-token');
expect(browser).toEqual(route);
expect(browser?.map((result) => result.id)).toEqual([
'space_guides/intro',
'space_v2/auth',
'space_guides/status',
'space_v1/legacy',
'record_1',
]);
});
it('leaves hidden variants out of the lookup, so their results go to the route', () => {
const spaces = getSearchSpaceLookup(structure);
expect(Object.keys(spaces).sort()).toEqual(['space_guides', 'space_v1', 'space_v2']);
expect(JSON.stringify(spaces)).not.toContain('/api/v0/');
const linker = createSiteLinker({
urlMode: 'url-host',
siteHost: 'docs.example.com',
siteBasePath: '/',
spaceBasePath: '/api/',
gitbookURL: undefined,
});
const searchResults = [...items, space('v0', 1, [{ id: 'draft', path: 'draft', rank: 5 }])];
expect(
computePublishedSearchResults({ asEmbeddable: false, linker, searchResults, spaces })
).toBeNull();
});
});
+225
View File
@@ -0,0 +1,225 @@
import type {
GitBookAPI,
SearchPageResult,
SearchRecordResult,
SearchSpaceResult,
SiteSearchScope,
} from '@gitbook/api';
import { orderSearchResultGroups } from '@/app/sites/dynamic/[mode]/[siteURL]/[siteData]/~gitbook/search/orderSearchResults';
import type {
ComputedPageResult,
ComputedSectionResult,
OrderedComputedResult,
SearchSiteContentScope,
} from '@/components/Search/search-types';
import { toEmbeddableLinkForPublishedContent } from '@/lib/embeddable-linker';
import type { GitBookLinker, SiteLinkerInput } from '@/lib/links';
import { joinPathWithBaseURL } from '@/lib/paths';
import { checkIsHttpURL } from '@/lib/urls';
export type SearchResultBreadcrumb = NonNullable<ComputedPageResult['breadcrumbs']>[number];
/**
* What a search result needs from the site structure for the space it belongs to.
*/
export type SearchSpaceEntry = {
siteSpaceId?: string;
spaceURL?: string;
breadcrumbs: SearchResultBreadcrumb[];
};
/**
* Search space entries keyed by space ID, for the first site space showing that space.
*/
export type SearchSpaceLookup = Record<string, SearchSpaceEntry>;
/**
* Everything the browser needs, besides a search pass, to query the search API of a site directly.
*/
export type DirectSearchProps = {
apiURL: string;
organizationId: string;
siteId: string;
linker: SiteLinkerInput;
spaces: SearchSpaceLookup;
/** Site route that hands out a search pass. */
passURL: string;
};
/**
* Body of the site search API request for a query in a scope.
*/
export function toSearchSiteContentBody(
query: string,
scope: SearchSiteContentScope
): Parameters<GitBookAPI['orgs']['searchSiteContent']>[2] {
return {
query,
...(scope.mode === 'current' && scope.restrictTo
? {
// `restrictTo` only exists in the newer `scope` request shape,
// and the published @gitbook/api types don't include it yet.
scope: {
mode: 'default',
currentSiteSpace: scope.siteSpaceId,
restrictTo: scope.restrictTo,
} as SiteSearchScope,
}
: scope),
};
}
/**
* Turn the search API items into ordered results, with page results computed by the caller.
*/
export function computeSearchResults(
searchResults: (SearchSpaceResult | SearchRecordResult)[],
getPageResult: (
spaceItem: SearchSpaceResult,
pageItem: SearchPageResult
) => ComputedPageResult | null
): OrderedComputedResult[] {
return orderSearchResultGroups<OrderedComputedResult>(
searchResults.map((resultItem) => {
if (resultItem.type === 'record') {
const result: OrderedComputedResult = {
type: 'record',
id: resultItem.id,
title: resultItem.title,
description: resultItem.description,
href: resultItem.url,
score: resultItem.score,
};
return { type: 'context' as const, results: [result] };
}
return {
type: 'pages' as const,
results: resultItem.pages.flatMap((pageItem) => {
const result = getPageResult(resultItem, pageItem);
return result ? [{ rank: pageItem.rank, result }] : [];
}),
};
})
);
}
/**
* Compute the results of the published content of a site, resolving spaces from a lookup.
* Returns null when a page result belongs to a space missing from the lookup.
*/
export function computePublishedSearchResults(input: {
asEmbeddable: boolean;
linker: GitBookLinker;
searchResults: (SearchSpaceResult | SearchRecordResult)[];
spaces: SearchSpaceLookup;
}): OrderedComputedResult[] | null {
const { asEmbeddable, linker, searchResults, spaces } = input;
const hasUnknownSpace = searchResults.some(
(item) => item.type === 'space' && item.pages.length > 0 && !spaces[item.id]
);
if (hasUnknownSpace) {
return null;
}
return computeSearchResults(searchResults, (spaceItem, pageItem) => {
const entry: SearchSpaceEntry | undefined = spaces[spaceItem.id];
const toHref = (pagePath: string) =>
getPublishedPageHref({ asEmbeddable, linker, pagePath, spaceURL: entry?.spaceURL });
return buildSearchPageResult({
pageItem,
spaceItem,
entry,
pageHref: toHref(pageItem.path),
getSectionHref: toHref,
});
});
}
/**
* Build the result for a page, with its best section.
*/
export function buildSearchPageResult(input: {
pageItem: SearchPageResult;
spaceItem: SearchSpaceResult;
entry: SearchSpaceEntry | undefined;
pageHref: string;
getSectionHref: (sectionPath: string) => string;
}): ComputedPageResult {
const { pageItem, spaceItem, entry, pageHref, getSectionHref } = input;
const page: ComputedPageResult = {
type: 'page',
id: `${spaceItem.id}/${pageItem.id}`,
title: pageItem.title,
description: pageItem.description,
href: pageHref,
pageId: pageItem.id,
spaceId: spaceItem.id,
siteSpaceId: entry?.siteSpaceId,
score: pageItem.score,
rank: pageItem.rank,
resultType: pageItem.resultType,
breadcrumbs: [
...(entry?.breadcrumbs ?? []),
...pageItem.ancestors.map((ancestor) => ({
label: ancestor.title,
})),
],
};
const pageSections =
pageItem.sections
?.filter((section) => section.title || section.body)
.map<ComputedSectionResult>((section) => ({
type: 'section',
id: `${page.id}/${section.id}`,
title: section.title,
href: getSectionHref(section.path),
body: section.body,
pageId: pageItem.id,
spaceId: spaceItem.id,
score: section.score,
})) ?? [];
// The API returns at most one section per page, ordered for use as the section destination preview.
const bestSection = pageSections[0];
if (bestSection) {
page.bestSection = {
href: bestSection.href,
title: bestSection.title,
body: bestSection.body,
score: bestSection.score,
};
}
return page;
}
/**
* Link to a page path of the published space at `spaceURL`.
*/
export function getPublishedPageHref(input: {
asEmbeddable: boolean;
linker: GitBookLinker;
pagePath: string;
spaceURL: string | undefined;
}) {
const { asEmbeddable, linker, pagePath, spaceURL } = input;
if (checkIsHttpURL(pagePath)) {
return pagePath;
}
if (!spaceURL) {
return linker.toPathInSpace(pagePath);
}
return asEmbeddable
? toEmbeddableLinkForPublishedContent(linker, spaceURL, pagePath)
: linker.toLinkForContent(joinPathWithBaseURL(spaceURL, pagePath));
}
+74 -96
View File
@@ -6,16 +6,21 @@ import type {
SiteSection,
SiteSectionGroup,
SiteSpace,
SiteStructure,
} from '@gitbook/api';
import type { IconName } from '@gitbook/icons';
import type { ComputedPageResult, ComputedSectionResult } from '@/components/Search/search-types';
import { toEmbeddableLinkForPublishedContent } from '@/lib/embeddable-linker';
import type { ComputedPageResult } from '@/components/Search/search-types';
import type { GitBookLinker } from '@/lib/links';
import { resolvePageId } from '@/lib/pages';
import { joinPathWithBaseURL } from '@/lib/paths';
import { getLocalizedTitle } from '@/lib/sites';
import { checkIsHttpURL } from '@/lib/urls';
import {
type SearchResultBreadcrumb,
type SearchSpaceEntry,
type SearchSpaceLookup,
buildSearchPageResult,
getPublishedPageHref,
} from '@/lib/search-results';
import { findSiteSpaceBy, getLocalizedTitle, listAllSiteSpaces } from '@/lib/sites';
export function transformSitePageResult(args: {
asEmbeddable: boolean;
@@ -37,20 +42,77 @@ export function transformSitePageResult(args: {
linker,
revisionPages,
} = args;
const currentLanguage = siteSpace?.space.language;
const spaceURL = siteSpace?.urls.published;
const breadcrumbs: NonNullable<ComputedPageResult['breadcrumbs']> = [];
const entry = getSearchSpaceEntry({ siteSpace, siteSection, siteSectionGroup });
let revisionTarget: { pages: Revision['pages']; page: RevisionPageDocument } | undefined;
if (revisionPages) {
const resolved = resolvePageId(revisionPages, pageItem.id);
if (!resolved || resolved.page.id !== pageItem.id) {
return null;
}
revisionTarget = { pages: revisionPages, page: resolved.page };
const revisionTarget: { pages: Revision['pages']; page: RevisionPageDocument } = {
pages: revisionPages,
page: resolved.page,
};
return buildSearchPageResult({
pageItem,
spaceItem,
entry,
pageHref: linker.toPathForPage(revisionTarget),
getSectionHref: (sectionPath) =>
linker.toPathForPage({
...revisionTarget,
anchor: getRetainableSectionAnchor(sectionPath, pageItem.path),
}),
});
}
const toHref = (pagePath: string) =>
getPublishedPageHref({ asEmbeddable, linker, pagePath, spaceURL: entry.spaceURL });
return buildSearchPageResult({
pageItem,
spaceItem,
entry,
pageHref: toHref(pageItem.path),
getSectionHref: toHref,
});
}
/**
* Index the visible site spaces by space, resolving each space as the search route does.
*/
export function getSearchSpaceLookup(structure: SiteStructure): SearchSpaceLookup {
const lookup: SearchSpaceLookup = {};
for (const { space } of listAllSiteSpaces(structure)) {
if (lookup[space.id]) {
continue;
}
const found = findSiteSpaceBy(structure, (siteSpace) => siteSpace.space.id === space.id);
if (found && !found.siteSpace.hidden) {
lookup[space.id] = getSearchSpaceEntry({
siteSpace: found.siteSpace,
siteSection: found.siteSection ?? undefined,
siteSectionGroup: found.siteSectionGroup ?? undefined,
});
}
}
return lookup;
}
function getSearchSpaceEntry(input: {
siteSpace?: SiteSpace;
siteSection?: SiteSection;
siteSectionGroup?: SiteSectionGroup | null;
}): SearchSpaceEntry {
const { siteSpace, siteSection, siteSectionGroup } = input;
const currentLanguage = siteSpace?.space.language;
const breadcrumbs: SearchResultBreadcrumb[] = [];
if (siteSectionGroup) {
breadcrumbs.push({
icon: siteSectionGroup.icon as IconName,
@@ -79,74 +141,11 @@ export function transformSitePageResult(args: {
});
}
breadcrumbs.push(
...pageItem.ancestors.map((ancestor) => ({
label: ancestor.title,
}))
);
const pageHref = revisionTarget
? linker.toPathForPage(revisionTarget)
: getPublishedPageHref({ asEmbeddable, linker, pagePath: pageItem.path, spaceURL });
const page: ComputedPageResult = {
type: 'page',
id: `${spaceItem.id}/${pageItem.id}`,
title: pageItem.title,
description: pageItem.description,
href: pageHref,
pageId: pageItem.id,
spaceId: spaceItem.id,
return {
siteSpaceId: siteSpace?.id,
score: pageItem.score,
rank: pageItem.rank,
resultType: pageItem.resultType,
spaceURL: siteSpace?.urls.published,
breadcrumbs,
};
const pageSections =
pageItem.sections
?.filter((section) => section.title || section.body)
.map<ComputedSectionResult>((section) => {
const anchor = revisionTarget
? getRetainableSectionAnchor(section.path, pageItem.path)
: undefined;
const sectionHref = revisionTarget
? linker.toPathForPage({
...revisionTarget,
anchor,
})
: getPublishedPageHref({
asEmbeddable,
linker,
pagePath: section.path,
spaceURL,
});
return {
type: 'section',
id: `${page.id}/${section.id}`,
title: section.title,
href: sectionHref,
body: section.body,
pageId: pageItem.id,
spaceId: spaceItem.id,
score: section.score,
};
}) ?? [];
// The API returns at most one section per page, ordered for use as the section destination preview.
const bestSection = pageSections[0];
if (bestSection) {
page.bestSection = {
href: bestSection.href,
title: bestSection.title,
body: bestSection.body,
score: bestSection.score,
};
}
return page;
}
function getRetainableSectionAnchor(sectionPath: string, pagePath: string): string | undefined {
@@ -157,24 +156,3 @@ function getRetainableSectionAnchor(sectionPath: string, pagePath: string): stri
return sectionPath.slice(hashIndex + 1) || undefined;
}
function getPublishedPageHref(input: {
asEmbeddable: boolean;
linker: GitBookLinker;
pagePath: string;
spaceURL: string | undefined;
}) {
const { asEmbeddable, linker, pagePath, spaceURL } = input;
if (checkIsHttpURL(pagePath)) {
return pagePath;
}
if (!spaceURL) {
return linker.toPathInSpace(pagePath);
}
return asEmbeddable
? toEmbeddableLinkForPublishedContent(linker, spaceURL, pagePath)
: linker.toLinkForContent(joinPathWithBaseURL(spaceURL, pagePath));
}
+60
View File
@@ -385,6 +385,10 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
return await serveSearchPrewarm(request, siteURLData);
}
if (isSearchPassRequest(siteURLData)) {
return await serveSearchPass(request, siteURLData);
}
//
// Render and serve the content
//
@@ -832,6 +836,62 @@ async function serveSearchPrewarm(request: NextRequest, siteURLData: PublishedSi
return new NextResponse(null, { status: 204 });
}
const SEARCH_PASS_TIMEOUT_MS = 1_500;
function isSearchPassRequest(siteURLData: PublishedSiteContent) {
return removeLeadingSlash(removeTrailingSlash(siteURLData.pathname)) === '~gitbook/search-pass';
}
/**
* Give the visitor's browser a short-lived pass to search the site with the API directly.
*/
async function serveSearchPass(request: NextRequest, siteURLData: PublishedSiteContent) {
if (request.method !== 'POST') {
return new NextResponse(null, { status: 405, headers: { allow: 'POST' } });
}
const api = apiClient({ apiToken: siteURLData.apiToken ?? null });
// The published API client has no method for this operation yet.
const pass = api
.request({
path: `/orgs/${siteURLData.organization}/sites/${siteURLData.site}/search/pass`,
method: 'POST',
secure: true,
})
.then(
(response) =>
response.json() as Promise<{
searchPass?: { token?: unknown; expiresAt?: unknown };
}>
)
.catch(() => null);
let timeout: ReturnType<typeof setTimeout> | undefined;
const reply = await Promise.race([
pass,
new Promise<null>((resolve) => {
timeout = setTimeout(() => resolve(null), SEARCH_PASS_TIMEOUT_MS);
}),
]);
clearTimeout(timeout);
const { token, expiresAt } = reply?.searchPass ?? {};
// The browser adds this lifetime to its own clock, so its clock skew does not matter.
const expiresIn =
typeof expiresAt === 'string' ? Date.parse(expiresAt) - Date.now() : Number.NaN;
if (typeof token !== 'string' || !token || !(expiresIn > 0)) {
return new NextResponse(null, {
status: 204,
headers: { 'cache-control': 'private, no-store' },
});
}
return NextResponse.json(
{ token, expiresIn },
{ headers: { 'cache-control': 'private, no-store' } }
);
}
/**
* Encode path in a site content.
* Special paths are not encoded and passed to be handled by the route handlers.