🐛 fix: race condition in search query execution

When a user opens the search modal and starts typing immediately, executeQuery() is triggered. Since buildIndex() is asynchronous, the fuse variable might still be undefined when .search() is called. Added safety checks for fuse initialization.
This commit is contained in:
Kamil Sijko committed 2026-05-05 11:50:28 +02:00
1 parent da8ca782aa
commit db9fb58bca
1 file changed
+7 -1
+7 -1
View File
@@ -151,6 +151,12 @@ function buildIndex() {
}
function executeQuery(term) {
if (!indexed) {
buildIndex();
}
if (!fuse) {
return;
}
let results = fuse.search(term);
let resultsHTML = "";
@@ -193,7 +199,7 @@ function executeQuery(term) {
}
output.innerHTML = resultsHTML;
if (results.length > 0) {
if (results.length > 0 && output.firstChild) {
first = output.firstChild.firstElementChild;
last = output.lastChild.firstElementChild;
}