diff --git a/packages/gitbook/src/components/Insights/InsightsProvider.tsx b/packages/gitbook/src/components/Insights/InsightsProvider.tsx index 66e956a17..2e65ab8c5 100644 --- a/packages/gitbook/src/components/Insights/InsightsProvider.tsx +++ b/packages/gitbook/src/components/Insights/InsightsProvider.tsx @@ -192,31 +192,14 @@ export function InsightsProvider(props: InsightsProviderProps) { React.useEffect(() => { getVisitorId().then((visitorId) => { visitorIdRef.current = visitorId; + // When the page is unloaded, flush all events, but only if the visitor ID is set + window.addEventListener('beforeunload', flushEventsSync); }); - }, []); - - // When the page is unloaded, flush all events - React.useEffect(() => { - window.addEventListener('beforeunload', flushEventsSync); return () => { window.removeEventListener('beforeunload', flushEventsSync); }; }, [flushEventsSync]); - /** - * Get the visitor ID and store it in a ref. - */ - // React.useEffect(() => { - // getVisitorId().then((visitorId) => { - // visitorIdRef.current = visitorId; - // // When the page is unloaded, flush all events, but only if the visitor ID is set - // window.addEventListener('beforeunload', flushEventsSync); - // }); - // return () => { - // window.removeEventListener('beforeunload', flushEventsSync); - // }; - // }, [flushEventsSync]); - return ( { + if (!questions) { + if (!cancelled) { + setResults([]); + setIsLoading(false); + } + captureException( + new Error(`corrupt-cache: getRecommendedQuestions is ${questions}`), + ); + return; + } + const results = questions.map((question) => ({ type: 'recommended-question', id: question, question: question, })) satisfies ResultType[]; + suggestedQuestionsRef.current = results; + if (cancelled) { + setIsLoading(false); return; } @@ -104,13 +118,30 @@ export const SearchResults = React.forwardRef(function SearchResults( setIsLoading(false); } + let cancelled = false; + debounceTimeout.current = setTimeout(async () => { const fetchedResults = await (global ? searchAllSiteContent(query, pointer) : searchSiteSpaceContent(query, pointer, revisionId)); - setResults(withAsk ? withQuestionResult(fetchedResults, query) : fetchedResults); + if (cancelled) { + return; + } + if (!results) { + captureException( + new Error( + `corrupt-cache: ${global ? 'searchAllSiteContent' : 'searchSiteSpaceContent'} is ${results}`, + ), + { extra: { results } }, + ); + setResults([]); + setIsLoading(false); + return; + } + + setResults(withAsk ? withQuestionResult(fetchedResults, query) : fetchedResults); setIsLoading(false); trackEvent({ @@ -120,6 +151,8 @@ export const SearchResults = React.forwardRef(function SearchResults( }, 350); return () => { + cancelled = true; + if (debounceTimeout.current) { clearTimeout(debounceTimeout.current); debounceTimeout.current = null; diff --git a/packages/gitbook/src/components/Search/server-actions.tsx b/packages/gitbook/src/components/Search/server-actions.tsx index 0b7aa186f..c82d48bf8 100644 --- a/packages/gitbook/src/components/Search/server-actions.tsx +++ b/packages/gitbook/src/components/Search/server-actions.tsx @@ -59,8 +59,6 @@ async function searchSiteContent(args: { cacheBust?: string; }): Promise { const { pointer, scope, query, cacheBust } = args; - console.log('searchSiteContent called'); - console.log('searchSiteContent', { pointer, scope, query, cacheBust }); if (query.length <= 1) { return []; diff --git a/packages/gitbook/src/components/SpaceLayout/SpaceLayout.tsx b/packages/gitbook/src/components/SpaceLayout/SpaceLayout.tsx index c2a11af27..8c2027665 100644 --- a/packages/gitbook/src/components/SpaceLayout/SpaceLayout.tsx +++ b/packages/gitbook/src/components/SpaceLayout/SpaceLayout.tsx @@ -76,8 +76,6 @@ export async function SpaceLayout(props: { }; const apiHost = (await api()).client.endpoint; const visitorAuthToken = await getCurrentVisitorToken(); - console.log('loading SpaceLayout'); - console.log('Auth...', visitorAuthToken); const enabled = await shouldTrackEvents(); return ( diff --git a/packages/gitbook/src/lib/csp.ts b/packages/gitbook/src/lib/csp.ts index 2a1f74628..663a2a14a 100644 --- a/packages/gitbook/src/lib/csp.ts +++ b/packages/gitbook/src/lib/csp.ts @@ -10,7 +10,6 @@ import { filterOutNullable } from './typescript'; export async function getContentSecurityPolicyNonce(): Promise { const headersList = await headers(); const nonce = headersList.get('x-nonce'); - if (!nonce) { throw new Error('No nonce found in headers'); }