Skip to content

Commit 14aeb0c

Browse files
authored
Merge pull request #2633 from GuillaumeGomez/speed-up-loading
Speed up search index loading
2 parents 3e6b42c + 7acc7a0 commit 14aeb0c

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/front-end/searcher/searcher.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/* global Mark, elasticlunr, path_to_root */
44

55
window.search = window.search || {};
6-
(function search(search) {
6+
(function search() {
77
// Search functionality
88
//
99
// You can use !hasFocus() to prevent keyhandling in your key
@@ -289,6 +289,9 @@ window.search = window.search || {};
289289

290290
// If reloaded, do the search or mark again, depending on the current url parameters
291291
doSearchOrMarkFromUrl();
292+
293+
// Exported functions
294+
config.hasFocus = hasFocus;
292295
}
293296

294297
function unfocusSearchbar() {
@@ -522,6 +525,4 @@ window.search = window.search || {};
522525

523526
loadScript(path_to_root + '{{ resource "searchindex.js" }}', 'search-index');
524527

525-
// Exported functions
526-
search.hasFocus = hasFocus;
527528
})(window.search);

src/renderer/html_handlebars/search.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ pub fn create_files(
6666
if search_config.copy_js {
6767
static_files.add_builtin(
6868
"searchindex.js",
69-
format!("Object.assign(window.search, {});", index).as_bytes(),
69+
// To reduce the size of the generated JSON by preventing all `"` characters to be
70+
// escaped, we instead surround the string with much less common `'` character.
71+
format!(
72+
"window.search = JSON.parse('{}');",
73+
index.replace("\\", "\\\\").replace("'", "\\'")
74+
)
75+
.as_bytes(),
7076
);
7177
static_files.add_builtin("searcher.js", searcher::JS);
7278
static_files.add_builtin("mark.min.js", searcher::MARK_JS);

tests/rendered_output.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -773,9 +773,10 @@ mod search {
773773
fn read_book_index(root: &Path) -> serde_json::Value {
774774
let index = root.join("book/searchindex.js");
775775
let index = fs::read_to_string(index).unwrap();
776-
let index = index.trim_start_matches("Object.assign(window.search, ");
777-
let index = index.trim_end_matches(");");
778-
serde_json::from_str(index).unwrap()
776+
let index = index.trim_start_matches("window.search = JSON.parse('");
777+
let index = index.trim_end_matches("');");
778+
// We need unescape the string as it's supposed to be an escaped JS string.
779+
serde_json::from_str(&index.replace("\\'", "'").replace("\\\\", "\\")).unwrap()
779780
}
780781

781782
#[test]

0 commit comments

Comments
 (0)