mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-11-26 10:13:46 +00:00
Use same-domain direct site search as fallback only
This commit is contained in:
@@ -2,4 +2,4 @@
|
||||
"gitbook": patch
|
||||
---
|
||||
|
||||
Retry direct site search on the site's own domain after a network error or timeout, and start on the same-domain proxy when `search.sameDomain` is enabled in site customization.
|
||||
Retry direct site search on the site's own domain after a network error or timeout.
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { afterEach, describe, expect, it, spyOn } from 'bun:test';
|
||||
import ReactDOM from 'react-dom';
|
||||
|
||||
import { createSearchPassStore } from './search-pass-store';
|
||||
import type { DirectSearchProps } from '@/lib/search-results';
|
||||
@@ -106,29 +105,6 @@ describe('createSearchPassStore', () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it('uses the site proxy immediately and skips preconnect when sameDomain is true', async () => {
|
||||
const preconnectSpy = spyOn(ReactDOM, 'preconnect').mockImplementation(() => undefined);
|
||||
const fetchSpy = spyOn(globalThis, 'fetch')
|
||||
.mockResolvedValueOnce(Response.json({ token: 'pass-1', expiresIn: 300_000 }))
|
||||
.mockResolvedValueOnce(Response.json({ items: [] }));
|
||||
restore = () => {
|
||||
fetchSpy.mockRestore();
|
||||
preconnectSpy.mockRestore();
|
||||
};
|
||||
|
||||
const store = createSearchPassStore({ ...directSearch, sameDomain: true });
|
||||
await store.requestPass();
|
||||
|
||||
expect(preconnectSpy).not.toHaveBeenCalled();
|
||||
expect(await search(store)).toEqual({ type: 'results', results: [] });
|
||||
expect(fetchSpy.mock.calls.map(([url]) => url)).toEqual([directSearch.passURL, proxyURL]);
|
||||
const init = fetchSpy.mock.calls[1]?.[1];
|
||||
expect(init).toMatchObject({ mode: 'same-origin', credentials: 'omit' });
|
||||
const headers = new Headers(init?.headers);
|
||||
expect(headers.get('x-gitbook-search-pass')).toBe('pass-1');
|
||||
expect(headers.has('authorization')).toBe(false);
|
||||
});
|
||||
|
||||
it('stays on the route for the rest of the page after a 403', async () => {
|
||||
const { authorizations, fetchSpy } = mockFetch([403]);
|
||||
restore = () => fetchSpy.mockRestore();
|
||||
|
||||
@@ -44,7 +44,7 @@ export type SearchPassStore = ReturnType<typeof createSearchPassStore>;
|
||||
*/
|
||||
export function createSearchPassStore(directSearch: DirectSearchProps | undefined) {
|
||||
let state: SearchPassState = { status: 'none' };
|
||||
let useProxy = Boolean(directSearch?.sameDomain && directSearch.proxyURL);
|
||||
let useProxy = false;
|
||||
let pendingRequest: Promise<void> | null = null;
|
||||
|
||||
const sendPassRequest = async () => {
|
||||
@@ -83,7 +83,7 @@ export function createSearchPassStore(directSearch: DirectSearchProps | undefine
|
||||
(current.status === 'none' && current.reason === '401') ||
|
||||
(current.status === 'ready' && current.replacesRejectedPass),
|
||||
};
|
||||
if (!useProxy && !directSearch.sameDomain) {
|
||||
if (!useProxy) {
|
||||
ReactDOM.preconnect(directSearch.apiURL, { crossOrigin: 'anonymous' });
|
||||
}
|
||||
} else if (current.status === 'ready') {
|
||||
|
||||
@@ -1,10 +1,4 @@
|
||||
import type {
|
||||
SiteCustomizationSettings,
|
||||
SiteExternalLink,
|
||||
SiteSection,
|
||||
SiteSectionGroup,
|
||||
SiteSpace,
|
||||
} from '@gitbook/api';
|
||||
import type { SiteExternalLink, SiteSection, SiteSectionGroup, SiteSpace } from '@gitbook/api';
|
||||
|
||||
import { hasMultipleSiteSections } from '../SiteSections';
|
||||
import type { GitBookSiteContext } from '@/lib/context';
|
||||
@@ -138,11 +132,8 @@ export function getDirectSearchProps(context: GitBookSiteContext): DirectSearchP
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const customization: SiteCustomizationSettings & {
|
||||
search?: { sameDomain?: boolean };
|
||||
} = context.customization;
|
||||
const { organizationId, siteId } = context.directSearch;
|
||||
const props: DirectSearchProps = {
|
||||
return {
|
||||
...context.directSearch,
|
||||
spaces: getSearchSpaceLookup(context.structure),
|
||||
passURL: context.linker.toPathInSite('~gitbook/search-pass'),
|
||||
@@ -150,9 +141,4 @@ export function getDirectSearchProps(context: GitBookSiteContext): DirectSearchP
|
||||
`~gitbook/search-direct/orgs/${organizationId}/sites/${siteId}`
|
||||
),
|
||||
};
|
||||
if (customization.search?.sameDomain === true) {
|
||||
props.sameDomain = true;
|
||||
}
|
||||
|
||||
return props;
|
||||
}
|
||||
|
||||
@@ -42,7 +42,6 @@ export type DirectSearchProps = {
|
||||
organizationId: string;
|
||||
siteId: string;
|
||||
proxyURL?: string;
|
||||
sameDomain?: boolean;
|
||||
linker: SiteLinkerInput;
|
||||
spaces: SearchSpaceLookup;
|
||||
/** Site route that hands out a search pass. */
|
||||
|
||||
Reference in New Issue
Block a user