Skip to content

Commit 8ab48db

Browse files
Only load searchindex when needed
1 parent 1b9f584 commit 8ab48db

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
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

+25-16
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ 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+
266+
function initSearchInteractions() {
262267
// Set up events
263268
searchicon.addEventListener('click', function(e) { searchIconClickHandler(); }, false);
264269
searchbar.addEventListener('keyup', function(e) { searchbarKeyUpHandler(); }, false);
@@ -271,7 +276,9 @@ window.search = window.search || {};
271276
// If reloaded, do the search or mark again, depending on the current url parameters
272277
doSearchOrMarkFromUrl();
273278
}
274-
279+
280+
initSearchInteractions();
281+
275282
function unfocusSearchbar() {
276283
// hacky, but just focusing a div only works once
277284
var tmp = document.createElement('input');
@@ -280,7 +287,7 @@ window.search = window.search || {};
280287
tmp.focus();
281288
tmp.remove();
282289
}
283-
290+
284291
// On reload or browser history backwards/forwards events, parse the url and do search or mark
285292
function doSearchOrMarkFromUrl() {
286293
// Check current URL for search request
@@ -313,7 +320,7 @@ window.search = window.search || {};
313320
}
314321
}
315322
}
316-
323+
317324
// Eventhandler for keyevents on `document`
318325
function globalKeyHandler(e) {
319326
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 +371,23 @@ window.search = window.search || {};
364371
}
365372
}
366373

374+
function loadScript(url, id) {
375+
if (document.getElementById(id)) {
376+
return;
377+
}
378+
const script = document.createElement('script');
379+
script.src = url;
380+
script.id = id;
381+
script.onload = () => init(window.search);
382+
script.onerror = error => {
383+
console.error(`Failed to load \`${url}\`: ${error}`);
384+
};
385+
document.head.append(script);
386+
}
387+
367388
function showSearch(yes) {
368389
if (yes) {
390+
loadScript('{{ resource "searchindex.js" }}', 'search-index');
369391
search_wrap.classList.remove('hidden');
370392
searchicon.setAttribute('aria-expanded', 'true');
371393
} else {
@@ -467,19 +489,6 @@ window.search = window.search || {};
467489
showResults(true);
468490
}
469491

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('{{ resource "searchindex.js" }}', 'search-index');
482-
483492
// Exported functions
484493
search.hasFocus = hasFocus;
485494
})(window.search);

0 commit comments

Comments
 (0)