Skip to content

Commit

Permalink
fix: do not throw error if a search index does not exist
Browse files Browse the repository at this point in the history
Closes #85
  • Loading branch information
cmfcmf committed Dec 14, 2021
1 parent 567f2c0 commit 9a17de6
Showing 1 changed file with 30 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,39 @@ function getItemUrl({ document }: MyItem): string {
return url;
}

function fetchIndex(baseUrl: string, tag: string): Promise<IndexWithDocuments> {
const EMPTY_INDEX = {
documents: [],
index: mylunr(function () {
this.ref("id");
this.field("title");
this.field("content");
}),
};

async function fetchIndex(
baseUrl: string,
tag: string
): Promise<IndexWithDocuments> {
if (SEARCH_INDEX_AVAILABLE) {
return fetch(`${baseUrl}search-index-${tag}.json`)
.then((content) => content.json())
.then((json) => ({
documents: json.documents as MyDocument[],
index: mylunr.Index.load(json.index),
}));
let json;
try {
const response = await fetch(`${baseUrl}search-index-${tag}.json`);
json = await response.json();
} catch (err) {
// An index might not actually exist if no pages for it have been indexed.
// https://github.com/cmfcmf/docusaurus-search-local/issues/85
// TODO: we should somehow pass the names of indexes that exist to the
// client at build time instead of catching the error here.
return EMPTY_INDEX;
}

return {
documents: json.documents as MyDocument[],
index: mylunr.Index.load(json.index),
};
} else {
// The index does not exist in development, therefore load a dummy index here.
return Promise.resolve({
documents: [],
index: mylunr(function () {
this.ref("id");
this.field("title");
this.field("content");
}),
});
return Promise.resolve(EMPTY_INDEX);
}
}

Expand Down

0 comments on commit 9a17de6

Please sign in to comment.