Skip to content

Commit

Permalink
Reintroduce a safety check around search whilst we continue investiga…
Browse files Browse the repository at this point in the history
…ting caching. (#2718)

Co-authored-by: Greg Bergé <[email protected]>
  • Loading branch information
emmerich and gregberge authored Jan 10, 2025
1 parent ecfdb97 commit af3c6a9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-kids-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'gitbook': minor
---

Reintroduce a safety check around search whilst we continue investigating caching.
22 changes: 22 additions & 0 deletions packages/gitbook/src/components/Search/SearchResults.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use client';
import { captureException } from '@sentry/nextjs';
import assertNever from 'assert-never';
import React from 'react';
import { useEventCallback } from 'usehooks-ts';
Expand Down Expand Up @@ -81,6 +82,16 @@ export const SearchResults = React.forwardRef(function SearchResults(

setResultsState({ results: [], fetching: true });
getRecommendedQuestions(getCtx(), spaceId).then((questions) => {
if (!questions) {
if (!cancelled) {
setResultsState({ results: [], fetching: false });
}
captureException(
new Error(`corrupt-cache: getRecommendedQuestions is ${questions}`),
);
return;
}

const results = questions.map((question) => ({
type: 'recommended-question',
id: question,
Expand Down Expand Up @@ -111,6 +122,17 @@ export const SearchResults = React.forwardRef(function SearchResults(
return;
}

if (!results) {
captureException(
new Error(
`corrupt-cache: ${global ? 'searchAllSiteContent' : 'searchSiteSpaceContent'} is ${results}`,
),
{ extra: { results } },
);
setResultsState({ results: [], fetching: false });
return;
}

setResultsState({ results, fetching: false });

trackEvent({
Expand Down
7 changes: 7 additions & 0 deletions packages/gitbook/src/components/Search/server-actions.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use server';

import { RevisionPage, SearchAIAnswer, SearchPageResult, SiteSpace, Space } from '@gitbook/api';
import { captureException } from '@sentry/nextjs';
import * as React from 'react';
import { assert } from 'ts-essentials';

Expand Down Expand Up @@ -233,6 +234,12 @@ export async function getRecommendedQuestions(
spaceId: string,
): Promise<string[]> {
const data = await api.getRecommendedQuestionsInSpace(ctx, spaceId);
if (!data.questions) {
captureException(new Error('Expected questions in getRecommendedQuestions'), {
extra: { data },
});
return [];
}
return data.questions;
}

Expand Down

0 comments on commit af3c6a9

Please sign in to comment.