From e3ca1ba2b8faae4143ea9764f7f49178faf9bdfc Mon Sep 17 00:00:00 2001 From: Johannes Marbach Date: Tue, 24 Mar 2026 15:48:57 +0100 Subject: [PATCH] Load main results in pages of 3 Signed-off-by: Johannes Marbach --- assets/js/offline-search.js | 84 +++++++++++++++++++++++++++---------- 1 file changed, 62 insertions(+), 22 deletions(-) diff --git a/assets/js/offline-search.js b/assets/js/offline-search.js index 1800b96e..1d2944d1 100644 --- a/assets/js/offline-search.js +++ b/assets/js/offline-search.js @@ -116,16 +116,7 @@ search backend. $html.append($searchResultBody); // Append a spinner while we're busy. - const $spinner = $("
") - .addClass("spinner-container") - .append($("
") - .addClass("spinner-border") - .attr("role", "status") - .append($("
") - .addClass("visually-hidden") - .text("Loading..."))) - .append($("

") - .text("Loading...")); + const $spinner = createSpinner(); $searchResultBody.append($spinner) // Display the popover. @@ -152,18 +143,7 @@ search backend. $("

").text(`No results found for query "${searchQuery}"`) ); } else { - for (const [index, result] of search.results.entries()) { - // Insert a container for the result *before* the spinner. This - // will push down the spinner as new content is loaded and keep - // it at the end of the popover. - const $container = $("

"); - $spinner.before($container); - - renderResult(await result.data(), index, $container); - } - - // Remove the spinner now that everything was loaded. - $spinner.remove(); + await loadAndRenderResults(search.results, 0, $spinner, $searchResultBody); } }; }); @@ -180,6 +160,62 @@ const disposePopover = ($targetSearchInput) => { } } +const createSpinner = () => { + return $("
") + .addClass("spinner-container") + .append($("
") + .addClass("spinner-border") + .attr("role", "status") + .append($("
") + .addClass("visually-hidden") + .text("Loading..."))) + .append($("

") + .text("Loading...")); +} + +const loadAndRenderResults = async (results, offset, $spinner, $searchResultBody) => { + // Load and render the first three results and hide the remainder behind a + // button to not freeze the browser by loading results that may not be + // displayed. + const LIMIT = 3; + + for (const [index, result] of results.entries()) { + if (index < LIMIT) { + // Insert a container for the result *before* the spinner. This + // will push down the spinner as new content is loaded and keep + // it at the end of the popover. + const $container = $("

"); + $spinner.before($container); + + renderResult(await result.data(), index + offset, $container); + } else if (index === LIMIT) { + const num_hidden_results = results.length - index; + const $loader = $("