diff --git a/assets/js/offline-search.js b/assets/js/offline-search.js index 55249083..cbdac0ae 100644 --- a/assets/js/offline-search.js +++ b/assets/js/offline-search.js @@ -73,7 +73,7 @@ search backend. } // - // Kick off the search and collect the results. + // Check if we need to do a search at all. // const searchQuery = $targetSearchInput.val(); @@ -81,37 +81,8 @@ search backend. return; } - // Show the results popover with a spinner while we're busy. - const $spinner = $("
") - .text("Loading...")); - const popover = new bootstrap.Popover($targetSearchInput[0], { - content: $spinner[0], - html: true, - customClass: "td-offline-search-results", - placement: "bottom", - }); - popover.show(); - - // Kick off the search. - const search = await pagefind.debouncedSearch(searchQuery); - if (search === null) { - // A more recent search call has been made, nothing to do. - return; - } - - // Load all result details. We may want to switch to a paged UI in future for better performance. - const results = await Promise.all(search.results.slice(0, 100).map(r => r.data())); - // - // Construct the search results html. + // Prepare the results popover. // const $html = $("
") + .text("Loading...")); + $searchResultBody.append($spinner) + + // Display the popover. + const popover = new bootstrap.Popover($targetSearchInput[0], { + content: $html[0], + html: true, + customClass: "td-offline-search-results", + placement: "bottom", + }); + popover.show(); + + // + // Kick off the search, load the results and inject them into the popover. + // + + const search = await pagefind.debouncedSearch(searchQuery); + if (search === null) { + // A more recent search call has been made, nothing to do. + return; + } + + if (search.results.length === 0) { $searchResultBody.append( $("
").text(`No results found for query "${searchQuery}"`) ); } else { - results.forEach((r, index_r) => { - // Add the main result's page title. - $searchResultBody.append($("