Skip to content

Commit 3ed3024

Browse files
authored
Merge pull request #2552 from GuillaumeGomez/deduplicate-search-indexes
Remove JSON search file
2 parents 43281c8 + f54356d commit 3ed3024

File tree

6 files changed

+15
-24
lines changed

6 files changed

+15
-24
lines changed

src/renderer/html_handlebars/search.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,10 @@ pub fn create_files(
6060
let index = write_to_json(index, search_config, doc_urls)?;
6161
debug!("Writing search index ✓");
6262
if index.len() > 10_000_000 {
63-
warn!("searchindex.json is very large ({} bytes)", index.len());
63+
warn!("search index is very large ({} bytes)", index.len());
6464
}
6565

6666
if search_config.copy_js {
67-
static_files.add_builtin("searchindex.json", index.as_bytes());
6867
static_files.add_builtin(
6968
"searchindex.js",
7069
format!("Object.assign(window.search, {});", index).as_bytes(),

src/theme/searcher/searcher.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ window.search = window.search || {};
363363
}
364364
}
365365
}
366-
366+
367367
function showSearch(yes) {
368368
if (yes) {
369369
search_wrap.classList.remove('hidden');
@@ -441,7 +441,6 @@ window.search = window.search || {};
441441
}
442442

443443
function doSearch(searchterm) {
444-
445444
// Don't search the same twice
446445
if (current_searchterm == searchterm) { return; }
447446
else { current_searchterm = searchterm; }
@@ -468,15 +467,18 @@ window.search = window.search || {};
468467
showResults(true);
469468
}
470469

471-
fetch(path_to_root + '{{ resource "searchindex.json" }}')
472-
.then(response => response.json())
473-
.then(json => init(json))
474-
.catch(error => { // Try to load searchindex.js if fetch failed
475-
var script = document.createElement('script');
476-
script.src = path_to_root + '{{ resource "searchindex.js" }}';
477-
script.onload = () => init(window.search);
478-
document.head.appendChild(script);
479-
});
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');
480482

481483
// Exported functions
482484
search.hasFocus = hasFocus;

tests/gui/move-between-pages.goml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// This tests pressing the left and right arrows moving to previous and next page.
22

3-
// We disable the requests checks because `searchindex.json` will always fail
4-
// locally (due to CORS), but the searchindex.js will succeed.
3+
// We disable the requests checks because `mode-rust.js` is not found.
54
fail-on-request-error: false
65

76
go-to: |DOC_PATH| + "index.html"

tests/gui/search.goml

Lines changed: 0 additions & 3 deletions
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: (

tests/gui/sidebar-nojs.goml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
// an iframe (because of JS disabled).
33
// Regression test for <https://github.com/rust-lang/mdBook/issues/2528>.
44

5-
// We disable the requests checks because `searchindex.json` will always fail
6-
// locally.
7-
fail-on-request-error: false
85
// We disable javascript
96
javascript: false
107
go-to: |DOC_PATH| + "index.html"

tests/gui/sidebar.goml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// This GUI test checks sidebar hide/show and also its behaviour on smaller
22
// width.
33

4-
// We disable the requests checks because `searchindex.json` will always fail
5-
// locally.
6-
fail-on-request-error: false
74
go-to: |DOC_PATH| + "index.html"
85
set-window-size: (1100, 600)
96
// Need to reload for the new size to be taken account by the JS.

0 commit comments

Comments
 (0)