Skip to content

Commit 32d9ad3

Browse files
Only load searchindex when needed
1 parent 52ee287 commit 32d9ad3

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

src/theme/index.hbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@
170170
{{#if search_enabled}}
171171
<div id="search-wrapper" class="hidden">
172172
<form id="searchbar-outer" class="searchbar-outer">
173-
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
173+
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header" disabled>
174174
</form>
175175
<div id="searchresults-outer" class="searchresults-outer hidden">
176176
<div id="searchresults-header" class="searchresults-header"></div>

src/theme/searcher/searcher.js

+33-18
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,17 @@ window.search = window.search || {};
259259
doc_urls = config.doc_urls;
260260
searchindex = elasticlunr.Index.load(config.index);
261261

262+
searchbar.removeAttribute("disabled");
263+
searchbar.focus();
264+
265+
const searchterm = searchbar.value.trim();
266+
if (searchterm !== "") {
267+
searchbar.classList.add("active");
268+
doSearch(searchterm);
269+
}
270+
}
271+
272+
function initSearchInteractions() {
262273
// Set up events
263274
searchicon.addEventListener('click', function(e) { searchIconClickHandler(); }, false);
264275
searchbar.addEventListener('keyup', function(e) { searchbarKeyUpHandler(); }, false);
@@ -271,7 +282,9 @@ window.search = window.search || {};
271282
// If reloaded, do the search or mark again, depending on the current url parameters
272283
doSearchOrMarkFromUrl();
273284
}
274-
285+
286+
initSearchInteractions();
287+
275288
function unfocusSearchbar() {
276289
// hacky, but just focusing a div only works once
277290
var tmp = document.createElement('input');
@@ -280,7 +293,7 @@ window.search = window.search || {};
280293
tmp.focus();
281294
tmp.remove();
282295
}
283-
296+
284297
// On reload or browser history backwards/forwards events, parse the url and do search or mark
285298
function doSearchOrMarkFromUrl() {
286299
// Check current URL for search request
@@ -313,7 +326,7 @@ window.search = window.search || {};
313326
}
314327
}
315328
}
316-
329+
317330
// Eventhandler for keyevents on `document`
318331
function globalKeyHandler(e) {
319332
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey || e.target.type === 'textarea' || e.target.type === 'text' || !hasFocus() && /^(?:input|select|textarea)$/i.test(e.target.nodeName)) { return; }
@@ -364,8 +377,23 @@ window.search = window.search || {};
364377
}
365378
}
366379

380+
function loadScript(url, id) {
381+
if (document.getElementById(id)) {
382+
return;
383+
}
384+
const script = document.createElement('script');
385+
script.src = url;
386+
script.id = id;
387+
script.onload = () => init(window.search);
388+
script.onerror = error => {
389+
console.error(`Failed to load \`${url}\`: ${error}`);
390+
};
391+
document.head.append(script);
392+
}
393+
367394
function showSearch(yes) {
368395
if (yes) {
396+
loadScript(path_to_root + '{{ resource "searchindex.js" }}', 'search-index');
369397
search_wrap.classList.remove('hidden');
370398
searchicon.setAttribute('aria-expanded', 'true');
371399
} else {
@@ -443,10 +471,10 @@ window.search = window.search || {};
443471
function doSearch(searchterm) {
444472
// Don't search the same twice
445473
if (current_searchterm == searchterm) { return; }
446-
else { current_searchterm = searchterm; }
447-
448474
if (searchindex == null) { return; }
449475

476+
current_searchterm = searchterm;
477+
450478
// Do the actual search
451479
var results = searchindex.search(searchterm, search_options);
452480
var resultcount = Math.min(results.length, results_options.limit_results);
@@ -467,19 +495,6 @@ window.search = window.search || {};
467495
showResults(true);
468496
}
469497

470-
function loadScript(url, id) {
471-
const script = document.createElement('script');
472-
script.src = url;
473-
script.id = id;
474-
script.onload = () => init(window.search);
475-
script.onerror = error => {
476-
console.error(`Failed to load \`${url}\`: ${error}`);
477-
};
478-
document.head.append(script);
479-
}
480-
481-
loadScript(path_to_root + '{{ resource "searchindex.js" }}', 'search-index');
482-
483498
// Exported functions
484499
search.hasFocus = hasFocus;
485500
})(window.search);

tests/gui/search.goml

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
// This tests basic search behavior.
22

3-
// We disable the requests checks because `searchindex.json` will always fail
4-
// locally (due to CORS), but the searchindex.js will succeed.
5-
fail-on-request-error: false
63
go-to: |DOC_PATH| + "index.html"
74

85
define-function: (

0 commit comments

Comments
 (0)