Skip to content

Commit

Permalink
perf: improve search speed
Browse files Browse the repository at this point in the history
  • Loading branch information
razonyang committed Aug 7, 2022
1 parent 100533a commit 9d3c2e4
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions assets/search/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,29 @@ class Engine {
'tags.title',
],
});
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status !== 200) {
console.error({ error: xhr.statusText });
return;
fetch(window.searchIndex).then((response)=>{
return response.json();
}).then((response) => {
const pages = response.pages;
const taxonomies = ['categories', 'authors', 'series', 'tags'];
for (const i in taxonomies) {
const datalist = document.querySelector(
'#' + taxonomies[i] + '-list'
);
const terms = response[i]
for (const j in terms) {
const option = document.createElement('option');
option.value = terms[j];
datalist.appendChild(option);
}
const pages = xhr.response.pages;
const taxonomies = ['categories', 'authors', 'series', 'tags'];
for (const i in taxonomies) {
const datalist = document.querySelector(
'#' + taxonomies[i] + '-list'
);
for (const j in xhr.response[taxonomies[i]]) {
const option = document.createElement('option');
option.value = xhr.response[taxonomies[i]][j];
datalist.appendChild(option);
}
}
this.fuse = new Fuse(pages, options);
callback(form.data());
}
};
xhr.responseType = 'json';
xhr.open('GET', window.searchIndex, true);
xhr.send(null);
this.fuse = new Fuse(pages, options);
callback(form.data());
}).catch((err) => {
console.error('unable to load search index',err)
}).finally(() => {

})
}

search(data: FormData) {
Expand Down

0 comments on commit 9d3c2e4

Please sign in to comment.